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
Share on other sites

The reason behind the seperate rows is that, i would like to have a reference number for each ticket which will be converted into a QR code that can be scanned at the entrance upon arrival.

 

Edd

Edited by eddcaton
Link to comment
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
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. Edited by scootstah
Link to comment
Share on other sites

1 - you are missing a closing form tag here.

2 - you want to post each single ticket with a separate booking number. That means a customer with 3 tickets has 3 booking numbers? Really? How will you tie the tickets to the purchaser?

Link to comment
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
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')");
    }    
}
Edited by EddNicks
Link to comment
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
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
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.