By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,953 Members | 1,143 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,953 IT Pros & Developers. It's quick & easy.

posting an array or using checkboxes

P: 2
Hi everyone,
I need some help. I'm trying to tweak a code and keep hitting my head against a wall. It's for a game that I am running where a user has "horses" that they enter into "classes" which are a part of "shows." They can have multiple horses and no limits, however the horse has to meet certain requirements in order to enter the classes. At the class page, a user sees their eligible horses in a drop down select list, they hit submit, and the horse is entered. They have to do this multiple times for each of their horses. However, I am trying to make it so that they can either a) see a submit all eligible horses button, which would insert all of the eligible horses with one click or b) see their list of eligible horses as checkboxes where they can select which ones they want and submit and have the horses inserted. I really don't care which it is, if it's easier to do the submit all that's fine. I was trying to get the checkbox portion to work, but I haven't had any luck. Any help would be much appreciated. Here is the code:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start(); // Maintain session state
  3. header("Cache-control: private");    // Fixes IE6's back button problem.
  4. $page_title = "Class Information";
  5.  
  6. include('header.php');
  7. $class_id = $_GET['id'];
  8. $enter = $_POST['enter_horse'];
  9. $check = $_POST['check'];
  10. $horse_id = $_POST['horse_id'];
  11.  
  12.  
  13. //general show information
  14. $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());
  15. $row = @mysql_fetch_array($result);
  16.     $show_id = $row['show_id'];
  17.     $show_name = $row['name'];
  18.     $runs_in = $row['datedif'];
  19.     $species = $row['species'];
  20.     $type = $row['type'];
  21.     $owner_id = $row['player_id'];
  22.     if(!$row['class_id']){myError("Invalid class!");include('footer.php');}
  23.     $entry_fee = $row['entry_fee'];
  24.     $num_entries = @mysql_num_rows(@mysql_query("SELECT horse_id FROM horses_entered WHERE class_id='$class_id'"));
  25.     $runs_in = "$runs_in day[s]";
  26.  
  27. if($enter){
  28.     //ensure horse is eligible to enter
  29.     $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"));
  30.     $exists = @mysql_num_rows(@mysql_query("SELECT horse_id FROM horses_entered WHERE horse_id='$horse_id' AND class_id='$class_id' LIMIT 1"));
  31.     if($my_money < $entry_fee){myError("You cannot afford the entry fee.", 1);}
  32.     if(!$good){myError("Are you sure you own the horse and it is between 3 and 20 years of age?");
  33.     }elseif($exists){myError("That horse is already entered in this class!");
  34.     }else{
  35.         @mysql_query("INSERT INTO horses_entered(horse_id, class_id) VALUES('$horse_id', '$class_id')")or die("Cannot create entry!");
  36.         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;
  37.         }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;}
  38.         @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());
  39.         @mysql_query("UPDATE horses SET points=points+1 WHERE horse_id='$horse_id' LIMIT 1")or die("Cannot update horse points!");
  40.         myError("Class entered!");
  41.     }
  42. }
  43.  
  44. //display the show information
  45. echo "<table>
  46. <tr><td><b>Class:</td><td>#$class_id</td></tr>
  47. <tr><td><b>Show:</td><td><a href='shows.php?id=$show_id'>$show_name (#$show_id)</a></td></tr>
  48. <tr><td><b>Runs:</td><td>$runs_in</td></tr>
  49. <tr><td><b>Entry Fee:</td><td>$$entry_fee</td></tr>
  50. <tr><td><b>Total Entrants:</td><td>$num_entries</td></tr>
  51. <tr><td valign=top><b>Your Horses:</td><td>
  52. <form action='classes.php?id=$class_id' method=POST>
  53. <select name='horse_id'>
  54. ";
  55. $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());
  56. while($row = @mysql_fetch_array($result)):
  57.     $horse_id = $row['horse_id'];
  58.     $horse_name = stripslashes($row['horse_name']);
  59.     $breed = $row['breed'];
  60.     echo "<option value='$horse_id'>$horse_name (#$horse_id), $breed</option>\n";
  61.     $prev_species = $species;
  62. endwhile;
  63. if(!$horse_id){echo "<option value=0>No eligible horses!";}
  64.  
  65.  
  66. echo "
  67. </select> &nbsp;&nbsp;
  68. <input type=submit name='enter_horse' value='Enter Horse!'></td></tr></form>
  69. <tr><td valign=top><b>Entrants:</td><td>
  70. ";
  71. }
  72.  
  73. $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";
  74. $result = @mysql_query($query)or die(mysql_error());
  75. while($row = @mysql_fetch_array($result)):
  76.     $name = $row['horse_name'];
  77.     $aid = $row['horse_id'];
  78.     $breed = $row['breed'];
  79.     $page = "horses.php";
  80.     echo "<a href='$page?id=$aid'>$name (#$aid)</a>, $breed<br>\n";
  81. endwhile;
  82. if(!$aid){echo "<i>No entrants.";}
  83. echo "</td></tr>
  84. </table>";
  85.  
  86. include('footer.php');
  87. ?>
It's the if($enter) & enter_horse form that are the original part that I would like to change and add on as a separate thing (it will only display to certain users).
Sep 28 '11 #1
Share this Question
Share on Google+
3 Replies


100+
P: 1,059
in general i use a simple trick on situation like send same type of data with unknown type.

as example, if you are making a sale, you don't know how many sale line you will make, the simplest trick is something below:
Expand|Select|Wrap|Line Numbers
  1. <form>
  2. <intput id='x_1'>
  3. <intput id='x_2'>
  4. <intput id='x_3'>
  5. <intput id='x_4'>
  6. <INPUT type='hidden' id='length' value=4>
  7. </form>
  8.  
you can implement this in various ways....
Sep 28 '11 #2

P: 2
Thanks for your response. Would you mind expanding upon your solution? I'm sorry, I don't necessarily understand how to implement what you're talking about into my code.

I was thinking, could a foreach system be used for this? Or am I way off?
Sep 28 '11 #3

100+
P: 1,059
after submitting the data (say as POST) you will get
Expand|Select|Wrap|Line Numbers
  1. //$_POST['x_1']
  2. //$_POST['x_2']
  3. //$_POST['x_3']
  4. //$_POST['x_4']
  5. //$_POST['length'];
  6.  
  7. $length=$_POST['length']; //make sure this index exists
  8. //now access like below:
  9. for($i=1;$i<=$length;$i++)
  10. {
  11.   echo $_POST['x_'.$i]."<br>";
  12. }
  13.  
  14.  
try this out
Sep 29 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.