Reputation: 33
I wonder if you guys could help me. I have a table called modules and within it it has moduleID, moduleTitle, moduleCode etc.
A checkbox is populated next to each module that is displayed and the value of the checkbox is the moduleID. When a user checks the modules and click submit they get passed onto another page. On this other page I have managed to retrieve the moduleID's which have been checked but I am having trouble in turning these moduleID so that I can display the moduleTitle, moduleCode etc for the ones that have been checked on this new page.
I have used the following code but no luck :confused:
<?php
$cats_upload = "";
foreach($_POST['module'] as $cats) // errors if no modules are selected
{
echo '<p>Module data moduleID: '.$cats.'</p>';
}
$sql = mysql_query("SELECT * FROM modules WHERE moduleID ='".$cats."'");
$productCount = mysql_num_rows($sql); //count the output amount
if($productCount > 0){
while($row = mysql_fetch_array($sql)){
$moduleTitle = $row["moduleTitle"];
$cats_upload .= "<tr><td>$moduleTitle</td></tr>";
}
} else {
$cats_upload = "<tr><td>No modules have been selected</td></tr>";
}
?>
If anyone has any suggestions to what it might be, please let me know. Thanks
Upvotes: 2
Views: 68
Reputation: 9874
$cats = array();
foreach($_POST['module'] as $cat) // errors if no modules are selected
{
echo '<p>Module data moduleID: '.$cat.'</p>';
$cats[] = intval($cat); // Assumption: ids are numeric
}
$sql = mysql_query("SELECT * FROM modules WHERE moduleID IN (".implode(',', $cats).")");
That should give you an SQL result with all matching modules.
Upvotes: 1
Reputation: 52372
$cats_upload = "";
if (!empty($_POST['module'])) {
foreach ($_POST['module'] as $moduleID) {
$modules[] = $moduleID;
}
$module_list = implode(',', $modules);
$sql = "SELECT moduleTitle FROM modules WHERE moduleID IN (" . $module_list . ")";
$result = mysql_query($sql) or die("Error in $sql: " . mysql_error());
while ($row = mysql_fetch_array($result)) {
$cats_upload .= "<tr><td>" . $row['moduleTitle'] . "</td></tr>";
}
} else {
$cats_upload .= "<tr><td>No modules have been selected</td></tr>";
}
Upvotes: 2