<?php
/**
* Sets 'active' to 'Y' if id included in colllection, else to 'N'
*/
$sql = sprintf(
"UPDATE table SET active = IF((id IN(%s)), 'Y', 'N')",
implode(
',',
array_map(
'intval',
$ids_from_form
)
)
);
?>
if (isset($_POST['submitted'])) {
if(is_array($_POST['current'])) // check whether any of the checkboxes has been ticked, if not - no need to perform the update
{
$id_in = implode(',', $_POST['current']); // from all the boxes that have been ticked, create a string that looks like: 1,2,3,4 so it can fit into MySQL's IN() function
mysql_query("update footer_logos SET visible = 'Y' where id IN($id_in)"); // run ONE query and update all the records that are specified by the checkboxes.
}
}
$query1 = "SELECT * FROM footer_logos";
$result1 = @mysql_query($query1); // Run the Query.
echo '<p><b>Edit "Footer Logos"</b></p>';
echo '<form name="form1" method="POST" action="update_logos.php" class="book">';
echo '<table width="550" border="0" style="padding:5px">';
echo '<tr style="background-color:#eaeaea;">
<td>Visible</td>
<td>Image</td>
<td>Link</td>
<td>ALT Text</td>
</tr>';
while ($row1 = mysql_fetch_assoc($result1)) { // use assoc, not array
if ($row1['visible']=='Y')
$checked="checked";
else
$checked="";
// use id to name each one differently
echo "<tr>
<td><input type='checkbox' name='current[{$row1['id']}]' id='current_{$row1['id']}' value='{$row1['id']}' class='maillistadd' {$checked} /></td>
<td><img src='../images/{$row1['src']}' /></td><td>{$row1['link']}</td><td>{$row1['alt']}</td>
</tr>";
}
echo '</table>';
?>
<input name="submit" type="submit" class="submit" value="Update the 'Logos' box" />
<input type="hidden" value="form1">
<input type="hidden" name="submitted" value="TRUE" />
</form>
I’d like to set visible to N in footer_logosif the current id does not appear in the $_POST[‘current’] array.
Should I select all the IDs from the footer_logos table and compare them with the $_POST[‘current’] array ?
Using Blue’s code above. I’m 90% of the way there.
And yes, potential flaw-a-mundo! It doesn’t set it to N if unticked.
Does that mean I just add a second query to set it to ‘N’ ?
e.g.
if(is_array($_POST['current'])) // check whether any of the checkboxes has been ticked, if not - no need to perform the update
{
$id_in = implode(',', $_POST['current']); // from all the boxes that have been ticked, create a string that looks like: 1,2,3,4 so it can fit into MySQL's IN() function
mysql_query("update footer_logos SET visible = 'Y' where id IN($id_in)"); // run ONE query and update all the records that are specified by the checkboxes.
} else {
mysql_query("update footer_logos SET visible = 'N' where id IN($id_in)"); // run ONE query and update all the records that are specified by the checkboxes.
}
First problem you have is by constructing the names of your checkboxes.
Having your checkboxes named “current_1”, “current_2” and so on isn’t really usable later on when you send the checkbox data to your php script.
That’s why programmers invented something called an array.
Array is a construct that allows you to group variables in such way that you can easily re-use their values and do grouped operations on those variables.
In short, had you named your checkboxes such as “current[1]”, “current[2]” you’d have much easier job handling input from them.
In terms of code, it would look like this when creating checkboxes:
// use id to name each one differently
echo "<tr>
<td><input type='checkbox' name='current[{$row1['id']]' id='current_{$row1['id']}' value='{$row1['id']}' class='maillistadd' {$checked} /></td>
<td><img src='../images/{$row1['src']}' /></td><td>{$row1['link']}</td><td>{$row1['alt']}</td>
</tr>";
And it would look like this when you want to update the database with boxes that have been ticked:
if(isset($_POST['submitted']))
{
if(is_array($_POST['current'])) // check whether any of the checkboxes has been ticked, if not - no need to perform the update
{
$id_in = implode(',', $_POST['current']); // from all the boxes that have been ticked, create a string that looks like: 1,2,3,4 so it can fit into MySQL's IN() function
mysql_query("update footer_logos SET visible = 'Y' where id IN($id_in)"); // run ONE query and update all the records that are specified by the checkboxes.
}
}
However, there’s one potential flaw - I don’t know if it was intended or not but once you check a box and update visible to Y - how can you set value of visible to something else (I am assuming other state can be N)?