vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Checkboxes in php (https://vborg.vbsupport.ru/showthread.php?t=34818)

Lesane 02-05-2002 07:39 PM

I try to determine if a variable is set and if so then the checkbox is selected. I tried it with this code but it dont works, when i execute it then all the checkboxes are checked while only one field is filled with aw so 1 checkbox has 2 be checked instead of all :(

PHP Code:

$result mysql_query("SELECT aw,awardsid,name, url FROM award");
         while (
$award mysql_fetch_array($result)) {
         
         if (Isset(
$award[aw]))
         
$lala="checked";
         else
         
$lala="";
         
       echo 
"<tr><td><input type='checkbox' name='aw[$award[awardsid]]' value='$award[awardsid]'";
          if (
$aw) {
          echo 
"selected $lala>$award[name]</td><td nowrap>";
          } else {
          echo 
$lala>$award[name]</td><td nowrap>";
          }
       echo 
"<img src=\"$award[url]\">&nbsp;&nbsp;&nbsp;</td>";
     } 

As you can c, i'm using this code to check if its set or not:

PHP Code:

if (Isset($award[aw]))
         
$lala="checked";
         else
         
$lala=""


Hope someone can help me out, any help is appreciated.

XiXora 02-05-2002 09:49 PM

i've laready asked 5 times for this :( looks like we'll have to wait to v 3 to me :(

Admin 02-06-2002 06:27 AM

Try this...
PHP Code:

$result mysql_query("SELECT aw,awardsid,name,url FROM award");
while (
$award mysql_fetch_array($result)) {
  if (
$award['aw']!='')
    
$checked='checked';
  else
    
$checked='';
         
  echo 
"<tr>
        <td>
        <input type=\"checkbox\" name=\"aw[
$award[awardsid]]\" value=\"$award[awardsid]\" $checked>
        
$award[name]
        </td>
        <td nowrap>
        <img src=\"
$award[url]\">&nbsp;&nbsp;&nbsp;
        </td>
        </tr>"
;



Lesane 02-06-2002 06:47 AM

Thanks for the replies

@firefly, that didnt worked. I tried this code and it works now :up:

PHP Code:

$lesane=$award[aw];
          if(
$lesane == "$award[awardsid]") {         
         
$lala="checked";
         } else {
         
$lala="";
         } 

Now i'm almost done with the hack but i'm stuck when you uncheck a checkbox, then the row wont be updated. The variable is still in the database. Lets say i have a award called lesane and it has currently the variable 1 in the database, now i want to uncheck it and when i click on submit then lesane must be updated as empty. Hope u can follow me and can help me out with this

Admin 02-06-2002 06:51 AM

You need to set a hidden field saying the field WAS there, and then in the parsing if the variable doesn't exists (=checkbox is not checked) remove the row.

Lesane 02-06-2002 07:02 AM

Cool, thanks Firefly. I gonna test that out.

Be Right Back :D

Lesane 02-06-2002 09:06 AM

i'm going crazy :D

i'm using the following code:

PHP Code:

<?php

include("config.php");

if (isset(
$submitok)) {


if (
$delete) { 
foreach(
$delete as $val) {
$query "DELETE FROM award WHERE aw='$uid'"
$result mysql_query($query) or die("SELECT Error: " mysql_error() . " in Query: $miet"); 
 }
if(
$result) {
print (
"deleted");
} else {
print (
"not gedeleted");
}
}


    foreach(
$aw as $val) {
    
$lala="UPDATE award SET aw='$val' WHERE awardsid='$val[awardsid]'";
    
$result mysql_query($lala) or die("SELECT Error: " mysql_error() . " in Query: $query");
       }
if(
$result) {
print (
"updated");
} else {
print (
"not updated");
}

    }

else {


?>


<center>
<h3>Awards
<hr width="75%" color="#00AAFF">
</h3>
</center><br>
<form action="<?PHP $PHP_SELF ?>">
<table width="100%" cellspacing="0" cellpadding="5" border="1">
 <tr>
  <td>Awardsname</td>
  <td>Button</td>
 
 </tr> 

<?php

include("config.php");

$result mysql_query("SELECT aw,awardsid,name, url FROM award");
         while (
$award mysql_fetch_array($result)) {
         
$lesane=$award[aw];
          if(
$lesane == "$award[awardsid]") {         
         
$lala="checked";
         } else {
         
$lala="";
         }
         
       echo 
"<tr><td><input type='checkbox' name='aw[$award[awardsid]]' value='$award[awardsid]'";
          if (
$aw) {
          echo 
"selected $lala>$award[name]</td><td nowrap>";
          } else {
          echo 
$lala>$award[name]</td><td nowrap>";
          }
       echo 
"<img src=\"$award[url]\">&nbsp;&nbsp;&nbsp;</td>";
     
     if(!
$award[aw]) {
     
$uid="aw[$award[awardsid]]";
     } else {
     
$uid=""
     }
     } 
 echo 
"<br><tr><td colspan=2 align=center>";
 echo 
"<br><input type='hidden' name='delete[]' value='$uid'>";
echo 
"<br><br><input type='submit' name='submitok' value='Submit'>";
echo 
"</td></tr></table></form>";

    }
?>

I have currently 2 awards in the database

1. Awards name with the awardsid 9 and aw is also 9
2. Lesane with the awardsid 13 and aw is empty (means not checked)

So when i execute the script then i c awards name selected and lesane is not selected and if i check the code by <input type =hidden then i c:

<input type='hidden' name='delete[]' value='aw[13]'>

so thats right, lesane is not selected and have the id 13.

Now i'm totally confused because when i click on submit then it deletes lesane totally and when i uncheck the award with the name "awards name" then that aw is not gonna deleted because only the input type = hidden was lesane :(

So my questions are:

Whats the delete query to delete only the aw row from awards where awardsid=?
&
How is it possible to delete the aw row from the award called "Awards Name" when i uncheck him?


I hope u have a solution :(

badr511 02-06-2002 09:51 AM

working now or not

badr511 02-06-2002 10:07 AM

firefly why ican't download attachment

Lesane 02-06-2002 10:15 AM

No, its still not working.

When i try 2 unselect a checkbox and press on submit then he must delete the variable from that table :?:

Lesane 02-07-2002 03:14 PM

Quote:

Originally posted by FireFly
You need to set a hidden field saying the field WAS there, and then in the parsing if the variable doesn't exists (=checkbox is not checked) remove the row.
Firefly, could you give me an example?

Admin 02-07-2002 03:28 PM

Ok, just talking in general here, but you should get the point.

If you look in the hacks database you will see something like this:
Code:

<tr align="center">
        <td bgcolor="#13486D"><img src="https://vborg.vbsupport.ru/images/folder.gif" align="left"></td>
        <td bgcolor="#1C5780" align="left" valign="top"><font face="verdana" size="2">[high]<input type="checkbox" name="install[34534]" value="yes" checked>[/high] <a href="index.php?s=&action=showhack&hackid=73">List similar usernames when moderating user registration</a></font></td>
        <td bgcolor="#13486D"><font face="verdana" size="2"><a href="https://vborg.vbsupport.ru/member.php?s=&action=getinfo&userid=3771">dlst</a></font></td>
        <td bgcolor="#1C5780" nowrap><font face="verdana" size="1">01-27-02<br><font color="#FF9C58">08:07 PM</font></font></td>
        <td bgcolor="#13486D" nowrap><font face="verdana" size="1">01-27-02<br><font color="#FF9C58">08:07 PM</font></font></td>
        <td bgcolor="#1C5780"><a href="javascript:whoinstalled(34534)"><font face="verdana" size="2">1 user</font></a></td>
        <td bgcolor="#13486D"><img src="https://vborg.vbsupport.ru/images/clear.gif" border="0" alt="0 votes - 0.00 average"></td>
</tr>
[high]<input type="hidden" name="all[34534]" value="1">[/high]

So you can see I put two fields for each hack, one is a part of an array all[], which eventually has all the ID's of the hacks in that page. The value of that field is either 0 or 1. 1 if the user already installed the hack, and 0 if he hasn't.

Then there's the real checkbox, that the user can change. That's part of the install[] array, which holds all the ID's of the hacks the user WANTS to have installed.

And here's a little bit of code from the page itself, that parses the results:
PHP Code:

        while (list($threadid,$was)=each($all)) {
            
$now=$install[$threadid];
            if (
$now=='yes' and $was=='0') {
                
// Here I install the hack!
            
} elseif ($now!='yes' and $was=='1') {
                
// Here I un-install the hack!
            
}
        } 

I'm listing the stuff from the all[] array, that has the ID's of the hacks and the state (installed or not) BEFORE the submit, and put the 1/0 into $was (meaning what WAS the state). Then I'm taking the value of the install[] with the current ID and put it into $now (which represents the state as it NEEDS to be).

And then it's really simple logic! If the hack wasn't installed ($was equals 0) and it SHOULD be now ($now equals yes), then we need to install the hack.
Otherwise, if the hack WAS installed ($was is 1) but the user un-checked the box (so $now is not yes), we need to un-install the hack.
If non of the above is true, it means that the hack is either already installed and the user didn't touch the checbox, or it is not installed but the user didn't check the checkbox neither.

*takes a deep breath* :)
Did I say I was talking in general? :D

Lesane 02-07-2002 05:35 PM

Thanks Firefly, it works almost 100%.

However one thing: $threadid. Where and how did u defined that one because i get an error:

PHP Code:

WarningVariable passed to each() is not an array or object in user.php on line 280 

I'm not using $threadid but $award[awardsid] for the id.

This is my currently code:

PHP Code:

$awards=$DB_site->query("SELECT aw,awardsid,name,url,userid FROM award");
      while (
$award=$DB_site->fetch_array($awards)) {
         if(
$award[aw] == "$award[awardsid]and $award[userid] == "$user[userid]") {         
         
$lala="checked";
         
$was="1";
         
$kol="yes";
         } else {
         
$lala="";
         
$was="0";
         
$kol="no";
         }    
          echo 
"<tr class='".getrowbg()."'><td><input type='checkbox' name='aw[$award[awardsid]]' value='$kol'";
           echo 
"$lala>$award[name]</td><td nowrap>";
           echo 
"<input type=\"hidden\" name=\"all[$award[awardsid]]\" value=\"$was\">";
           echo 
"<img src=\"$award[url]\">&nbsp;&nbsp;&nbsp;</td>";
         }
     
      while (list(
$award[awardsid],$was)=each($all)) {
            
$now=$aw[$award[awardsid]];
            if (
$now=='yes' and $was=='0') {
                
$kutje='yes';
            } elseif (
$now!='yes' and $was=='1') {
                
$kutje='no';
            }
         } 


Its a little general ;):D

Admin 02-07-2002 05:40 PM

1. What's this $kol crap? The checkbox value is always yes!

2. The whole part where you list the array and do your stuff is AFTER you SUBMIT the form, not while printing it.

Lesane 02-07-2002 06:04 PM

1.
PHP Code:

if($award[aw] == "$award[awardsid]and $award[userid] == "$user[userid]") {
$kol="yes";
         } else {
$kol="no";
         } 

So kol is yes when the checkbox has a variable.

2.
Quote:

I'm listing the stuff from the all[] array, that has the ID's of the hacks and the state (installed or not) BEFORE the submit
So i thought before the submit :D

Its working perfect now, thank you for your help Firefly. Appreciated.

Lesane 02-07-2002 07:01 PM

Quote:

Originally posted by FireFly
1. What's this $kol crap? The checkbox value is always yes!

You right, its always yes :surprised :stoned:

How did you defined the value yes?

Lesane 02-09-2002 10:42 AM

firefly, it works perfect if i have just 1 award but when i have more then 1 award then it updates the database only with the last award, it only listening to the last award. So if i have 2 awards then it updates only the 2nd award and ignores the 1st one. Could u help me with this (hopefully) last problem ;)

I have the currently code:

Code before submit:

PHP Code:

$awards=$DB_site->query("SELECT aw,awardsid,name,url,userid FROM award");
      while (
$award=$DB_site->fetch_array($awards)) {
         if(
$award[aw] == "$award[awardsid]and $award[userid] == "$user[userid]") {         
         
$lala=" checked";
         
$was="1";
         
$kol="no";
         } else {
         
$lala="";
         
$was="0";
         
$kol="yes";
         }
          echo 
"<tr class='".getrowbg()."'><td><input type='checkbox' name='aw[$award[awardsid]]' value='$kol'";
           echo 
"$lala>$award[name]</td><td nowrap>";
           echo 
"<input type=\"hidden\" name=\"all[$award[awardsid]]\" value=\"$was\">";
           echo 
"<img src=\"$award[url]\">&nbsp;&nbsp;&nbsp;</td>";
         } 


Code after submit:

PHP Code:

while (list($award[awardsid])=each($all)) {
            
$was=$all[$award[awardsid]];
            
$now=$aw[$award[awardsid]];
            if (
$now=='yes' and $was=='0') {
               
$DB_site->query("UPDATE award SET aw='$award[awardsid]' WHERE userid=$userid and awardsid=$award[awardsid]");
            } elseif (
$now!='yes' and $was=='1') {
                
$DB_site->query("UPDATE award SET aw=0 WHERE userid=$userid and awardsid=$award[awardsid]");
            }
         } 


Lesane 02-10-2002 08:27 AM

Nevermind, i got it working now :up: :)


All times are GMT. The time now is 10:19 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.02819 seconds
  • Memory Usage 1,869KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code_printable
  • (11)bbcode_php_printable
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (18)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete