Jump to content

[SOLVED] Processing a table of checkboxes / records.


Recommended Posts

I have created an HTML table by looping through a database.

My checkboxes are all created by naming them as:

 

<input type=\"checkbox\" name=\"checkbox_" . $i . "\" > </td>" .

 

Which creates checkbox_1 - checkbox_x

 

So , when they click the button and my form action comes in to play, I don't know how to loop through these checkboxes and check values in each row to insert / update the database and do other processing with it. I'm not sure if I should re-run the query to get the value of $i again and use that in the loop or if there is another way to look at the $POST var and check all the checkbox_x values til I don't have one?

 

I appreciate any help with this.

 

Thanks,

 

Adam

 

so by calling name=checkbox[], I am making it an array name? I'm not understanding the syntax...

 

Then, in the next line, I'm getting the checkbox array from post as $array = $POST['checkbox'] - and then I can loop through it?

 

Please help me to understand the top line of syntax and how this array happens - or if this is what is really going on here...

 

Thanks,

 

Adam

<input type="checkbox" name="name[]" value="1"><br>
<input type="checkbox" name="name[]" value="2"><br>
<input type="checkbox" name="name[]" value="3"><br>
<input type="checkbox" name="name[]" value="4"><br>
<input type="checkbox" name="name[]" value="5"><br>

 

<?php
$name = $_POST['name'];

if(count($name) > 0){
foreach($name AS $names){
	echo $names . "<br>\n";
}
}else {
echo "Please select one!\n";
}
?>

you should of done

 

 

<input type=\"checkbox\" name=\"checkbox[]\" > 

 

and the in the next page

 

$arrCheckbox = $_POST['checkbox'];

Can you illustrate going through array results and creating a checkbox for each record in a loop?

 

I'm having a hard time understanding how checkbox[] etc will all have unique values etc so I can loop through later...

 

Thanks,

 

Adam

mimzie, the creation of the checkboxes is done exactly as you did it in the first post. You just need to replace the name attribute with an array.

 

So like you can generate all the checkboxes and then put in a submit button. Use php to check if the submit button is clicked. If so, you can use $_POST['checkbox'] to get the entire array and store that in a variable. The 'value' field that you assign to <input> (like the one mgallforever posted) will be the values in the array. You can loop through that and get the values.

I have gotten a chance to work on this piece a little again and ran in to something else...

 

    foreach($name AS &$names){
  $key = $key + 1;
  echo "Key is " . $key . "<br />";
  if ( $key = 1 ){
    $BatchIDs = "($names";
  }
      else if ( $key < $theCount ){
    $BatchIDs .= "$names,";
  }
  else {
    $BatchIDs .= "$names)";
      } //else
}//foreach
  } //if(count)>0 
  else {
    echo "There are no orders to process. Please check the applicable orders to be sent for processing.\n";
  }

 

This echos 1,2,2,2,2,2.

It should echo 1,2,3,4,5,6 according to the # of iterations in the loop.

I found a writeup about a bug related to foreach in php but it doesn't speak of a workaround for a situation like what I'm in. Should I even be using a foreach loop?

 

I'm basically trying to build a SQL command as :

SELECT * FROM `table`WHERE batchid IN ( 1, 2, 3, 5, 6 )

 

where the ( 1, 2, 3, 5, 6 ) is the $BatchIDs.

 

Being new to php, I'm sure there is a cleaner way to do this.

 

Thanks for all your help. I will be using php lots and have a lot of experience in Delphi which is WAY different than php, but I'll get it!

- Adam

This is likely pretty hacky code, but does what I need:

 

 $name = $_POST['checkbox'];  
  if(count($name) > 0){
    $BatchIDs = "(";
    foreach($name AS $names){
    $BatchIDs .= "$names,";
}//foreach
$BatchIDs = substr_replace($BatchIDs ,"",-1);
$BatchIDs = $BatchIDs . ")";

Anyone got any comments on how to make this sexier?

Thanks,

 

Adam

Well if it works fine, leave it as is.

 

As for a more simplified version (which isn't that much more simpler than it is):

$name = $_POST['checkbox'];  
$BatchIDs = "";
  if(count($name)){
    foreach($name AS $names){
    $BatchIDs .= "$names,";
}//foreach
$BatchIDs = "(" . substr_replace($BatchIDs ,"",-1) . ")";

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.