Jump to content

[SOLVED] While & Foreach Problem


Rocketaka

Recommended Posts

Sorry if this has already been solved but none of the topics on this have been able to fix my problem. I've got a software script that stores software titles. I have a page that displays all of the software titles and gives me the ability to delete software from the database. The problem is I want to be able to use checkboxes and it's just not working.

 

So far the best I have gotten is the form will delete the software title that's the last title in the while loop. Here's my code:

 

<form method='POST' action='index.php?cat=admin&page=removesoftware' style='margin:0px;'>";
while ($data = mysql_fetch_array($query)) {
	if($class == $class1) {
		$class = $class2;
	}
	else {
		$class = $class1;
	}
	$id = $data['id'];
	$title = $data['title'];
	$category = $data['category'];
	$status = $data['status'];
	$date = $data['date'];
	$by = $data['by'];
	echo "<tr class='$class'>
		<td width='55%'>
		<input type='checkbox' name='checked[]' value='".$id."'>
		<input type='hidden' name='id' value='$id'>
		<input type='hidden' name='title' value='$title'>
		".$data['title']."</td>
		<td width='25%'>".$data['category']."</td>
		<td width='20%'>".$data['status']."</td></tr>";
}
echo "</table><br>";
echo "
<input type='submit' name='remove' style='border:1px #1469A2 solid; margin-left:10px;' value='Remove Selected'>
</form>
";

 

Here's the code when the button is pressed:

 

	if(isset($_POST['remove'])) {
	foreach ($_POST['checked'] as $val) {
		$id = $_POST['id'];
		$title = $_POST['title'];
		$sql = "DELETE FROM `software` WHERE `id` = '$id'";
		mysql_query("$sql") or die (mysql_error());
		echo "You have removed <b>".$title."</b> successfully!\n";
	}
echo "<a href='index.php?cat=admin&page=software'>Go Back?</a>";
}

 

Link to comment
https://forums.phpfreaks.com/topic/40776-solved-while-foreach-problem/
Share on other sites

How about:

if ( isset($_POST['remove']) AND sizeof($_POST['checked']) ) {
$ids = implode(',',$_POST['checked']);
foreach ( $_POST['checked'] as $tmp ) { $title_list[] = $_POST['title'][$tmp]; }
$titles = implode(', ',$title_list);
$sql = "DELETE FROM `software` WHERE `id` IN ($ids)";
mysql_query("$sql") or die (mysql_error());
echo "You have removed <b>".$titles."</b> successfully!\n";
}
echo "<a href='index.php?cat=admin&page=software'>Go Back?</a>";

 

That way it's all done in one query.

 

And update the form to include the title array:

 

	echo "<tr class='$class'>
		<td width='55%'>
		<input type='checkbox' name='checked[]' value='".$id."'>
		<input type='hidden' name='title[$id]' value='$title'>
		".$data['title']."</td>
		<td width='25%'>".$data['category']."</td>
		<td width='20%'>".$data['status']."</td></tr>";

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.