Jump to content

Checking Checkboxes


bacarudaguy

Recommended Posts

After doing a bunch more reading and back tracking, I think I'm on the right track trying to figure out how to compare two values (both coming out of a DB)... however, I'm running into an issue of having my checkboxes checked if the value is true... ::):confused:

 

Any ideas?

 

$query = "SELECT td_name, td_sname, td_phone, td_email, td_venue FROM tds WHERE td_id=$id";		
$result = @mysql_query ($query); // Run the query.

$venueq = "SELECT venue_name AS vname, venue_day AS vday FROM venues ORDER BY venue_name ASC";
$venuer = @mysql_query ($venueq);

if (mysql_num_rows($result) == 1) { // if query found the TD

// Get the user's information.
while (list($tdname, $tdsname, $tdphone, $tdemail, $tdvenue) = mysql_fetch_row ($result)) {

// Create the form.
echo '<h2>Edit TD</h2>
<form action="edit_td.php" method="post">
<p>TD Name: <input type="text" name="tdn" size="20" maxlength="30" value="' . $tdname . '" /></p>
<p>TD Screen Name: <input type="text" name="tdsn" size="20" maxlength="30" value="' . $tdsname . '" /></p>
<p>TD Phone: <input type="text" name="tdp" size="15" maxlength="15" value="' . $tdphone . '" /></p>
<p>TD Email: <input type="text" name="tde" size="25" maxlength="30" value="' . $tdemail . '" /></p>';
echo "<p>TD Venue Assignments: \n";

echo '<table border="0">';

$tdvenue = explode (";", $tdvenue);

//Pull venue name and day to build checkboxes

while (list($vname, $vday) = mysql_fetch_row($venuer)) {

$venue = $vname . '-' . $vday;

//foreach ($tdvenue AS $tdv) {

	if ($tdvenue == $venue) {
		echo '<tr>
		<td>
		<input type="checkbox" name="td_venue[]" value="' . $tdvenue . '" checked="checked" /> ' . $tdvenue . '
		</td>
		</tr>';

	} else {

		echo '<tr>
		<td>
		<input type="checkbox" name="td_venue[]" value="' . $vname . '-' . $vday . '" /> ' . $vname . '-' . $vday . '
		</td>
		</tr>';

	} //end of if

//} // end of foreach

} //end of while

echo '</table></p>';

} //end of while

echo '<p><input type="submit" name="submit" value="Submit" /> <input type="button" name="cancel" value="Cancel" onClick="location.href=(\'view_td.php\');" /></p>
<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name="id" value="' . $id . '" />
</form>';

Link to comment
Share on other sites

yeah that HTML is completely valid. I don't see how it would be echoing incorrectly, its obviously your if statement thats causing it to output HTML that will leave it unchecked.

 

echo both variables that are being tested in that if statement and make sure that they are what you expect them to be.

Link to comment
Share on other sites

yeah that HTML is completely valid. I don't see how it would be echoing incorrectly, its obviously your if statement thats causing it to output HTML that will leave it unchecked.

 

echo both variables that are being tested in that if statement and make sure that they are what you expect them to be.

I've tried that but my feeling is that I'm not handling the array properly either... anybody ever tried anything like this?

Link to comment
Share on other sites

Ahh I didnt see your use of the list function. It seems valid to me, but try changing

 

this

while (list($vname, $vday) = mysql_fetch_row($venuer)) {

$venue = $vname . '-' . $vday;

 

to something like

while ($row = mysql_fetch_row($venuer)) {
$vname = $row['vname'];
$vday = $row['vday'];

$venue = $vname . '-' . $vday;

 

And see if the issue is still there. If not you can eliminate your use of the list() as the culprit

Link to comment
Share on other sites

Ahh I didnt see your use of the list function.

 

And see if the issue is still there. If not you can eliminate your use of the list() as the culprit

Well... that certainly makes things interesting now... I get the correct amount of times looped (6 - 6 rows in the DB) and I get 6 checkboxes, all with a '-' next to them instead of data from the DB... ::):o

 

Plan C?

Link to comment
Share on other sites

No need for Plan C as that is a good result.  Now you know your variables are empty so you just need to track down the reason.

 

You could print_r() the $row and see what is in it.  Check that vname and vday are the correct names for the contents.  Echo $vname and $vday.  You say it is looping the correct number of times but mysql_num_rows will confirm that.  When you get any problems like this just echo everything and find out exactly where things stop working.

Link to comment
Share on other sites

No need for Plan C as that is a good result.  Now you know your variables are empty so you just need to track down the reason.

 

You could print_r() the $row and see what is in it.  Check that vname and vday are the correct names for the contents.  Echo $vname and $vday.  You say it is looping the correct number of times but mysql_num_rows will confirm that.  When you get any problems like this just echo everything and find out exactly where things stop working.

 

Ok... slowly making progress here... I changed this...

 

while ($row = mysql_fetch_row($venuer)) {
$vname = $row['vname'];
$vday = $row['vday'];

$venue = $vname . '-' . $vday;

 

To this....

 

while ($row = mysql_fetch_array ($venuer, MYSQL_ASSOC)) {
$vname = $row['vname'];
$vday = $row['vday'];

//print $vname;
//print $vday;

$venue = $vname . '-' . $vday;

 

Getting a little closer as the venues are now printing, but still have an issue comparing the entire string in the db to sections of it at a time looping through the while to check the checkboxes... :confused:

 

Here's the current code...

 

// Retrieve the user's information.
$query = "SELECT td_name, td_sname, td_phone, td_email, td_venue FROM tds WHERE td_id=$id";		
$result = @mysql_query ($query); // Run the query.

$venueq = "SELECT venue_name AS vname, venue_day AS vday FROM venues ORDER BY venue_name ASC";
$venuer = @mysql_query ($venueq);

if (mysql_num_rows($result) == 1) { // if query found the TD

// Get the user's information.
while (list($tdname, $tdsname, $tdphone, $tdemail, $tdvenue) = mysql_fetch_row ($result)) {

// Create the form.
echo '<h2>Edit TD</h2>
<form action="edit_td.php" method="post">
<p>TD Name: <input type="text" name="tdn" size="20" maxlength="30" value="' . $tdname . '" /></p>
<p>TD Screen Name: <input type="text" name="tdsn" size="20" maxlength="30" value="' . $tdsname . '" /></p>
<p>TD Phone: <input type="text" name="tdp" size="15" maxlength="15" value="' . $tdphone . '" /></p>
<p>TD Email: <input type="text" name="tde" size="25" maxlength="30" value="' . $tdemail . '" /></p>';
echo "<p>TD Venue Assignments: \n";

echo '<table border="0">';

$tdvenue = explode (";", $tdvenue);

//Pull venue name and day to build checkboxes

while ($row = mysql_fetch_array ($venuer, MYSQL_ASSOC)) {
$vname = $row['vname'];
$vday = $row['vday'];

//print $vname;
//print $vday;

$venue = $vname . '-' . $vday;

//foreach ($tdvenue AS $tdv) {

	if ($tdvenue == $venue) {
		echo '<tr>
		<td>
		<input type="checkbox" name="td_venue[]" value="' . $tdvenue . '" checked /> ' . $tdvenue . '
		</td>
		</tr>';

	} else {

		echo '<tr>
		<td>
		<input type="checkbox" name="td_venue[]" value="' . $vname . '-' . $vday . '" /> ' . $vname . '-' . $vday . '
		</td>
		</tr>';

	} //end of if

//} // end of foreach

} //end of while

echo '</table></p>';

} //end of while

echo '<p><input type="submit" name="submit" value="Submit" /> <input type="button" name="cancel" value="Cancel" onClick="location.href=(\'view_td.php\');" /></p>
<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name="id" value="' . $id . '" />
</form>';

Link to comment
Share on other sites

It may be just me but I still don't know what you need help with.  Is there any way to see this page in action on the Web?

 

In your last post you had:

<input type="checkbox" name="td_venue[]" value="' . $tdvenue . '" checked /> ' . $tdvenue . '

 

To take it one step further you get the data from the database (presumably you have a field which holds a true/false 1/0 value to show whether it should be checked.

 

Once you have the 1 or 0 you need an IF statement that does this:

 

if 0 then $checkedstatus="" else $checkedstatus="checked"

 

In the echo statement you then echo $checkedstatus and it will either be empty or have checked in it and the box with either not be checked or will be check.

 

Which bit of that do you need help with?

 

 

Link to comment
Share on other sites

Hey DaiLaughing - thanks for the reply... basically, when I select a checkbox (or multiple ones) it creates a string in the DB (';' separator). My problem is breaking that string up correctly (I'm currently using the 'explode' function) and comparing it to the data in the loop that is pulling the venue name and venue day data....

 

Here's a link to the test page... click the 'E' next to the 'Test User' to see the edit page where I'm needing to have the checkboxes automatically checked. You'll see the assigned venues to the 'Test User' at the end of the table.

 

http://jokerspokerleague.com/venuetest/view_td.php

 

Here's the code creating the string for the DB...

 

// Convert TD Venue array for DB
	$tdven = $_POST['td_venue'];
	$tdv = implode (";", $tdven);
//my update query uses the $tdv variable to update that field

 

Here's the most recent code too...

 

// Retrieve the user's information.
$query = "SELECT td_name, td_sname, td_phone, td_email, td_venue FROM tds WHERE td_id=$id";		
$result = @mysql_query ($query); // Run the query.

$venueq = "SELECT venue_name AS vname, venue_day AS vday FROM venues ORDER BY venue_name ASC";
$venuer = @mysql_query ($venueq);

if (mysql_num_rows($result) == 1) { // if query found the TD

// Get the user's information.
while (list($tdname, $tdsname, $tdphone, $tdemail, $tdvenue) = mysql_fetch_row ($result)) {

// Create the form.
echo '<h2>Edit TD</h2>
<form action="edit_td.php" method="post">
<p>TD Name: <input type="text" name="tdn" size="20" maxlength="30" value="' . $tdname . '" /></p>
<p>TD Screen Name: <input type="text" name="tdsn" size="20" maxlength="30" value="' . $tdsname . '" /></p>
<p>TD Phone: <input type="text" name="tdp" size="15" maxlength="15" value="' . $tdphone . '" /></p>
<p>TD Email: <input type="text" name="tde" size="25" maxlength="30" value="' . $tdemail . '" /></p>';
echo "<p>TD Venue Assignments: \n";

echo '<table border="0">';

$tdvenue = explode (";", $tdvenue);

$checked = ' checked="checked" ';

//Pull venue name and day to build checkboxes

while ($row = mysql_fetch_array ($venuer, MYSQL_ASSOC)) {
$vname = $row['vname'];
$vday = $row['vday'];

//print $vname;
//print $vday;

$venue = $vname . '-' . $vday;

//foreach ($tdvenue AS $tdv) {

	if ($tdvenue == $venue) {
		echo '<tr>
		<td>
		<input type="checkbox" name="td_venue[]" value="' . $tdvenue . '" ' . $checked . '/> ' . $tdvenue . '
		</td>
		</tr>';

	} else {

		echo '<tr>
		<td>
		<input type="checkbox" name="td_venue[]" value="' . $vname . '-' . $vday . '" /> ' . $vname . '-' . $vday . '
		</td>
		</tr>';

	} //end of if

//} // end of foreach

} //end of while

echo '</table></p>';

} //end of while

echo '<p><input type="submit" name="submit" value="Submit" /> <input type="button" name="cancel" value="Cancel" onClick="location.href=(\'view_td.php\');" /></p>
<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name="id" value="' . $id . '" />
</form>';

 

It may be just me but I still don't know what you need help with.  Is there any way to see this page in action on the Web?

 

In your last post you had:

<input type="checkbox" name="td_venue[]" value="' . $tdvenue . '" checked /> ' . $tdvenue . '

 

To take it one step further you get the data from the database (presumably you have a field which holds a true/false 1/0 value to show whether it should be checked.

 

Once you have the 1 or 0 you need an IF statement that does this:

 

if 0 then $checkedstatus="" else $checkedstatus="checked"

 

In the echo statement you then echo $checkedstatus and it will either be empty or have checked in it and the box with either not be checked or will be check.

 

Which bit of that do you need help with?

Link to comment
Share on other sites

I think I'm starting to understand.  You have one field in the DB which contains multiple things separated by a ;

 

Ignoring the fact that that is the wrong way to do it (for now) what is in the field?  The whole name of the venue or the word checked or...?

 

Back to correct database structure.  The venues should be in a different table as one record in your main table can have many venues.  More complicatedly (?) you also need yet another table because one venue can be used in many of the main table records.  You should try to learn about database design with special reference to one-to-many relationships and normalisation.  I'm happy to try to get your existing system to work but if you want to do this sort of thing in the long term you need the database knowledge.

Link to comment
Share on other sites

Back to correct database structure.  The venues should be in a different table as one record in your main table can have many venues.  More complicatedly (?) you also need yet another table because one venue can be used in many of the main table records.  You should try to learn about database design with special reference to one-to-many relationships and normalisation.  I'm happy to try to get your existing system to work but if you want to do this sort of thing in the long term you need the database knowledge.

 

I have a basic grasp of correct database structure... here's how mine is laid out. I have a DB specific for this "application", and it contains 3 tables...

 

1st Table - "days"

days_id - auto increment, unique id for table

venue_day - contains 7 rows, one for each day of the week

 

2nd Table - "tds"

td_id - auto increment, unique id for table

td_name - tournament director name

td_sname - "                      " screen name

td_phone - "                      " phone num

td_email - "                      " email

td_venue - venues td is assigned to - this is varchar(300) - reason I did this is a td could have no venues assigned, or 1-6 venues... wasn't sure of any other way to handle the string... would it be better to just have a 0 or 1 assigned to the string instead of combining the venue name and venue day?

 

3rd Table - "venues"

venue_id - auto increment, unique id

venue_name - name of venue

venue_address

venue_city

venue_state

venue_zip

venue_phone

venue_day - value created from pulling days from "days" db through a select list

venue_link - http link to venue

venue_review - http link to reviews about venue

venue_map1 - http link to google iframe

venue_map2 - http link to larger google map

venue_fg - time of 1st poker game

venue_sg - time of 2nd poker game

venue_smoking - if venue allows smoking or not

venue_food - if venue has food or not

 

Hopefully this helps you understand my DB logic a little bit... please feel free to make any improvement suggestions... like I mentioned, I wasn't sure of how to go about having a specific column of data for each venue each td is assigned too... venues come and go and get rotated to different days, so assignments change as well...

Link to comment
Share on other sites

td_venue - venues td is assigned to - this is varchar(300) - reason I did this is a td could have no venues assigned, or 1-6 venues... wasn't sure of any other way to handle the string... would it be better to just have a 0 or 1 assigned to the string instead of combining the venue name and venue day?

 

That's the bit that breaches the database rules in normalisation as you have multiple values in one field.  But breaching the rules doesn't mean it can't work.  However, for now maybe break the rules in another way and instead of having the venues in a text field consider having 6 fields which are BOOL type and so just on or off 1 or 0.  This mimics the checkbox concept that you use to display this data and should make it easier to keep straight in your head.  Then you just use the IF concept I mentioned before.

 

But as you say venues change so the best way is to link your venues table with the tds table.  Maybe called bookings.  This will probably have an auto-increment ID plus td_id and venue_id as it's only three fields.

 

To use that data you could query as you do now to get the tournament data but then run a second query to SELECT the venues which match that tournament ID.  The correct way is to use JOIN but maybe leave that until you have read a bit of the background theory.  This approach allows for no venues, one venue or many venues.  You then do a while loop through the venue bookings found and show the check boxes accordingly.

 

If you need to show the venue names they can come from that table using another query (or preferably a JOIN).

 

I hope that helps a bit.  I don't want to do the whole thing for you partly as my boss is probably wondering what I'm doing now but also because I learn(ed) best from puzzling things out with helpful pointers to where to look and what to do.

 

 

Link to comment
Share on other sites

Dai - you pose a great way of going about this logically! :D

 

From what you've said, here's what I've gathered....

 

4th table - "tournys"

tourny_id - unique id, auto increment

td_id - the td that we're "editing" to assign to a specific venue (venue_id)

venue_id - the id of the venue that the td is assigned to

venue_day - id of day of the week of venue from days table

 

If I'm thinking about this right, it could look something like this...

 

tourny_id - 0 (first entry)

td_id - 23 (our test user)

venue_id - 4 (let's say this one of the venues)

venue_day - 1 (monday)

 

tourny_id - 1 (second entry)

td_id - 23 (test user again)

venue_id - 6 (and this is another venue)

venue_day - 0 (sunday)

 

If my thinking is on the right track, I think I can make this happen... I've got a PHP/MySQL book here... I'll do some more research in to JOINS and such. Read over that section a little bit already.

 

If I haven't said it yet, I appreciate the help and insight. I'm all self taught on my PHP skill so far...

Link to comment
Share on other sites

Dai - did some playing around (nothing to the DB yet) and managed to get a result that I've somewhat been looking for... :D

 

This...

// Retrieve the user's information.
$query = "SELECT td_name, td_sname, td_phone, td_email, td_venue FROM tds WHERE td_id=$id";		
$result = @mysql_query ($query); // Run the query.

$venueq = "SELECT venue_name AS vname, venue_day AS vday FROM venues ORDER BY venue_name ASC";
$venuer = @mysql_query ($venueq);

if (mysql_num_rows($result) == 1) { // if query found the TD

// Get the user's information.
while (list($tdname, $tdsname, $tdphone, $tdemail, $tdvenue) = mysql_fetch_row ($result)) {

// Create the form.
echo '<h2>Edit TD</h2>
<form action="edit_td.php" method="post">
<p>TD Name: <input type="text" name="tdn" size="20" maxlength="30" value="' . $tdname . '" /></p>
<p>TD Screen Name: <input type="text" name="tdsn" size="20" maxlength="30" value="' . $tdsname . '" /></p>
<p>TD Phone: <input type="text" name="tdp" size="15" maxlength="15" value="' . $tdphone . '" /></p>
<p>TD Email: <input type="text" name="tde" size="25" maxlength="30" value="' . $tdemail . '" /></p>';
echo "<p>TD Venue Assignments: \n";

print_r ($tdvenue);

echo '<table border="0">';

$checked = 'checked="checked" ';

//Pull venue name and day to build checkboxes

while ($row = mysql_fetch_array ($venuer, MYSQL_ASSOC)) {

$vname = $row['vname'];
$vday = $row['vday'];

$venue = $vname . '-' . $vday;

$tdvenue2 = explode (";", $tdvenue);

//added foreach loop to get result
foreach ($tdvenue2 AS $tdven) {

	if ($venue == $tdven) {
		echo '<tr>
		<td>
		<input type="checkbox" name="td_venue[]" value="' . $venue . '" ' . $checked . '/> ' . $venue . '
		</td>
		</tr>';

	} else {

		echo '<tr>
		<td>
		<input type="checkbox" name="td_venue[]" value="' . $venue . '" /> ' . $venue . '
		</td>
		</tr>';

	} //end of if

} // end of foreach

} //end of while

echo '</table></p>';

} //end of while

echo '<p><input type="submit" name="submit" value="Submit" /> <input type="button" name="cancel" value="Cancel" onClick="location.href=(\'view_td.php\');" /></p>
<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name="id" value="' . $id . '" />
</form>';

...makes the loop check the checkboxes. However, I only had assigned 3 originally, so I noticed that it's displying 3 of each venue (6 venues), and as it finds a new one to check, it's the next one down in the series of 3... this picture should help.

 

Like you said, I know the DB structure is wrong, but I'm getting a result out of it, which means it should work. I'm sure there's a snippet of code I can use to prevent it from printing multiple rows of venues... still working on figuring that out! 8)

 

[attachment deleted by admin]

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.