Forum Moderators: coopster

Message Too Old, No Replies

Urgently need some help please

Where id = array?

         

TheCoupe

12:40 pm on Apr 26, 2005 (gmt 0)

10+ Year Member



Hi, I already had this posted under a different discussion I had going, but I'm really stuck... don't know what I'm doing with this as yesterday was the first time I'd worked with array's.

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);
?>

mcibor

2:19 pm on Apr 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So you have an array of ids and want to select only them?

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.

TheCoupe

3:55 pm on Apr 26, 2005 (gmt 0)

10+ Year Member



Hi thanks, for your reply... I am wanting to display most details from the rows in the database, based on the selected checkbox id's.

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

mcibor

6:17 pm on Apr 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try doing this:
in checkbox html:
<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}?>

file check.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());

This works on my comp.

Best wishes
Michal Cibor