lbaxterl Posted December 16, 2010 Share Posted December 16, 2010 I am having trouble setting a checkbox to "checked" if the row "published" is set to "1" (0 being not checked). I know this involves an if published==1 then print "checked", but i just cant get it to work. Any help would be greatly apprecatied: <?php include "base.php"; $sql="SELECT * FROM project"; $result=mysql_query($sql); // Count table rows $count=mysql_num_rows($result); ?> <table width="500" border="0" cellspacing="1" cellpadding="0"> <form name="form1" method="post" action=""> <tr> <td> <table width="500" border="0" cellspacing="1" cellpadding="0"> <tr> <td align="center"><strong>Id</strong></td> <td align="center"><strong>title</strong></td> <td align="center"><strong>Published</strong></td> <td align="center"><strong>Promoted</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ $pub = $rows['publised']; ?> <tr> <td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td> <td align="center"><? echo $rows['title']; ?></td> <td align="center"><input name="published[]" type="checkbox" id="published" value="<? echo $rows['published']; ?>"></td> <td align="center"><input name="promoted[]" type="text" id="promoted" value="<? echo $rows['promoted']; ?>"></td> </tr> <?php } ?> <tr> <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td> </tr> </table> </td> </tr> </form> </table> <?php // Check if button name "Submit" is active, do this if($Submit){ for($i=0;$i<$count;$i++){ $sql1="UPDATE project SET published='$published[$i]', promoted='$promoted[$i]' WHERE id='$id[$i]'"; $result1=mysql_query($sql1); } } if($result1){ header("location:update_multiple.php"); } mysql_close(); ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 16, 2010 Share Posted December 16, 2010 Give this a shot. <td align="center"><input name="published[]" type="checkbox" id="published" value=<?php echo "\"{$rows['published']}\""; echo $rows['published'] == 1 ? ' checked="checked"' : ''; ?>></td> Quote Link to comment Share on other sites More sharing options...
lbaxterl Posted December 16, 2010 Author Share Posted December 16, 2010 Thanks, that was a big help, although that way didn't update the values in the db, I was able to modify it though, heres the code if your interested: <td align="center"><input name="published[]" type="checkbox" id="published" value="1" <? echo $rows['published'] == 1 ? ' checked="checked"' : ''; ?>></td> Thanks a lot! I appreciate it! Quote Link to comment Share on other sites More sharing options...
lbaxterl Posted December 17, 2010 Author Share Posted December 17, 2010 Ignore the last message, It isn't working as expected. I have the boxes checking based off the values in the database problem is if i check say the 10th row of the table and submit them it doesn't update that row of the database it updates the next blank one? I hope that makes sense. Thanks again Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 17, 2010 Share Posted December 17, 2010 The value of a checkbox is only sent if it's checked, so you need to code it so the checkboxes are associated with a particular record. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.