Ok I know this has been answered a few times, but I just can't seem to get mine working!
I have 8 weeks in my database (set up as columns) and I display the value of these with php as follows
<form method="post" id="updating" action="<?php $_PHP_SELF ?>">
<?php
$count = 1;
foreach ($results as $v) {
while($count<9){
$week = Week.$count;
$checkvalue = $v -> $week;
?>
Week<?echo $count;?> <input type="checkbox" id="Week<?php echo$count;?>" name="week[]" value="<?php echo $checkvalue;?>" <?php if($checkvalue==1){?>
checked="checked"
<?php } ?>
/>
<?php
$count++;
}
}
?>
<input name="update" type="submit" id="update" value="Update">
</form>
I know it's probably not as graceful as it should be, but it does output the correct HTML for me and displays the information as it's presented in the database.
<form method="post" id="updating" action="">
Week1 <input type="checkbox" id="Week1" name="week[]" value="1" checked="checked"
/>
Week2 <input type="checkbox" id="Week2" name="week[]" value="0" />
Week3 <input type="checkbox" id="Week3" name="week[]" value="1" checked="checked"
/>
Week4 <input type="checkbox" id="Week4" name="week[]" value="1" checked="checked"
/>
Week5 <input type="checkbox" id="Week5" name="week[]" value="0" />
Week6 <input type="checkbox" id="Week6" name="week[]" value="1" checked="checked"
/>
Week7 <input type="checkbox" id="Week7" name="week[]" value="1" checked="checked"
/>
Week8 <input type="checkbox" id="Week8" name="week[]" value="1" checked="checked"
/>
<input name="update" type="submit" id="update" value="Update">
</form>
The problem I'm having is when I tick or untick those checkboxes, the updated values don't appear to be captured, and hence the database isn't being updated properly. Here's where the self post request is handled.
<?php if(isset($_POST['update']))
{
$count = 1;
if(isset($_POST['week']) && !empty($_POST['week']))
foreach($_POST['week'] as $w){ echo $w; //prints 1 all the time
$week = Week.$count;
echo $week;//prints out weeks I want to update correctly (database headings)
$query = $db->getQuery(true);
// Fields to update.
$fields = array(
$week.'=' . $db->quote( $w ));
// Conditions for which records should be updated.
$conditions = array(
'UserID='. (int) $user_id);
$query->update($db->quoteName('tools_engage'))->set($fields)->where($conditions);
$db->setQuery($query);
try {
$result = $db->query(); // Use $db->execute() for Joomla 3.0.
echo ("Values saved and updated </br>");
$count++;
} catch (Exception $e) {
// Catch the error.
}
}
}?>
I'm doing this in Joomla, hence the weird database update syntax, but it does work and I've used them elsewhere in this program so I know they're not the problem
As I commented in the code, the array week[] just seems to contain the value 1 for everything, no matter what boxes I check or uncheck. Which then results in every value in the database being updated to 1/true which obviously isn't right.
I have a suspicion the problem lies in my HTML output for the update form, but can't seem to crack it, any help is much appreciated!
Update: Ok so going on nvuono's suggestion I have updated my form to be like this
<?php
$count = 1;
foreach ($results as $v) {
while($count<9){
$week = Week.$count;
$checkvalue = $v -> $week;
?>
Week<?echo $count;?> <input type="checkbox" id="Week<?php echo$count;?>" name="week[<?php echo $count;?>]" value="1" <?php if($checkvalue==1){?>
checked="checked"
<?php } ?>
/>
<?php
$count++;
}
}
?>
So now each name has an index in the array does it not? If that is the case, should my
if(isset($_POST['update']))
section not now be printing out all the values in the array in the order they've been entered?
ie. My logic would be that it prints out 1,0,0,1,0,0,0 (box 1 and 4 checked) or what ever boxes are checked. But instead it just prints out 1,1 so I've no way of knowing what index they are, and subsequently what week I should be updating