Jump to content

Do I Need Arrays?


eddcaton

Recommended Posts

Hello, 

 

I have done some work with PHP but just can't seem to find a solution to my problem.

 

I would like to be able to make a ticket reservation system for a local charity. So what i would like to end up with is a form with text fields that will take the users details etc etc... << Which i have working.

 

When the user comes to choosing how many tickets they want, it will give them 3 different tickets they can get and a text field of drop down menu next to them:

Adult 

Kid

Family (2 Adults + 2 Kids)

 

So for an example the user has selected 1 adult and 2 kids i want to be able to insert 3 separate rows to one MYSQL table with the correct information on them (e.g. 1 Adult row and 2 Kids rows).

 

Here is what is really confusing me as i don't know where i should start looking. If anyone has any ideas of where i can look.... or even if it can be done or not.....

 

Thanks 

 

Edd

Link to comment
https://forums.phpfreaks.com/topic/297125-do-i-need-arrays/
Share on other sites

  • 2 weeks later...

Here is my html form that posts to my submit.php


<table width="396" border="0">
<form action="submit.php" method="post">

   <tr>
    <td width="44"> </td>
    <td colspan="2"><center><h2>A Bit About You..</h2></center></td>
    <td width="58"> </td>
  </tr>
  <tr>
    <td> </td>
    <td width="107"><p>Forename: </p></td>
    <td width="159"><input type="text" name="firstname" required></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td>Surname:</td>
    <td><input type="text" name="secondname" required></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td colspan="2"><center><h2>How Do We Get In Touch?</h2></center></td>
    <td></td>
  </tr>
  <tr>
    <td> </td>
    <td>Email Address:</td>
    <td><input type="text" name="email" required></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td colspan="2"><p><center><h2>When Are You Visiting?</h2></center></p></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td colspan="2"> <center>   <select name="dayattending">
    <option value="Saturday 16th">Saturday 16th</option>
    <option value="Sunday 17th">Sunday 17th</option>
    </select></center></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td colspan="2"><center><h2>Who's Coming?</h2></center></td>
    <td> </td>
  </tr>
    <tr>
    <td> </td>
    <td colspan="2"><center>Early Bird Discount
    <br />
    Adult Ticket £2.50
    <br />
    Childrens Ticket (age 3-15) £0.50
     <br />
     Family Pass : £6.00
      <br /><br />
    </center></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td><center>Adult Ticket</center></td>
    <td><center>Childrens Ticket (age 3-15)</center></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td><center><input type="text" size = "2" name="adults" value="0"></center></td>
    <td><center><input type="text" size = "2" name="kids" value="0"></center></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td colspan="2"><center>Family Ticket (2 Adults & 2 Children)</center></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td colspan="2"><center><input type="text" size = "2" name="family" value="0"></center></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td colspan="2"><center><input type="submit"></center></td>
    <td> </td>
  </tr>
</table>

I am finding it really hard to get my head around the arrays as i have never used them before. If someone can give me an example i can try to build.

 

I want to input each ticket as a single post in a MYSQL table so they can have their own booking number, which i will convert into a barcode to the public can be scanner in to the venue...

Link to comment
https://forums.phpfreaks.com/topic/297125-do-i-need-arrays/#findComment-1516418
Share on other sites

You don't even need arrays, you just need to do some action * number of tickets. That is what a for loop is used for.

$numAdults = $_POST['adults'];
$numKids = $_POST['kids'];

if ($numAdults != 0) {
    for ($i = 0; $i < $numAdults; $i++) {
        // create an adult ticket
    }    
}

if ($numKids != 0) {
    for ($i = 0; $i < $numKids; $i++) {
        // create a kid ticket
    }       
}
Rather than running an INSERT query on each loop iteration, though, I'd recommend that you compile a list of values to insert and do it all at once with a batch insert. Manual here.
Link to comment
https://forums.phpfreaks.com/topic/297125-do-i-need-arrays/#findComment-1516419
Share on other sites

Yes thats correct..

I will look at tying all the tickets together with a transaction id number or the payment email address that will be the same for all the tickets bought in that transaction. Then when a customer is logged into the online system they can see all of their tickets....

Link to comment
https://forums.phpfreaks.com/topic/297125-do-i-need-arrays/#findComment-1516423
Share on other sites

So would the adults submit work something like this???

Obviously i will have to but a few lines in to connect to the DB but..

I have added a random number generator for now to make the transaction Number...

$randomgen = mt_rand() . "\n";
echo mt_rand(10);
$transaction_no = "$randomgen";
$numAdults = $_POST['adults'];
$numKids = $_POST['kids'];

if ($numAdults != 0) {
    for ($i = 0; $i < $numAdults; $i++) {
        // create an adult ticket
mysqli_query($connect,"INSERT INTO tickets (ticket_type, transaction_no)
VALUES ('$i','$transaction_no')");
    }    
}
Link to comment
https://forums.phpfreaks.com/topic/297125-do-i-need-arrays/#findComment-1516428
Share on other sites

 

So would the adults submit work something like this???

Obviously i will have to but a few lines in to connect to the DB but..

I have added a random number generator for now to make the transaction Number...

$randomgen = mt_rand() . "\n";
echo mt_rand(10);
$transaction_no = "$randomgen";
$numAdults = $_POST['adults'];
$numKids = $_POST['kids'];

if ($numAdults != 0) {
    for ($i = 0; $i < $numAdults; $i++) {
        // create an adult ticket
mysqli_query($connect,"INSERT INTO tickets (ticket_type, transaction_no)
VALUES ('$i','$transaction_no')");
    }    
}
Link to comment
https://forums.phpfreaks.com/topic/297125-do-i-need-arrays/#findComment-1516588
Share on other sites

mt_rand() is going to eventually produce duplicates. That is not a good way to do it. You'd probably want a "transactions" database table with the transaction number being the auto-incremented primary key. Of course, then the transaction numbers would be sequential, so if that is a problem you'll have to use something else.
Link to comment
https://forums.phpfreaks.com/topic/297125-do-i-need-arrays/#findComment-1516599
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.