How do I write a query to display only the results that were checked by the user ("Y") and display only those column headings of the "Y"???
Any help would be appreciated!
Moderator: General Moderators
That all depends on what data type the field is. It could be Bool, or TinyInt, or Enum, or Char, or possibly something else. You need to find out what data type the field is, in the table.jviney wrote:I have a site where the user uses checkboxes to select options. They are stored in a MySQL database as "Y/N" answers.
How do I write a query to display only the results that were checked by the user ("Y") and display only those column headings of the "Y"???
Any help would be appreciated!
It will make a difference whether you allow a Null or not. If you don't allow a Null, you must set a Default of either 'N' or 'Y'.jviney wrote:they are enum('N', 'Y')
Code: Select all
...
$sql = "SELECT id, myYNfield, ... FROM ....";
...
echo "<input type='checkbox' name='myCheckbox' ";
if($myYNfield=='Y') echo "checked";
echo ">";
...Oh! OK, sorry I didn't understand what you wanted. Then you would want to do something like:jviney wrote:the default is "N"...what I am trying to display in the output is...
If they checked the checkmark, display the row name.
An example I have is multiple checkmarks to select, "alcohol" and "food" and "dancing". If the venue has alcohol served, the checkmark for alcohol is selected and so on. In my output I want the table to say "alcohol" if it was checked.
Code: Select all
...
while($row=mysql_fetch_array($result)) {
... // probably echo other fields, such as venue name, or whatever
if($row['alcohol']=='Y') echo "Alcohol<br />";
if($row['food']=='Y') echo "Food<br />";
if($row['dancing']=='Y') echo "Dancing<br />";
}
It's pretty hard to tell you how to code it when I don't know what you want it to look like. I presume you know basic HTML syntax (if you don't, I can't conduct a class in HTML here). Start with writing the HTML code for your table. Wherever it needs a value from the database, you just use <?php echo "...."; ?> whatever values you need. If it's different depending on something else, as in my example, you do all the conditional code (if, or while, or whatever) inside the php tags. For long stretches of HTML that doesn't need any values from PHP, you can just write plain HTML (outside any php tags), or for short stretches of HTML, you can use echo, from inside php tags. You'll find all the references and a lot of tutorials at http://w3schools.comjviney wrote:FANTASTIC It works! Thank you, thank you, thank you!
Now, I have another question, I'm having problems inserting those lines into a table format, any ideas? It would be much appreciated!
Code: Select all
if ($wantsIceCream == 'y') // with enumCode: Select all
if ($wantsIceCream) // with booleanCode: Select all
<?php print_r($row); ?>