Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 04-23-2010, 09:57 PM checkbox
Extreme Talker

Posts: 173
Trades: 0
I am trying to create a datagrid with a checkbox at the top of each column to allow the user to turn a column off or on.

I then want to save the options to a file so that once the user sets the columnes, it will allow the user to come back to the screen without having to select the options again.

What I am doing is storing the the options in a text string,

String 00110011

0 0 1 1 0 0 1 1
NAME DATE A B C D E F

Trying to demonstrate above that the user has selected the columns with 1 above them, but I cannot get a check box to return a value.

PHP Code:
if(isset($_POST["console"])){
 
$console=implode("",$_POST["console"]);
 
$sql='UPDATE tbl_staff SET interface ="'.$console.'" WHERE staff_id='.$_SESSION['staff_id'];
 
$result mysql_query($sql$link);
 if (!
$result) {
  echo 
'DB Error, could not query the database MySQL Error: '.mysql_error();
    exit;
 }
 
$_SESSION['interface'] = $console;
 echo 
$console;

PHP Code:
     for ($x=1;$x<strlen($_SESSION['interface']);$x++){
      
$str_header=$str_header.'<td><input type="checkbox" name="console[]" value="'.$col[$x][1].'" checked="'.$col[$x][0].'" /></td>';
     } 
dgkindy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-23-2010, 09:59 PM Re: checkbox
Extreme Talker

Posts: 173
Trades: 0
I should say, I cannot get an unchecked checkbox to return a value
dgkindy is offline
Reply With Quote
View Public Profile
 
Old 04-23-2010, 10:13 PM Re: checkbox
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
An unchecked checkbox is not going to return anything, so you will most likely need to set a hidden input field for the total count of columns so you can iterate.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 04-23-2010, 10:42 PM Re: checkbox
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
If the value of the checkboxes is a sequence then you can determine which values are missing (ie are zero):
PHP Code:
for ($x=1;$x<=strlen($_SESSION['interface']);$x++){
      
$str_header=$str_header.'<td><input type="checkbox" name="console[]" value="'.$x.'" checked="'.$col[$x][0].'" /></td>';
     } 
PHP Code:
$count strlen($_SESSION['interface']);
$consoleArr $_POST['console'];
$consolrStr '';
$x 0;
for(
$i 1$i <= $count$i++)
{
     if(
$consoleArr[$x] == $i)
     {
          
$consoleStr .= '1';
          
$x++;
     }
     else
     {
          
$consoleStr .= '0';
     }

I didn't test that code but I think it will get the job done. Also, I think you had an off-by-one error in your loop:
PHP Code:
for ($x=1;$x<strlen($_SESSION['interface']);$x++) 
should be

PHP Code:
for ($x=1;$x<=strlen($_SESSION['interface']);$x++) 
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 04-23-2010 at 10:54 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 04-24-2010, 10:02 AM Re: checkbox
Extreme Talker

Posts: 173
Trades: 0
Thanks Nullpointer for the help.

Here is the final bit of code.
PHP Code:
if(isset($_POST["console"])){

$count strlen($_SESSION['interface']); 
$consoleArr $_POST["console"];
if(
$consoleArr[0]==1){
 
$consoleStr '1'
 }
else{
 
$consoleStr '2'
 }
$x 1
for(
$i 1$i $count$i++) { 
     if(
$consoleArr[$x] == $i
     { 
          
$consoleStr .= '1'
          
$x++; 
     } 
     else 
     { 
          
$consoleStr .= '0'
     } 
}  
 
$sql='UPDATE tbl_staff SET interface ="'.$consoleStr.'" WHERE staff_id='.$_SESSION['staff_id'];
 
$result mysql_query($sql$link);
 if (!
$result) {
  echo 
'DB Error, could not query the database MySQL Error: '.mysql_error();
    exit;
 }
 
$_SESSION['interface'] = $consoleStr;

One last question, since you pointed out that I should be using <= and not just <. However, when I set the value to <= it causes one more iteration and then an error. Would you then suggest that I should subtract 1 from $count before hand?
dgkindy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to checkbox
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.16067 seconds with 12 queries