Checkbox Array
hi
i having problem form/update database page.
i use while loop print form photos listed in table - names/descriptions , user must check checkbox if he/she include photo in main gallery. goes ok except checkbox system.
i think problem lies in checkbox array won't make blank element if checkbox has not been checked. ie. if check 20th , 30th photo's array still think 1st , 2nd. need force new array elements in unchecked records on update form use conditional set values in database. can see problem? help. chris
form:
while ($row = mysqli_fetch_array($r)) {
echo '<div class="photo-thumb">
<img src="../upload/' . rawurlencode($row['photoname']) . '" height="80px" />
<a href="delete_photo.php?id=' . $row['photoid'] . '&photoname=' . $row['photoname'] . '">delete</a>
<p>' . $row['photoname'] . '</p>';
if ($row['gallery'] == 1) {echo '<p>photo gallery: <input name="gallery[]" id="gallery" type="checkbox" value="1" checked="checked" /></p>';}
else {echo '<p>photo gallery: <input name="gallery[]" id="gallery" type="checkbox" value="1"/></p>';}
echo '<p>description: <input name="photodescription[]" id="photodescription" type="text" maxlength="80" value="' . $row['photodescription'] . '" /></p>
<input name="photoid[]" id="photoid" type="hidden" value="' . $row['photoid'] . '" />';
echo '</div>';
}
update page:
<?php
$query = "select * photos";
$result = mysqli_query($dbc, $query);
$total = count($_post['photoid']);
for ($i = -1; $i <= $total; $i++)
{
$photodescription = $_post['photodescription'][$i];
$photodescription = mysqli_real_escape_string($dbc, $photodescription);
$photoid = $_post['photoid'][$i];
if (!isset($_post['gallery'][$i])) {$gallery = 0;}
else {$gallery = 1;}
$q = "update photos set photodescription=\"$photodescription\", gallery='$gallery' photoid=\"$photoid\"";
$r = mysqli_query($dbc, $q);
}
?>
i can see couple of ways of handling this.
the simplest use pair of radio buttons instead of checkbox, , make "don't include" option default. long 1 radio button selected, value appear in array.
the alternative use counter in while loop in form.
$thumb = 1;
while ($row = mysqli_fetch_array($r)) {
// inside loop set value of checkbox $thumb
// increment counter @ end of loop
$thumb++;
}
the value of each checkbox number. go through update loop, need check number:
if (isset($_post['gallery'][$i]) && $_post['gallery'] == $i + 1) {
$gallery = 1;
} else {
$gallery = 0;
}
More discussions in Develop server-side applications in Dreamweaver
adobe
Comments
Post a Comment