Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display checkbox values

Status
Not open for further replies.

bigcat48

Programmer
Aug 27, 2008
72
US
All:

I need to display checkbox values on my edit page, but what I have now displays all checkboxes checked, instead of reading the checkbox values saved in my database table.

Here's the code.
Code:
			<div class="row">
				<label>Event(s):</label>
					<?php
					  while ($event = mysql_fetch_array($events)) {
						$eventid = $event['id'];
						$ename = htmlspecialchars($event['eventName']); 
						
					    $result = @mysql_query(
					      "SELECT contact.id, contact.eventid, events.id, events.eventName 
						   FROM `contact`, `events`
					       WHERE contact.eventid = events.id");
					    if (!$result) {
					      exit('<p>Error fetching event details: ' .
					          mysql_error() . '</p>');
					    }
						
						if(mysql_num_rows($result)) {
						  echo "<span class='inline'><input type='checkbox' class='chkbx' checked='checked' name='eventid[]' value='$eventid' />$ename</span>";
						} else {
						  echo "<span class='inline'><input type='checkbox' class='chkbx' name='eventid[]' value='$eventid' />$ename</span>";
						}
					  }
					?>
			</div>

I already have another query listing the checkbox items, if that helps.
Code:
  $events = @mysql_query('SELECT id, eventName FROM events');
  if (!$events) {
    exit('<p>Unable to obtain event list from the database.</p>');
  }


Not certain if any of you need to see more to understand what I need help in. Let me know if you do.

Please help. Thank you.

 
Had the same problem not too long ago.

Check out the FAQ faq434-7120.

Make sure your fields in the MYSQL DB are set as BOOL(TinyInt(1), i think) and are null(although null is optional)

now, write your checkbox HTML code something like:

Code:
<input name = "eventid[]" type="checkbox" <?php echo (empty($result['fieldname'])) ? '' : ' checked="checked" ';?> value="1"/>

simply put you first check for the value in the MYSQL database first, before placing the "checked option in the checkbox.


-------------------------------------------------------------
"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
 
Jedel,

If I'm understanding this correctly, if I set the database field type as BOOL, the field would only accept a value of true/false or 0/1.

If this is the case, then this change will not work for what I need. Because I need to be able to add multiple values to this one field.

Currently I have set this field as follows:
field: eventid
type: VARCHAR(255)
Null: yes
Default: Null

Side Note, I'm thinking that I need to create new table establishing a 1 to 1 relationship this field and the table that holds the listing information. For example.

tblContact: id, eventid
tblEvents: id, eventName

tblContactEvents: contactid, eventsid
 
I suppose you need to make a design choice here.
Checkboxes can only take 1 of two values either checked or not. So a bool would be a good daata type.
You could have a database record for each checkbox tied back to some id. The records would be quite wide as they will have the id field, some kind of sequence and the checkbox itself.
Or on a single database row you could have the number of check boxes you have on the screen represented as that number of bool fields. This breaks a rule of relation database design by having a repeating group in a sinlge row, but you can condioer it de-normalised for performance.
I may have totaly misunderstood your issue here but I hope this helps !
 
If you want to use multiple values in teh fiels, then I'm assuming it would be a text field. In this case, try using radio buttons, or a dropdown field for your data.

If you are doing what I suspect your are, then radio buttons would be the way to go. You may just need to have one radio button per group.

Without known what your output is, it makes it kind of difficult to provide more

Cheers

Dean

-------------------------------------------------------------
"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top