Jump to content

[SOLVED] Checkbox Displaying from Database


gamerzfuse

Recommended Posts

<input type="checkbox" id="heatedseats" name= "heatedseats" disabled="disabled"  value= "<?php $vehicles->heatedseats->ViewValue ?>"/>

 

This is essentially what I want to do (but this checkbox does not return the value from the database)

 

I have a databast called vehicles, a field called heatedseats and I want to take the tinyint value from that field (1 or 0) and display it as a checkbox, checked or unchecked, but also disable to checking.

This is for a features list.

 

Thanks!

You must set the checked attribute to checked if you want the check-box pre checked. (Do i win an award for most extensive use of the word checked in one sentence?) e.g.:

 

<?php
$yourValueFromDb = 1;
$checked = ($yourValueFromDb == 1) ? 'checked="checked"' : '';
echo '<input type="checkbox" id="heatedseats" name= "heatedseats" disabled="disabled" '.$checked.' "/>';
?>

 

I wrote a tutorial about working with check-boxes and databases, which you might like to read: http://www.phpfreaks.com/tutorial/working-with-checkboxes-and-a-database

I just don't understand this line:

$yourValueFromDb = 1;

 

How does this value work, or how do I ask the database if the value =1?

 

 

Indeed. It was just for demonstration. I'm assuming you'll be generating these checkboxes in a loop in any case.

 

Try the tutorial above, it really will help - you'll not need the part which shows you how to update the database with the checkboxes, but the rest is relevant

I read the tutorial, but it all seems complicated to me.

I have already setup a form to enter a 1 if the box is checked and a 0 if the box is unchecked.

 

Is there no simple line of code to then retrieve this?

Something like:

 

<?php 
$heatedseats = $vehicles->heatedseats->ViewValue
if $heatedseats == "1" {
$checked = ? 'checked="checked"';
}
?>

 

?

Thanks for you help!

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.