Checkbox in an array

  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

I have 9 checkboxes that I'm putting into an array. Here is my code:

Code: [ Select ]
<input type=\"checkbox\" value=\"1\" name=\"read_ck[]\"$read_ck />


The problem that I'm having is when a checkbox isn't checked, then it doesn't hold a position in the array, so let's say I check box # 2, 5, 7. I can't pull that from the array, because I don't know which position in the array they will be in. If you check 2 of the checkboxes, then only postion 0 & 1 will have values; however, I need all 9 positions of the array to have a value. Any ideas?

Thanks,
Jordon
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • zbrdlowski
  • Beginner
  • Beginner
  • User avatar
  • Posts: 35
  • Loc: Trencin - Slovakia / Europe

Post 3+ Months Ago

I've solved it long time ago just like this:

$count = NULL;
$cont = TRUE;
$stop = 'put amountr of checkboxes you wanna display';

while (($count <= $stop) && ($cont == TRUE)){
echo '<input type="radio" name="read_ck['.$count.']" value="blah blah">';
if ($count == ($stop )) {
$cont = FALSE;
}
}

or somethig like that... I write it from head, so please excuse some errors...

After that you gotta create loop for checkin' each checkbox:

if (isset($_POST['submit']))
{
foreach($read_ck as $index => $variable)
{
do any action you need to be done here...
$index is an array position
$variable is value for that checkbox.
}
}

was it helpful?
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

Thank you for the post! Your solution looks great, the only issue I see, which I didn't tell the full details, my checkboxes are generated by a query, so therefore I don't know how many checkboxes I will have. I might have 5 on one page and 9 on the other. That's why I was using an array to post, which works perfectly with a text box; however, due to the fact that checkboxes don't post anything when they are not checked, I can't tell check box is checked based on the position in the array.

Thanks for the suggestion! Keep them coming!

Jordon
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

  • zbrdlowski
  • Beginner
  • Beginner
  • User avatar
  • Posts: 35
  • Loc: Trencin - Slovakia / Europe

Post 3+ Months Ago

But that's not problem at all. All you need is just put some incrementing number into read_ck[] in your form. Or just count how many checkboxes is there and put that integer into $stop variable. That's it.

During creating that form, just create some variable, and let it increment with each loop that create checkboxes...

Something like:

$stop = '0';
while{
this is part of the code that creating chcbx.
$stop = $stop + 1;
or
$stop += 1;
for short...
}

rest has been described before...
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

I took many of your suggestions into play and this is what I came up with.

Checkbox code:
Code: [ Select ]
$course_set = mssql_query("SELECT * FROM SNP_crsunits WHERE crs_id = '{$course1['crs_id']}'", $connection);
    
    for ($i = 0; $course = mssql_fetch_array($course_set); $i++) {
        $read = '#FFFFFF';
        $read_ck = '';
          if ($course['readbx']=='1') {
        $read = '#A9F5A9';
        $read_ck = ' checked="checked"';
      };
        $attend = '#FFFFFF';
        $attend_ck = '';
          if ($course['attend']=='1') {
        $attend = '#A9F5A9';
        $attend_ck = ' checked="checked"';
      };
        $complete = '#FFFFFF';
        $complete_ck = '';
          if ($course['complete']=='1') {
        $complete = '#A9F5A9';
        $complete_ck = ' checked="checked"';
      };
        $discuss = '#FFFFFF';
        $discuss_ck = '';
          if ($course['discuss']=='1') {
        $discuss = '#A9F5A9';
        $discuss_ck = ' checked="checked"';
      };
      echo "      
      <tr>
      <td>{$course['unit']}<input type=\"hidden\" name=\"hd[]\" value=\"{$course['crsunit_id']}\" /></td>      
      <td align=\"center\" bgcolor=\"{$read}\">
      <input type=\"checkbox\" value=\"$i\" name=\"read_ck[]\"$read_ck /></td>
      
      <td align=\"center\" bgcolor=\"{$attend}\">
      <input type=\"checkbox\" value=\"$i\" name=\"attend_ck[]\"$attend_ck /></td>
      
      <td align=\"center\" bgcolor=\"{$complete}\">
      <input type=\"checkbox\" value=\"$i\" name=\"complete_ck[]\"$complete_ck /></td>
      
      <td align=\"center\" bgcolor=\"{$discuss}\">
      <input type=\"checkbox\" value=\"$i\" name=\"discuss_ck[]\"$discuss_ck /></td>
      </tr>
  1. $course_set = mssql_query("SELECT * FROM SNP_crsunits WHERE crs_id = '{$course1['crs_id']}'", $connection);
  2.     
  3.     for ($i = 0; $course = mssql_fetch_array($course_set); $i++) {
  4.         $read = '#FFFFFF';
  5.         $read_ck = '';
  6.           if ($course['readbx']=='1') {
  7.         $read = '#A9F5A9';
  8.         $read_ck = ' checked="checked"';
  9.       };
  10.         $attend = '#FFFFFF';
  11.         $attend_ck = '';
  12.           if ($course['attend']=='1') {
  13.         $attend = '#A9F5A9';
  14.         $attend_ck = ' checked="checked"';
  15.       };
  16.         $complete = '#FFFFFF';
  17.         $complete_ck = '';
  18.           if ($course['complete']=='1') {
  19.         $complete = '#A9F5A9';
  20.         $complete_ck = ' checked="checked"';
  21.       };
  22.         $discuss = '#FFFFFF';
  23.         $discuss_ck = '';
  24.           if ($course['discuss']=='1') {
  25.         $discuss = '#A9F5A9';
  26.         $discuss_ck = ' checked="checked"';
  27.       };
  28.       echo "      
  29.       <tr>
  30.       <td>{$course['unit']}<input type=\"hidden\" name=\"hd[]\" value=\"{$course['crsunit_id']}\" /></td>      
  31.       <td align=\"center\" bgcolor=\"{$read}\">
  32.       <input type=\"checkbox\" value=\"$i\" name=\"read_ck[]\"$read_ck /></td>
  33.       
  34.       <td align=\"center\" bgcolor=\"{$attend}\">
  35.       <input type=\"checkbox\" value=\"$i\" name=\"attend_ck[]\"$attend_ck /></td>
  36.       
  37.       <td align=\"center\" bgcolor=\"{$complete}\">
  38.       <input type=\"checkbox\" value=\"$i\" name=\"complete_ck[]\"$complete_ck /></td>
  39.       
  40.       <td align=\"center\" bgcolor=\"{$discuss}\">
  41.       <input type=\"checkbox\" value=\"$i\" name=\"discuss_ck[]\"$discuss_ck /></td>
  42.       </tr>


Then my action code to make that post to the DB:

Code: [ Select ]
  $read_checkboxes = is_array($_POST['read_ck'])? array_flip($_POST['read_ck']) : array();
  $attend_checkboxes = is_array($_POST['attend_ck'])? array_flip($_POST['attend_ck']) : array();
  $complete_checkboxes = is_array($_POST['complete_ck'])? array_flip($_POST['complete_ck']) : array();
  $discuss_checkboxes = is_array($_POST['discuss_ck'])? array_flip($_POST['discuss_ck']) : array();
  
  for($i=0;$i<count($_POST['hd']);$i++) {
    
    if (isset($read_checkboxes[$i])) {
      mssql_query("UPDATE SNP_crsunits SET readbx='1' WHERE crsunit_id='{$_POST[hd][$i]}'");
    } else {
      mssql_query("UPDATE SNP_crsunits SET readbx='0' WHERE crsunit_id='{$_POST[hd][$i]}'");
    }
    
    if (isset($attend_checkboxes[$i])) {
      mssql_query("UPDATE SNP_crsunits SET attend='1' WHERE crsunit_id='{$_POST[hd][$i]}'");
    } else {
      mssql_query("UPDATE SNP_crsunits SET attend='0' WHERE crsunit_id='{$_POST[hd][$i]}'");
    }
    
    if (isset($complete_checkboxes[$i])) {
      mssql_query("UPDATE SNP_crsunits SET complete='1' WHERE crsunit_id='{$_POST[hd][$i]}'");
    } else {
      mssql_query("UPDATE SNP_crsunits SET complete='0' WHERE crsunit_id='{$_POST[hd][$i]}'");
    }
    
    if (isset($discuss_checkboxes[$i])) {
      mssql_query("UPDATE SNP_crsunits SET discuss='1' WHERE crsunit_id='{$_POST[hd][$i]}'");
    } else {
      mssql_query("UPDATE SNP_crsunits SET discuss='0' WHERE crsunit_id='{$_POST[hd][$i]}'");
    }
  1.   $read_checkboxes = is_array($_POST['read_ck'])? array_flip($_POST['read_ck']) : array();
  2.   $attend_checkboxes = is_array($_POST['attend_ck'])? array_flip($_POST['attend_ck']) : array();
  3.   $complete_checkboxes = is_array($_POST['complete_ck'])? array_flip($_POST['complete_ck']) : array();
  4.   $discuss_checkboxes = is_array($_POST['discuss_ck'])? array_flip($_POST['discuss_ck']) : array();
  5.   
  6.   for($i=0;$i<count($_POST['hd']);$i++) {
  7.     
  8.     if (isset($read_checkboxes[$i])) {
  9.       mssql_query("UPDATE SNP_crsunits SET readbx='1' WHERE crsunit_id='{$_POST[hd][$i]}'");
  10.     } else {
  11.       mssql_query("UPDATE SNP_crsunits SET readbx='0' WHERE crsunit_id='{$_POST[hd][$i]}'");
  12.     }
  13.     
  14.     if (isset($attend_checkboxes[$i])) {
  15.       mssql_query("UPDATE SNP_crsunits SET attend='1' WHERE crsunit_id='{$_POST[hd][$i]}'");
  16.     } else {
  17.       mssql_query("UPDATE SNP_crsunits SET attend='0' WHERE crsunit_id='{$_POST[hd][$i]}'");
  18.     }
  19.     
  20.     if (isset($complete_checkboxes[$i])) {
  21.       mssql_query("UPDATE SNP_crsunits SET complete='1' WHERE crsunit_id='{$_POST[hd][$i]}'");
  22.     } else {
  23.       mssql_query("UPDATE SNP_crsunits SET complete='0' WHERE crsunit_id='{$_POST[hd][$i]}'");
  24.     }
  25.     
  26.     if (isset($discuss_checkboxes[$i])) {
  27.       mssql_query("UPDATE SNP_crsunits SET discuss='1' WHERE crsunit_id='{$_POST[hd][$i]}'");
  28.     } else {
  29.       mssql_query("UPDATE SNP_crsunits SET discuss='0' WHERE crsunit_id='{$_POST[hd][$i]}'");
  30.     }
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

It may work, but if server load is an issue at all, then that's a bad way to go about it. Any time you see yourself doing that, it's a bad idea. Aggregate your results by setting a boolean variable in each of your if's, then build one update query (with the new boolean vars plugged in) and run that instead.

Here's some pseudo for you:
Code: [ Select ]
$stmt = $dbh->prepare("UPDATE SNP_crsunits SET field1=?, field2=? WHERE crsunit_id='{$_POST[hd][$i]}'");

$stmt->bindParam(1, $bool1);
$stmt->bindParam(2, $bool2);

bool1 = false;
bool2 = false;

if(blah)
  bool1 = true;
if(something else)
  bool2 = true;

$stmt->execute();
  1. $stmt = $dbh->prepare("UPDATE SNP_crsunits SET field1=?, field2=? WHERE crsunit_id='{$_POST[hd][$i]}'");
  2. $stmt->bindParam(1, $bool1);
  3. $stmt->bindParam(2, $bool2);
  4. bool1 = false;
  5. bool2 = false;
  6. if(blah)
  7.   bool1 = true;
  8. if(something else)
  9.   bool2 = true;
  10. $stmt->execute();
  • Tannu4u
  • Proficient
  • Proficient
  • User avatar
  • Posts: 480
  • Loc: India

Post 3+ Months Ago

Do not assign a value of "1" to all of them, assign an incremental value to each of them, so for eg for first checkbox assign a value of 1, for the 5th checkbox assign a value of 5.

In this way you will be able to make out which checkbox was checked based on the value that you get from the POST.

I hope this helps.
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

I have to assign 1 to all of them, because, when I query to see if they are check or not, I'm looking for a value of 1. If I just used an incremental value, then I would never know if it was check or not.
  • zbrdlowski
  • Beginner
  • Beginner
  • User avatar
  • Posts: 35
  • Loc: Trencin - Slovakia / Europe

Post 3+ Months Ago

Then why don't you use radiobutton instead...?
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

The application that I'm developing is a checklist. Basically when you complete a step then you check off that it's complete. We also need the ability to uncheck in case we need to go back. So, due to this a radio button doesn't make sense.

Thank you for every's help!

Jordon
  • zbrdlowski
  • Beginner
  • Beginner
  • User avatar
  • Posts: 35
  • Loc: Trencin - Slovakia / Europe

Post 3+ Months Ago

But in this case you can simply check it out with

if(empty()) & if (isset())
Or
if(!empty()) & if (!isset())

Or another way, you can ceck it with javascript before you submit the form...
  • Tannu4u
  • Proficient
  • Proficient
  • User avatar
  • Posts: 480
  • Loc: India

Post 3+ Months Ago

You are only getting the value of those check boxes that are checked, and not for the one's that you haven't.

So assign them an incremental value and see which one is checked.
  • zbrdlowski
  • Beginner
  • Beginner
  • User avatar
  • Posts: 35
  • Loc: Trencin - Slovakia / Europe

Post 3+ Months Ago

But he says, that after filling up some part of the form, he must check somethin'... I think that's the bad way to do it. If there will be Javascript with some OnChange() statement, he will have it automatically... I guess.

Or maybe if he will check whether are there empty cells he can send it back to form.
Meanwhile values of those non-empty cells could be stored into $_SESSION variables and put back to proper fields...

I did it like this looooong time before and it worx good
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

I got this working and posted my solution! I know that somebody said it wasn't the best way of doing it, because of performance; however, this is a app for 10 people on an internal site, not a big deal.

Thanks for all y'all's help!

Jordon
  • zbrdlowski
  • Beginner
  • Beginner
  • User avatar
  • Posts: 35
  • Loc: Trencin - Slovakia / Europe

Post 3+ Months Ago

Okay, Try this:
in the part of the script which goes after form submitting put this:

$message = NULL;
if (empty($_POST['name_of_var'])) {
$variable_1 = FALSE;
$message .= '<p>Field Nr 1. is empty!</p>';
} else {
$variable_1 = $_POST['name_of_var'];
}

You can put as many of them as necessary. After that you gotta do simple check:

if ($variable_1 && $variable_2 && $variable_3 etc...)
{
put your script which process form values here
}
else {
print $message;
}
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

zbrdlowski, the OP has reached a solution for the problem already. You can stop now. ;)
  • zbrdlowski
  • Beginner
  • Beginner
  • User avatar
  • Posts: 35
  • Loc: Trencin - Slovakia / Europe

Post 3+ Months Ago

Okay... I just realized, there are many ways how to get the same result as he want... :D

Post Information

  • Total Posts in this topic: 18 posts
  • Users browsing this forum: No registered users and 69 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.