October 13, 200818 yr This has been driving me crazy for the past hour and a half, Let's say on a normal HTML page I have the following: <tr> <td> Event </td> <td> Date </td> <td> Adult (£) </td> <td> Child (£) </td> <td> Conc (£) </td> </tr> <tr class="row_dark"> <td>Event Name</td> <td>October, Saturday 18th, 07:00PM</td> <td> <select name = "selNumAdultTicketsEventID59"> <option value="0">0 x £39.00</option> <option value="1">1 x £39.00</option> <option value="2">2 x £39.00</option> <option value="3">3 x £39.00</option> </select> </td> <td> <select name = "selNumChildTicketsEventID59"> <option value="0">0 x £39.00</option> <option value="1">1 x £39.00</option> <option value="2">2 x £39.00</option> <option value="3">3 x £39.00</option> </select> </td> <td> <select name = "selNumConcTicketsEventID59"> <option value="0">0 x £39.00</option> <option value="1">1 x £39.00</option> <option value="2">2 x £39.00</option> <option value="3">3 x £39.00</option> </select> </td> </tr> So you can see that for EventID of 59 the user can purchase adult, child or concession tickets for the event. On the same page I have about 100 cycles of this repeated code for different events. At the moment i have some really ugly, unelegant code which I need to upgrade: if (($value > 0) && ($value < 50)) { $varSplit = explode('EventID', $varName); $x = $varSplit[1]; if ($varSplit[0] == 'selNumAdultTickets') { //echo "<br>Adult Ticket Querying Database Event ID ".$x; $query = "select * from tblEventTickets WHERE transactionID = '".mysql_real_escape_string($transactionID)."' AND eventID = '".mysql_real_escape_string($x)."'"; $result = mysql_query($query); $num_results = mysql_num_rows($result); //echo "<br>Number of results".$num_results; $adultTxt = "selNumAdultTicketsEventID".$x; $b = $_POST[$adultTxt].value; if ($num_results > 0) { //echo "<br>Update Table"; $query = "UPDATE tblEventTickets SET adultTickets='".$b."' WHERE transactionID = '".mysql_real_escape_string($transactionID)."' AND eventID = '".mysql_real_escape_string($x)."'"; } else { //echo "<br>Add to Table"; $query = "insert into tblEventTickets values ('', '".mysql_real_escape_string($transactionID)."', '".$x."', '', '".$b."','','') "; $result = mysql_query($query); if ($result) echo mysql_error(); } } ...etc } This code is duplicated twice further in order for concession tickets and child tickets. It basically looks through the HTML POSTS and finds anything with a value of more than zero i.e. user has selected a ticket - it then makes sure that this represents a value for number of adult, child or concession tickets. it then saves this to the database... where i can then pick it up later if you are still reading right now (which most probably aren't) - thank you...! i appreciate my code is absolutely shocking what i really want to do is end up with PHP cycling through all the POSTS and to create an array of the events that the user has selected in the following form: EVENT ID, ADULT TICKETS, CHILD TICKETS, CONC TICKETS EVENT ID, ADULT TICKETS, CHILD TICKETS, CONC TICKETS ... etc Have I explained this clearly enough, or not? ANY answers or feedback would be unbelievably good - I just know there are better PHP'ers out there than me, and hopefully someone will respond
October 13, 200818 yr I must confess, I didn't ready everything but I think i get what you are trying to do. First of all, I would stick in a hidden filed (perhaps dynamic) that will give you your ID for the event like so... <input type="hidden" value="59" name="eventID"> You might want to check out PHP's date() method so that you don't have to manually update your date field Take <td> <select name = "selNumAdultTicketsEventID59"> <option value="0">0 x £39.00</option> <option value="1">1 x £39.00</option> <option value="2">2 x £39.00</option> <option value="3">3 x £39.00</option> </select> </td> And convert it to a for loop to save your tired fingers!... <? echo '<td>'; echo '<select name = "' . $id . '">'; //selNumAdultTicketsEventID59, $id is a variable that you can set rather than having to write it out each time since we know the event id from our hidden field this can be set to adult/child/etc $price = "£39.00"; $maxTickets = 4; for($i = 0; $i <= $maxTickets; $i++){ //$maxTickets is the maximum you can purchase set above echo '<option value="$i">' . $i . ' x £39.00</option>'; //This will print out the box as it appears above } echo '</select>'; echo '</td>' So lets say that you have your hidden input box with your event id, and some other inputs with ADULT TICKETS, CHILD TICKETS, CONC TICKETS They will look like this once posted $_POST['aTickets'] //Adult Tickets $_POST['cTickets'] $_POST['concTickets'] $_POST['eventID'] On the page you are submitting (Make sure you are using method="post") this information to, say submit.php we can get these values into an array like so <?php $orderArray = array(); //Create an array assigned to $orderArray $orderArray[] = $_POST['aTickets']; //Adult Tickets $orderArray[] = $_POST['cTickets']; $orderArray[] = $_POST['concTickets']; $orderArray[] = $_POST['eventID']; //Then try print_r($orderArray); //You should see your values here! ?>
October 21, 200818 yr Hello, If you put [] after the input elements name, then PHP will create an array of each of these. Then you can have code such as this: <tr> <td> Event </td> <td> Date </td> <td> Adult (£) </td> <td> Child (£) </td> <td> Conc (£) </td> </tr> <tr class="row_dark"> <td>Event Name</td> <td>October, Saturday 18th, 07:00PM</td> <td> <select name = "selNumAdultTicketsEventID[59]"> <option value="0">0 x £39.00</option> <option value="1">1 x £39.00</option> <option value="2">2 x £39.00</option> <option value="3">3 x £39.00</option> </select> </td> <td> <select name = "selNumChildTicketsEventID[59]"> <option value="0">0 x £39.00</option> <option value="1">1 x £39.00</option> <option value="2">2 x £39.00</option> <option value="3">3 x £39.00</option> </select> </td> <td> <select name = "selNumConcTicketsEventID[59]"> <option value="0">0 x £39.00</option> <option value="1">1 x £39.00</option> <option value="2">2 x £39.00</option> <option value="3">3 x £39.00</option> </select> </td> </tr> If you don't put the value 59 in, it will automagically number the array. Cheers, Chris.
Create an account or sign in to comment