Jump to content

Filling Checkboxes


lbaxterl

Recommended Posts

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();
?>



Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.