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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

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.