Forum Moderators: coopster
I have id values passed via checkboxes on a form to another page. I need to be able to get the resulting rows from MySQL. Below is what I have so far, and don't know how to move one from this... this is probably something fairly simple but my brain is fried and I'm running out of time to get this completed for my boss :(
I am using Dreamweaver for the main layout etc, but manual coding as much as possible. Any help is appreciated.
<?php
$checked_Recordset1 = "0";
if (isset($_SESSION['checkboxed'])) {
$checked_Recordset1 = (get_magic_quotes_gpc())? $_SESSION['checkboxed'] : addslashes($_SESSION['checkboxed']);
}
$colname_Recordset1 = "1";
if (isset($_SESSION['MM_Username'])) {
$colname_Recordset1 = (get_magic_quotes_gpc())? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_xerox, $xerox);
$query_Recordset1 = sprintf("SELECT * FROM job WHERE company = '%s' AND id = '%s'", $colname_Recordset1,$checked_Recordset1);
$Recordset1 = mysql_query($query_Recordset1, $xerox) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
If yes then write:
$ids = $_POST["ids"];//add slashes, here it's not so relevant
$ids = implode(",", $ids);//turns an array into a string with ids separated by commas
$query_Recordset1 = sprintf("SELECT * FROM job WHERE company = '%s' AND id IN (%s)", $colname_Recordset1, $ids);
The function WHERE id IN (1,5,7) will do the job.
Hope this helps
Michal Cibor
BTW you can get the array by
<input type="checkbox" name="ids[]" value="<?php echo $id;?>">
the [] in name means to php that $_POST["ids"] is an array. Only checked values will appear.
I have the checkboxes passing the id's to an array and am displaying them on the results page via:
<?php
$n = count($checkboxed);
for ($i = 0; $i <= $n; $i++)
{
echo ($checkboxed[$i]);
echo '<br>';
}?>
but I don't know how to do a loop on the database to select the rows based on the array values which are the row ID values.
Thanks
<form action="check.php" action="POST">
<?php for($i = 0; $i < 10; $i++)
{echo $i;?>
<input type="checkbox" name="ids[]" value="<?php echo $i;?>">
<?php}?>
<?php if(isset($_POST["ids"])) $ids = implode(',', $_POST["ids"]);
else $ids = 0;
echo "Ids: ".$ids.".<br>";
$ask = "SELECT * FROM job WHERE id IN ($ids)";
$answer = mysql_query($ask) or die(mysql_error());
Best wishes
Michal Cibor