0

How would I recode this to use checkboxes for each $result ? I want to make it so that instead of selecting different results from the drop down multiple times, a user can check the boxes of results they want and submit them that way.

echo "<table>
<tr><td><b>Class:</td><td>#$class_id</td></tr>
<tr><td><b>Show:</td><td><a href='shows.php?id=$show_id'>$show_name (#$show_id)</a></td></tr>
<tr><td><b>Runs:</td><td>$runs_in</td></tr>
<tr><td><b>Entry Fee:</td><td>$$entry_fee</td></tr>
<tr><td><b>Total Entrants:</td><td>$num_entries</td></tr>
<tr><td valign=top><b>Your Horses:</td><td>
<form action='classes.php?id=$class_id' method=POST>
<select name='horse_id'>
";
$result = @mysql_query("SELECT horse_name, breed, horse_id FROM horses WHERE player_id='$player_id' AND age>2 AND age<=20 ORDER BY horse_name ASC")or die("Cannot find horses! " . mysql_error());
while($row = @mysql_fetch_array($result)):
	$horse_id = $row['horse_id'];
	$horse_name = stripslashes($row['horse_name']);
	$breed = $row['breed'];
	echo "<option value='$horse_id'>$horse_name (#$horse_id), $breed</option>\n";
	$prev_species = $species;
endwhile;
if(!$horse_id){echo "<option value=0>No eligible horses!";}


echo "
</select> &nbsp;&nbsp;
<input type=submit name='enter_horse' value='Enter Horse!'></td></tr></form>
<tr><td valign=top><b>Entrants:</td><td>
";
4
Contributors
19
Replies
21
Views
5 Years
Discussion Span
Last Post by Ezzaral
Featured Replies
  • 1
    cereal 1,419   5 Years Ago

    Remove select tags and change line 16 to this:[code]echo "<input type='checkbox' name='horse_id[]' value='$horse_id' /> $horse_name (#$horse_id), $breed\n";[/code] This will give you an array, if then you need to select from the database you can use implode() from PHP and in() from MySQL to do a single query: [code]$a = implode(',',$_POST['horse_id']); … Read More

  • 1
    diafol 3,669   5 Years Ago

    Don't ever send a form to itself. Send the form (method attribute) to a form handler page which will do your processing and then you can decide whether to redirect back to the form or not. Also you can force an array on $_POST['horse_id] by placing (array) before it. Check … Read More

  • 1
    diafol 3,669   5 Years Ago

    yes, but checkboxes are easier. I wouldn't use a textlist. A bit pointless. look, checkboxes send info to server on form submit if they are checked or they don't if not checked. check for $_POST['mycheckbox']: [CODE]if(isset($_POST['mycheckbox'])){ $myarray = (array) $_POST['mycheckbox']; $mynewstring = "(" . implode("),(",$myarray) . ")"; //for insert $myselectstring … Read More

1

Remove select tags and change line 16 to this:

echo "<input type='checkbox' name='horse_id[]' value='$horse_id' /> $horse_name (#$horse_id), $breed\n";

This will give you an array, if then you need to select from the database you can use implode() from PHP and in() from MySQL to do a single query:

$a = implode(',',$_POST['horse_id']);
$query = "select * from horses where id in ($a)";

Credits to ardav :)

Votes + Comments
too kind cereal! :)
0

Thank you for your help! I'm confused as to where I would put the $a and $query.

if($enter){
	//ensure horse is eligible to enter
	$good = @mysql_num_rows(@mysql_query("SELECT horse_id FROM horses WHERE horse_id='$horse_id' AND player_id='$player_id' AND age>=2 AND age<=20"));
	$exists = @mysql_num_rows(@mysql_query("SELECT horse_id FROM horses_entered WHERE horse_id='$horse_id' AND class_id='$class_id' LIMIT 1"));
	if($my_money < $entry_fee){myError("You cannot afford the entry fee.", 1);}
	if(!$good){myError("Are you sure you own the horse and it is between 3 and 20 years of age?");
	}elseif($exists){myError("That horse is already entered in this class!");
	}else{
		@mysql_query("INSERT INTO horses_entered(horse_id, class_id) VALUES('$horse_id', '$class_id')")or die("Cannot create entry!");
		if($type == 1 AND $entry_fee){@mysql_query("UPDATE players SET money=money+'$entry_fee' WHERE player_id='$owner_id' LIMIT 1")or die("Cannot update player money!"); $points=1;
		}elseif($type == 2 AND $entry_fee){@mysql_query("UPDATE clubs SET money=money+'$entry_fee' WHERE president='$owner_id' LIMIT 1")or die("Cannot update player money2!"); $points=2;}
		@mysql_query("UPDATE players SET money=money-'$entry_fee', points=points+'$points' WHERE player_id='$player_id' LIMIT 1")or die("Cannot update player money3! " . @mysql_error());
		@mysql_query("UPDATE horses SET points=points+'$points' WHERE horse_id='$horse_id' LIMIT 1")or die("Cannot update horse points!");
		myError("Class entered!");
	}
}

This is the other portion of the code, does it change what you're instructing me to do?

0

I think I'm definitely missing something... I'm seeing this:

Warning: implode() [function.implode]: Invalid arguments passed in /home5/horsegal/public_html/classes.php on line 102
0

Thank you for your help! I'm confused as to where I would put the $a and $query.

Depends on what you want to achieve with the input sent. I don't see implode() in your code. The error message you got happens only if $_POST is not an array but a string.

0

Cereal, is there any way I could send you the code for the page I'm looking at? I really appreciate your help. I'm seeing things exactly the way I want to see them, they just aren't functioning.

However, I found that

$horse_id = $_POST['horse_id'];

is in the very beginning of the page, which would explain the conflict. Not sure how to go about fixing that?

1

Don't ever send a form to itself. Send the form (method attribute) to a form handler page which will do your processing and then you can decide whether to redirect back to the form or not.

Also you can force an array on $_POST by placing (array) before it. Check that it's set first of course (with isset()).

0

Sorry about the PMs, I'll claim ignorance!

Okay, would it help if I showed you the original coding?

<?php
session_start(); // Maintain session state
header("Cache-control: private");	// Fixes IE6's back button problem.
$page_title = "Class Information";

include('header.php');
$class_id = $_GET['id'];
$enter = $_POST['enter_horse'];
$horse_id = $_POST['horse_id'];


//general show information
$result = @mysql_query("SELECT s.show_id, s.player_id, s.type, s.name, DATEDIFF(s.run_date, NOW()) AS datedif, c.class_id, s.entry_fee FROM classes c, shows s WHERE c.class_id='$class_id' AND c.show_id=s.show_id LIMIT 1")or die("Cannot find class! " . mysql_error());
$row = @mysql_fetch_array($result);
	$show_id = $row['show_id'];
	$show_name = $row['name'];
	$runs_in = $row['datedif'];
	$species = $row['species'];
	$type = $row['type'];
	$owner_id = $row['player_id'];
	if(!$row['class_id']){myError("Invalid class!");include('footer.php');}
	$entry_fee = $row['entry_fee'];
	$num_entries = @mysql_num_rows(@mysql_query("SELECT horse_id FROM horses_entered WHERE class_id='$class_id'"));
	$runs_in = "$runs_in day[s]";

if($enter){
	//ensure horse is eligible to enter
	$good = @mysql_num_rows(@mysql_query("SELECT horse_id FROM horses WHERE horse_id='$horse_id' AND player_id='$player_id' AND age>=4 AND age<=20"));
	$exists = @mysql_num_rows(@mysql_query("SELECT horse_id FROM horses_entered WHERE horse_id='$horse_id' AND class_id='$class_id' LIMIT 1"));
	if($my_money < $entry_fee){myError("You cannot afford the entry fee.", 1);}
	if(!$good){myError("Are you sure you own the horse and it is between 4 and 20 years of age?");
	}elseif($exists){myError("That horse is already entered in this class!");
	}else{
		@mysql_query("INSERT INTO horses_entered(horse_id, class_id) VALUES('$horse_id', '$class_id')")or die("Cannot create entry!");
		if($type == 1 AND $entry_fee){@mysql_query("UPDATE players SET money=money+'$entry_fee' WHERE player_id='$owner_id' LIMIT 1")or die("Cannot update player money!"); $points=1;
		}elseif($type == 2 AND $entry_fee){@mysql_query("UPDATE clubs SET money=money+'$entry_fee' WHERE president='$owner_id' LIMIT 1")or die("Cannot update player money2!"); $points=2;}
		@mysql_query("UPDATE players SET money=money-'$entry_fee', points=points+'$points' WHERE player_id='$player_id' LIMIT 1")or die("Cannot update player money3! " . @mysql_error());
		@mysql_query("UPDATE horses SET points=points+'$points' WHERE horse_id='$horse_id' LIMIT 1")or die("Cannot update horse points!");
		myError("Class entered!");
	}
}


//display the show information
echo "<table>
<tr><td><b>Class:</td><td>#$class_id</td></tr>
<tr><td><b>Show:</td><td><a href='shows.php?id=$show_id'>$show_name (#$show_id)</a></td></tr>
<tr><td><b>Runs:</td><td>$runs_in</td></tr>
<tr><td><b>Entry Fee:</td><td>$$entry_fee</td></tr>
<tr><td><b>Total Entrants:</td><td>$num_entries</td></tr>
<tr><td valign=top><b>Your Horses:</td><td>
<form action='classes.php?id=$class_id' method=POST>
<select name='horse_id'>
";
$result = @mysql_query("SELECT horse_name, breed, horse_id FROM horses WHERE player_id='$player_id' AND age>3 AND age<=20 ORDER BY horse_name ASC")or die("Cannot find horses! " . mysql_error());
while($row = @mysql_fetch_array($result)):
	$horse_id = $row['horse_id'];
	$horse_name = stripslashes($row['horse_name']);
	$breed = $row['breed'];
	echo "<option value='$horse_id'>$horse_name (#$horse_id), $breed</option>\n";
	$prev_species = $species;
endwhile;
if(!$horse_id){echo "<option value=0>No eligible horses!";}


echo "
</select> &nbsp;&nbsp;
<input type=submit name='enter_horse' value='Enter Horse!'></td></tr></form>
<tr><td valign=top><b>Entrants:</td><td>
";

$query = "SELECT h.horse_name, h.horse_id, h.breed FROM horses_entered he LEFT JOIN horses h ON he.horse_id=h.horse_id WHERE he.class_id='$class_id' ORDER BY h.horse_name ASC";
$result = @mysql_query($query)or die(mysql_error());
while($row = @mysql_fetch_array($result)):
	$name = $row['horse_name'];
	$aid = $row['horse_id'];
	$breed = $row['breed'];
	$page = "horses.php";
	echo "<a href='$page?id=$aid'>$name (#$aid)</a>, $breed<br>\n";
endwhile;
if(!$aid){echo "<i>No entrants.";}
echo "</td></tr>
</table>";

include('footer.php');
?>

Like I said, my end goal is to have checkboxes instead of the drop down, so that multiple horses can be selected and submitted at once instead of one at a time. Make sense?

0

Okay, if we scratch the checkbox idea, is it possible to instead have a "submit all eligible horses button?" By that I mean, instead of the original drop down select list, could the horses names be echoed in a text list, and upon hitting the submit button all of those results are run through the query to insert them into the classes?

1

yes, but checkboxes are easier. I wouldn't use a textlist. A bit pointless.

look, checkboxes send info to server on form submit if they are checked or they don't if not checked.

check for $_POST:

if(isset($_POST['mycheckbox'])){
  $myarray = (array) $_POST['mycheckbox'];
  $mynewstring = "(" . implode("),(",$myarray) . ")"; //for insert
  $myselectstring = implode(",",$myarray); //for select

  $sql = "INSERT INTO .... (horse_id) VALUES($mynewstring)"; //for inserting
  $sql2 = "SELECT .... FROM ... WHERE horse_id IN ($myselectstring)"; //for selecting
}

This type of code to create checkboxes in first place:

echo "<input name=\"mycheckbox[]\" type=\"checkbox\" value=\"$horse_id\" /> $horse_name (#$horse_id), $breed";
0

Thank you ardav. I'm struggling to understand... I'm about to give up entirely! This looks promising, and I can sort of feel my brain almost comprehending it :D

I'm going to try to implement this now. Crossing my fingers! Thank you for your help.

0

Hmm. It appears as though I'm getting an unexpected end error after inserting this code.

If you wouldn't mind, could you clarify a little bit more for me? (forgive me, I'm blonde):

if(isset($_POST['mycheckbox'])){
  $myarray = (array) $_POST['mycheckbox'];
  $mynewstring = "(" . implode("),(",$myarray) . ")"; //for insert
  $myselectstring = implode(",",$myarray); //for select

  $sql = "INSERT INTO .... (horse_id) VALUES($mynewstring)"; //for inserting
  $sql2 = "SELECT .... FROM ... WHERE horse_id IN ($myselectstring)"; //for selecting
}

Am I to place this near where the if[enter] information and whatnot is ?
As far as the sql and sql2, are the insert/select to mirror the original insert/select? For the $sql, I'm not sure where the $class_id comes into play, which is pretty paramount.

0

Alright, so, I inserted everything, I have no idea if it's correct, but it isn't working. I'm getting the "Are you sure you own the horse and it is between 3 and 20 years of age?" which is from

if($enter){
	//ensure horse is eligible to enter
	$good = @mysql_num_rows(@mysql_query("SELECT horse_id FROM horses WHERE horse_id='$horse_id' AND player_id='$player_id' AND age>=2 AND age<=20"));
	$exists = @mysql_num_rows(@mysql_query("SELECT horse_id FROM horses_entered WHERE horse_id='$horse_id' AND class_id='$class_id' LIMIT 1"));
	if($my_money < $entry_fee){myError("You cannot afford the entry fee.", 1);}
	if(!$good){myError("Are you sure you own the horse and it is between 3 and 20 years of age?");
	}elseif($exists){myError("That horse is already entered in this class!");
	}else{
		@mysql_query("INSERT INTO horses_entered(horse_id, class_id) VALUES('$horse_id', '$class_id')")or die("Cannot create entry!");
		if($type == 1 AND $entry_fee){@mysql_query("UPDATE players SET money=money+'$entry_fee' WHERE player_id='$owner_id' LIMIT 1")or die("Cannot update player money!"); $points=1;
		}elseif($type == 2 AND $entry_fee){@mysql_query("UPDATE clubs SET money=money+'$entry_fee' WHERE president='$owner_id' LIMIT 1")or die("Cannot update player money2!"); $points=2;}
		@mysql_query("UPDATE players SET money=money-'$entry_fee', points=points+'$points' WHERE player_id='$player_id' LIMIT 1")or die("Cannot update player money3! " . @mysql_error());
		@mysql_query("UPDATE horses SET points=points+1 WHERE horse_id='$horse_id' LIMIT 1")or die("Cannot update horse points!");
		myError("Class entered!");
	}
}

However the drop down list still works, because it is, in fact, valid...

I DO want those same restrictions/conditions applied to the checked horses. Does this change anything?

0

OK, it's me that now doesn't understand. You say you've included everything, but are still using a dropdown. My code is set up to use checkboxes.

From (trying to!) read you code, it seems a little overcomplicated - heck of a lot of sql going on.

I had a go at optimizing it, but I'm not sure how it's supposed to work.

You seem to have 3 main inputs:

URL parameter: class
Form field: enter (checkbox??)
Form field(s): horse_id (select or checkbox)

Can horse_id be an array (multiple values?)

Which ones MUST be set?
What happens when one isn't set?

Why is 'class' set in the url? Can't you include it as a hidden field in the form? It shouldn't make much difference, but it's just ugly/ easy to mess with.

0

:) Sorry, I know it must be confusing.

You're right, there is still the drop down option. That is because there are two levels of membership. I would like for the upgraded members to see the checkbox option, as it makes the process easier (and perhaps have the checkboxes toggled automatically upon loading). I didn't include that in the code, but there is the ability to set the if condition for upgrades...

I didn't code this myself, if I did then I could probably do what needed to be done! I have only been able to fix minor bugs by myself so far.

The class id is derived from the url, as when looking at a show, the user sees all the different classes, clicks the one they want, and enteres the horse in the class.

Horse_id can be a number of different values. A member can have as many or as few horses as they want, and each horse has a different id. That's why there are the older than 2 but less than 20 conditions set for which horses appear to them when entering shows.

I'm not sure what happens when nothing is set because there is always one horse showing in the drop down box (if already entered, brings them back to the page with the already entered message) and I haven't been able to get that far with the checkboxes.

That's why I was wondering if it might be easier to go with an "enter all eligible horses" button where it would take those multiple ids of the horses who fit the conditions and enter them all... versus the checkboxes.

0

(oh and by the way, I paid for this code, I am not stealing it, if that's what made you angry!)

0

It seems pretty obvious from this post that he is irritated that you bailed out of this ongoing thread and started a new one to go in some other direction.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.