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-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 08:24 PM.

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.01622 seconds
  • Memory Usage 1,789KB
  • 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
  • (6)bbcode_php_printable
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (8)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
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete