Trying to make checkbox checked using:
$sel = mysql_query("SELECT headingid FROM headings");
while ($row2 = mysql_fetch_array($sel)) {
$headings[] = $row2['headingid'];
}
if(!$_POST['Submit']) {
$records = mysql_query("SELECT headingid FROM mheadings WHERE dataid = '".NumValidate($_REQUEST['dataid'])."'");
while ($row1 = mysql_fetch_array($records)) {
$headingid = $row1['headingid'];
}
}else{
$headingid = $_POST['heading'];
}
if(isset($_REQUEST['edit']) AND in_array($headingid, $headings)) $checked = 'checked'; else $checked = '';
It is checking all the boxes when there are only 3 matches… Im sure Im on the right track?
Thanks
You’re on the right track. I presume that later on you use a loop of some kind to show the check boxes for the headings? It’s within that loop where you’ll want to assign $checked each time you go through the loop.
Yep in the loop is:
echo '<td><input type="checkbox" name="heading[]" value="'.$headingid.'" '.$checked.'></td>
But it apears to be checking all the boxes.
It is checking all the boxes because you are assigning $checked only once, before the loop has occurred.
You need to assign $checked from within the loop instead, so that for each checkbox a different $checked value is calculated.
im sure that im checking it in the loop…
$sql = mysql_query("SELECT headingid, heading FROM headings");
while ($row = mysql_fetch_array($sql)) {
foreach($row as $key=>$value){
$$key = ValidateOutput($value);
}
if(isset($_REQUEST['edit']) AND in_array($headingid, $headings)) $checked = 'checked'; else $checked = '';
echo '<td><input type="checkbox" name="heading[]" value="'.$headingid.'" '.$checked.'></td>';
}
The rest of the code posted is outside of the loop… Obvioulsy not correct?
It would apear that $headingid if echoed is repeating (displaying) the last headingid matched 11 times… there are also 11 headings in total.
Would there need to be a foreach in there somewhere as well?
My apologies, the presentation of the code makes it tricky to comprehend.
The checkbox becomes checked when each $headingid appears in the $headings array (and of course when in edit mode).
Is it a correct assumption that $headings contains the id’s of the checkboxes that you want to be ticked?
Yea sorry I have just been taking bits of the code out to place here.
$headings is all the ids available for all headings…
$headingid are the values that exist and need to be checked
matrix_:
Yea sorry I have just been taking bits of the code out to place here.
$headings is all the ids available for all headings…
$headingid are the values that exist and need to be checked
By definition, each $headingid is contained inside $headings, so the comparison will always be true.
Are you able to explain the theory behind how the script should determine the difference between what should be checked and not checked?
3 tables…
headings table
data table
association table
Listings can go under mutiple heading… association table holds:
headingid and dataid
if a headingid in the association table matches a headingid in the headings table then check the checkbox on edit…
Hope that makes some sence.
Thanks
Okay, that makes better sense.
Is the value of $headingid appearing correctly in the form within the value attribute?
matrix_:
It would apear that $headingid if echoed is repeating (displaying) the last headingid matched 11 times… there are also 11 headings in total.
Would there need to be a foreach in there somewhere as well?
No not yet, let’s track things back further, to find out where the problem lies.
Can you move the echo to between the while loop and the foreach loop.
I want you to see what $row[‘headingid’] looks like before the foreach has had a go at it.
If I put it in the correct place you asked, I beleave I have… it echoed the last headingid found once
May I have a look at the updated code?
$sel = mysql_query("SELECT headingid FROM headings");
while ($row2 = mysql_fetch_array($sel)) {
$headings[] = $row2['headingid'];
}
if(!$_POST['Submit']) {
$records = mysql_query("SELECT headingid FROM mheading WHERE dataid = '".NumValidate($_REQUEST['dataid'])."'");
while ($row1 = mysql_fetch_array($records)) {
$headingid1 = $row1['headingid'];
}
}else{
$headingid1 = $_POST['heading'];
}
$sql = mysql_query("SELECT headingid, headings FROM headings");
echo ''.$headingid1.'<br />';
while ($row = mysql_fetch_array($sql)) {
foreach($row as $key=>$value){
$$key = ValidateOutput($value);
}
if ($numcolsprinted == $numcols) {
echo '<tr>';
$numcolsprinted = 0;
}
if(isset($_REQUEST['edit']) AND in_array($headingid1, $headings)) $checked = 'checked'; else $checked = '';
echo '<td><input type="checkbox" name="heading[]" value="'.$headingid.'" '.$checked.'></td>
<td>'.$headings.'</td>';
$numcolsprinted++;
}
for ($i=1; $i<=$colstobalance; $i++) {
$colstobalance = $numcols - $numcolsprinted;
}
I now see that there is $headingid1 which is coming from $_POST
Are you wanting the checkbox to show only when the $headingid from the database matches $headingid1 from $_POST ?
if else
If the form is submited then match the POST $headingid (this is if mandatory fields are missed the form reloads populated) else match the mysql query results for that record
if $heading1 match $headings make those checkboxes checked
So if I understand correctly, you want an unsubmitted form to set the checkboxes based on the database, and when the form is submitted you want to use the submitted values instead of the database ones.
To do that, you will want something like this within the while loop
if (form has been submitted) {
$checkid = the id from the posted form
} else {
$checkid = the id from the database
}
$checked = ($headingid === $checkid) ? ' checked' : '';
Isnt that the same as?
if(!$_POST['Submit']) {
$records = mysql_query("SELECT headingid FROM mheading WHERE dataid = '".NumValidate($_REQUEST['dataid'])."'");
while ($row1 = mysql_fetch_array($records)) {
$headingid1 = $row1['headingid'];
}
}else{
$headingid1 = $_POST['heading'];
}
Yes, they seem similar.
Can you confirm that $headingid1 is always receiving the correct value?