Display ticked checkbox's after insert into db and page return

I have a series of categories related to hotel services and each category has a selection of checkbox’s for instance the ‘Rooms’ section has air conditioning, balcony, bathrobes, bathtub, beach view and so on, there loads of them and these are for the admin guys here to fill in as we put hotels into the db.

After selecting they click ‘submit’ and these selections are stored in an array and entered into their sections in the db, that bit is fine, its when the page returns I need those checkbox’s to still be checked to confirm everything.

So below is the what creates the checkbox’s for one section as the page loads -

$q=mysql_query('select * from tbl_amenities WHERE ((Id_Ame > 200) && (Id_Ame < 401)) order by Nom_Ame') or die(mysql_error());
while($r=mysql_fetch_row($q)) { $Ramenities[$r[0]]=array('id'=>$r[0],'name'=>$r[1], 'selected'=>0 ); } 

Rooms:
<?php $cont=0; ?>
<?php foreach($Ramenities as $id=>$values ) { $cont++; ?>
<?php if($cont==4) { ?><tr> <?php } ?>
<td class="t1"><input type='checkbox' name='chame[<?php echo $id?>]'
<?php  if($values['selected']==1) print("checked"); ?>><?php echo $values['name']?></td>
<?php  if($cont==3) { ?></tr><?php $cont=0; }  ?>
<?php } ?>

So that builds the checkbox’s, and then when admin are happy with their selctions, its inserted into the db as below

if(isset($_POST['chame']))
{
$amearr=$_POST['chame'];
$keyarr=array_keys($amearr);
$i=0;
$cnt=count($amearr);
for($i=0;$i<$cnt;$i++)
{
$ins=mysql_query("insert into tbl_hotname (Id_Hot,Id_Ame) values($id,$keyarr[$i])") or die(mysql_error());
}
} 

The page then refresh’s and what I cant work out is how do I tick the box’s that where selected so to confirm after page load.

That’s what this part is doing:

<?php  if($values['selected']==1) print("checked"); ?>

so if the checkbox was selected it adds “checked” to the html for the checkbox. So you’d need to do the same once the page has been refreshed.

Ye I see that now, so though ah this is easy but cant blimming work it out.

I’ve tried all angles, will carry on though and try and get there.

I put it at the end of the INSERT script, but for some reason unlike all the other variables, it doesn’t influence the checkboxes

Post the code.

Ok this is the code inside the INSERT script

$q=mysql_query(" select Id_HnS, Id_Stand from tbl_hotnstand where Id_Hot=$germanID");
while($r=mysql_fetch_row($q)){ $standards[$r[1]]['selected']=1; }

$q=mysql_query(" select Id_HnA, Id_Ame from tbl_hotname where Id_Hot=$germanID AND ((Id_Ame > 0) && (Id_Ame < 201))");
while($r=mysql_fetch_row($q)){ $Gamenities[$r[1]]['selected']=1; echo $Gamenities[$r[1]]."visit"; }

$q=mysql_query(" select Id_HnA, Id_Ame from tbl_hotname where Id_Hot=$germanID AND ((Id_Ame > 200) && (Id_Ame < 401))");
while($r=mysql_fetch_row($q)){ $Ramenities[$r[1]]['selected']=1; }

$q=mysql_query(" select Id_HnA, Id_Ame from tbl_hotname where Id_Hot=$germanID AND ((Id_Ame > 400) && (Id_Ame < 601))");
while($r=mysql_fetch_row($q)){ $Samenities[$r[1]]['selected']=1; }

$q=mysql_query(" select Id_HnA, Id_Ame from tbl_hotname where Id_Hot=$germanID AND ((Id_Ame > 600) && (Id_Ame < 801))");
while($r=mysql_fetch_row($q)){ $Mamenities[$r[1]]['selected']=1; }

$q=mysql_query(" select Id_HnA, Id_Ame from tbl_hotname where Id_Hot=$germanID AND ((Id_Ame > 800) && (Id_Ame < 1001))");
while($r=mysql_fetch_row($q)){ $Aamenities[$r[1]]['selected']=1; }

$q=mysql_query(" select Id_HnT, Id_Tem from tbl_hotntem where Id_Hot=$germanID");
while($r=mysql_fetch_row($q)){ $thems[$r[1]]['selected']=1; }

$q=mysql_query(" select id_HnT, Id_TOp from tbl_hotntop where Id_Hot=$germanID");
while($r=mysql_fetch_row($q)){ $touroperators[$r[1]]['selected']=1; }

I put an echo in $Gamenities just to check that it was counting the checked boxes, and it’s coming back correct. But then the code below is not reflecting those boxes that are checked.

 STANDARDS
<?php $cont=0; ?>
<?php foreach($standards as $id=>$values ) { $cont++; ?>
<?php if($cont==4) { ?><tr> <?php } ?>
<td class="t1"><input type='checkbox' name='chstd[<?php echo $id?>]'
<?php  if($values['selected']==1) print("checked"); ?>><?php echo $values['name']?></td>
<?php  if($cont==3) { ?></tr><?php $cont=0; }  ?>
<?php } ?>

					 
General:
<?php $cont=0; ?>
<?php foreach($Gamenities as $id=>$values ) { $cont++;?>
<?php if($cont==4) { ?><tr> <?php } ?>
<td class="t1"><input type='checkbox' name='chame[<?php echo $id?>]'
<?php  if($values['selected']==1) print("checked"); ?>><?php echo $values['name']?></td>
<?php  if($cont==3) { ?></tr><?php $cont=0; }  ?>
<?php } ?>

If you var_dump any of the arrays you created, does it give you what you might expect? And when it’s produced the HTML code, if you do a “view source” in the browser, does that show anything? On the latter point I’m wondering whether the issue is needing to echo " checked" to make sure there’s a space after the name, but it’s a while since I’ve tried it and I can’t remember whether html cares about that.

With the echo I put inside the Gamenties which is here -

$q=mysql_query(" select Id_HnA, Id_Ame from tbl_hotname where Id_Hot=$germanID AND ((Id_Ame > 0) && (Id_Ame < 201))");
while($r=mysql_fetch_row($q)){ $Gamenities[$r[1]]['selected']=1; echo $Gamenities[$r[1]]."visit"; }

If I select 6 checkboxes for example I get this -

ArrayvisitArrayvisitArrayvisitArrayvisitArrayvisitArrayvisit

So that’s fine.

I viewed source and no sign of selected, so they def not being passed over.

When I var_dump as below, please correct me if its wrong

$q=mysql_query(" select Id_HnA, Id_Ame from tbl_hotname where Id_Hot=$germanID AND ((Id_Ame > 0) && (Id_Ame < 201))");
while($r=mysql_fetch_row($q)){ $Gamenities[$r[1]]['selected']=1; echo $Gamenities[$r[1]]."visit"; }

var_dump ($Gamenities[$r[1]]);

I get NULL

You get NULL because you’re dumping the array outside your while() loop, and by that time $r has no value (which is how the loop exited). Just dump the whole array

var_dump($Gamenities);

You could also try:

$q=mysql_query(" select Id_HnA, Id_Ame from tbl_hotname where Id_Hot=$germanID AND ((Id_Ame > 0) && (Id_Ame < 201))");
while($r=mysql_fetch_row($q)){ $Gamenities[$r[1]]['selected']=1; echo $r[1] . " - " . $Gamenities[$r[1]]['selected']; }

and see if it is setting the array element.

Ok ye sorry

array(10) { [9]=> array(1) { ["selected"]=> int(1) } [8]=> array(1) { ["selected"]=> int(1) } [6]=> array(1) { ["selected"]=> int(1) } [5]=> array(1) { ["selected"]=> int(1) } [4]=> array(1) { ["selected"]=> int(1) } [3]=> array(1) { ["selected"]=> int(1) } [2]=> array(1) { ["selected"]=> int(1) } [50]=> array(1) { ["selected"]=> int(1) } [1]=> array(1) { ["selected"]=> int(1) } [49]=> array(1) { ["selected"]=> int(1) } }

I selected 10 of the checkboxes this time

Well, I can’t see it. I manually created an array and copied your code into it to display, and I get three ticked checkboxes. I can only guess it must be something to do with the other code, for example I can’t see where you create the [‘name’] field, or where (if) you build the un-ticked entries. I didn’t need to add a space before “checked”, so that was a red herring.

when the page loads first time so no hotel selected I think it uses the code below to create the checkboxes -

$q=mysql_query('select * from tbl_standards order by Id_Stand');
while($r=mysql_fetch_row($q)) {	$standards[$r[0]]=array('id'=>$r[0],'name'=>$r[6], 'selected'=>0 ); }

$q=mysql_query('select * from tbl_amenities WHERE ((Id_Ame > 0) && (Id_Ame < 201)) order by Nom_Ame') or die(mysql_error());
while($r=mysql_fetch_row($q)) { $Gamenities[$r[0]]=array('id'=>$r[0],'name'=>$r[1], 'selected'=>0 ); }

And is all that still present after you’ve done the inserts? When you var_dumped $Gamenities above, it only had the selected entries, or did you edit out the rest for privacy or whatever? And is the id field the same in the array, as you use $r[0] in one and $r[1] in the other, albeit with different queries?

Ye that’s an observation I made too, the var dump only showed the selected ones, and no I didn’t clean anything out there.

But on page refresh the whole lot of he checkboxes are there, just not the ones selected.

mmmm interesting

I think I’ve got it, nothing really wrong with the code just that the loading of the checkboxes was being over written due to them being outside an if statement.

I moved them inside, and it seems to have done the trick. But thank you as I wouldn’t have got there unless we had sort of gone through it like we did.

Cheers

p.s hopefully wont be back

Good to know, thanks for the update.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.