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
https://forums.phpfreaks.com/topic/221870-filling-checkboxes/
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/221870-filling-checkboxes/#findComment-1148155
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
https://forums.phpfreaks.com/topic/221870-filling-checkboxes/#findComment-1148200
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
https://forums.phpfreaks.com/topic/221870-filling-checkboxes/#findComment-1148586
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.