SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help with code structure

    Hi,

    I need someone to help with some code. I am new to PHP and I need something really fast here for a project I am working on.

    Its simple code but I cant get it working.

    What I need is code for the following:
    A while loop that runs 200 times, I want it to have an if-else statement inside it. This if-else will check a database entry to see if it is equeal to 1 or zero.

    if it is equal to 1 it will return the text "booked", if it is equal to 0 it will return the text "available"

    the database is called "cinema" and the table is called "booking" with 2 fields: ID (which is an auto_increment primary key) and "status"

    The loop wil be checking the value in "status" to see if it is equal to 1 or 0.

    Also, I want the loop to run 200 times and with each iteration it the control variable goes up 1. The loop control also will refer to the primary key ID. So, the loop will run through checking every table row up through each ID and the corresponding 'status' value.

    Also, I need the loop to go to a new line after every 20th iteration with a <p> tag or something

    I would appreciate it if someone could provide me with the code for this.

    Thanks a lot!
    Last edited by mcrumlish; Mar 29, 2002 at 10:48.

  2. #2
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    // connect things here
    mysql_select_db("cinema") or die(mysql_error());
    $query mysql_query("SELECT id, status FROM booking") or die(mysql_error());
    $i 0;

    while (
    $data mysql_fetch_array($query))
    {
      
    extract($data);

      if (
    $status == 0)
        echo 
    "$id is available";

      else
        echo 
    "$id is booked";

      if (
    $i 20 == 0
        echo 
    "<p>";
      
      
    $i++;

    This code will not run 200 times instead it will run the numbers of rows returned by the mysql query.

    Note: untested

  3. #3
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply.
    That code works great

    Can u help me with one other thing, can you tell me a way to fill the table with 200 entries for ID and status without me manually doing it in phpmyAdmin.
    Thanks

  4. #4
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi!

    This will create a a textfile with 200 lines and a random value of either 0 or 1 representing the booked status:

    PHP Code:
    echo "<pre>";
    $fp fopen("./name.txt""w"); // name and path here
    for ($i 0$i 200$i++)
    {
        
    srand((double)microtime()*1000000);
        
    $j=rand(0,1);
        
    fwrite($fp"$i;$j\r\n");
        echo 
    "$i;$j\r\n";
    }
    fclose($fp);
    echo 
    "</pre>"
    Then you could after establishing a database connection use this to load the data into the table:

    PHP Code:
    mysql_select_db("cinema);
    mysql_query("
    LOAD DATA LOCAL INFILE
    'D:\\Internet\\Ide\\PHP\\file.txt'
    INTO TABLE `bookingFIELDS TERMINATED BY ';' LINES TERMINATED BY '\r\n'") or die(mysql_error()); 
    You must edit the line 'D:\\Internet\\Ide\\PHP\\file.txt' to reflect your paths.
    On Windows it's double backslash "\\" and on Linux / Unix it should be "/".

    Or if you have phpmyadmin installed you could open this and use the option to insert a text file into an existing table.

  5. #5
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    thanks again

    Thanks Chris, that worked great.

    Now I have another question

    I have everything working ok, the table is filled and the loop is running. I have changed the loop to produce a HTML table with a table cell with either text saying "booked" or if it is available then a table cell with a link "book seat"

    How can I make it so each link goes to a page with a booking form called book.php and passes the ID value of the seat (i.e. $i in the loop)

    I also want this then to be submitted in a form on the next page which has fields for name, e-mail , address and credit card. I need the form to, once submitted, change the "status" value of the seat from "1" to "0".

    Thanks again fo your help,
    Martin

  6. #6
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the while loop use:

    PHP Code:
    if ($status == 0
    {
       echo 
    "<a href='book.php?id=$id'>book seat</a>";

    And on the page where the form is (book.php)

    PHP Code:
    <?php
    if (!isset($HTTP_POST_VARS['submit']))
    {
    ?>

    <form method="POST" action="<?php echo "$PHP_SELF?id=".$HTTP_GET_VARS['id']; ?>">
    your name: <input type="text" name="name"><br>
    your email: <input type="text" name="email"><br>
    your creditcardno: <input type="text" name="credit"><br>
    <input type="submit" name="submit">
    </form>

    <?php }

    if (isset(
    $HTTP_POST_VARS['submit']))
    {
       
    $bid = (int)$HTTP_GET_VARS['id'];

       
    mysql_connect("...");
       
    mysql_select_db("cinema");
       
    $query mysql_query("SELECT * FROM booking WHERE id = $bid");

       
    // some simple checks
       
    if (mysql_num_rows($query) == 0)
          die(
    "The id is not available");

       
    mysql_query("UPDATE booking SET booked = 1 WHERE id = $bid");

       
    $email $HTTP_POST_VARS['email'];
       
    $name $HTTP_POST_VARS['name'];
       
    $credit $HTTP_POST_VARS['credit'];

       
    mail("youremail""Changed booked status for $bid by $name <$email>""changed status");

       
    mail($email"Your data has been processed""Text here");
    ?>
    Note: Untested
    Last edited by Chris82; Mar 30, 2002 at 06:37.

  7. #7
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Chris,

    I added that code and on the booked page i got this error:

    Parse error: parse error in c:\program files\nusphere\apache\nsdocs\cinema\book.php on line 35

    Any ideas?

  8. #8
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I found the problem. it was just missing 2 closing }

    However, now when i click submit I get a page not found.
    Here is the (slightly changed) code

    <html>
    <body>
    <?php
    if (!isset($HTTP_POST_VARS['submit']))
    {
    ?>

    <form method="POST" action="$PHP_SELF?id=<?php echo $HTTP_GET_VARS['id']; ?>">
    your name: <input type="text" name="name"><br>
    your email: <input type="text" name="email"><br>
    your creditcardno: <input type="text" name="credit"><br>
    <input type="submit" name="submit">
    </form>

    <?php if (isset($HTTP_POST_VARS['submit']))
    {
    $bid = (int)$HTTP_GET_VARS['id'];

    mysql_connect("localhost", "root", "");
    mysql_select_db("cine");
    $query = mysql_query("SELECT * FROM booking WHERE id = $bid");

    // some simple checks
    if (mysql_num_rows($query) == 0)
    die("The id is not available");

    mysql_query("UPDATE booking SET booked = 1 WHERE id = $bid");

    $email = $HTTP_POST_VARS['email'];
    $name = $HTTP_POST_VARS['name'];
    $credit = $HTTP_POST_VARS['credit'];

    mail("martin@evolutionnewmedia.com", "Changed booked status for $bid by $name <$email>", "changed status");
    mail($email, "Your data has been processed", "Text here");
    }
    }
    ?>
    </body>
    </html>

    and here is what appears in the URL field in IE when it after I submit the form and the 404 error appears:
    http://localhost:9000/cinema/$PHP_SELF?id=4

    (I clicked seat 4)

    Any ideas?

    //Martin

  9. #9
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Never mind,

    I worked it out for myself.

    Thanks,
    //Martin

  10. #10
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I have another problem

    I have the page working if each booking is done with a link, however. I want to have multiple bookings from one page. I want to do this with checkboxes but I am not sure how.

    here is the existing code that i have. there 3 php pages.

    ---------
    Page 1 - booking page

    <html>
    <head>
    <style>
    td(font-family:verdana; font-size:10px)
    a{color:white; text-decoartion:none; font-family:verdana}
    a.hover{colorrange; text-decoration:underline}
    </style>
    </head>

    <body>
    <form method="POST" action="book1.php?id=$id">
    <table width="760" align="center" border="1" cellpadding="10">
    <tr>
    <td colspan="20">Screen 1</td>
    </tr>
    <tr>
    <?php
    // connect things here
    mysql_connect("localhost", "root", "");
    mysql_select_db("cine") or die(mysql_error());
    $query = mysql_query("SELECT id, status FROM screen1") or die(mysql_error());
    $i = 0;

    while ($data = mysql_fetch_array($query))
    {
    extract($data);

    if ($status == 0)
    echo "<td bgcolor='white' width='20'><input type='checkbox' name='checkbox' value='id'></td>";

    else
    echo "<td bgcolor='orange' width='20'><font size='1' face='verdana'><a href='cancel1.php?id=$id'>cancel</a></font></td>";

    $i++;

    if ($i % 20 == 0)
    echo "</tr><tr>";


    }
    // echo "$id is available";
    ?>
    <tr>
    <td colspan="20" align="right">
    <input type="submit" name="submit">
    </td>
    </tr>
    </table>
    </form>
    </body>
    </html>

    -------------
    screen 2 - submit info to database (confirm)
    -------------
    <html>
    <body>

    <form method="POST" action="seat1.php?id=<?php echo $HTTP_GET_VARS['id']; ?>">
    1
    <input type="submit" name="submit">
    </form>

    </body>
    </html>
    -------------
    screen 3 - inserts info and has a link back to the start
    -------------

    <?php
    $bid = (int)$HTTP_GET_VARS['id'];
    mysql_connect("localhost", "root", "");
    mysql_select_db("cine");
    $query = mysql_query("SELECT * FROM screen1 WHERE id = $bid");

    // some simple checks
    if (mysql_num_rows($query) == 0)
    die("The id is not available");

    mysql_query("UPDATE screen1 SET status = 1 WHERE id = $bid");
    echo "updated - <a href='main.htm'>Back</a>";

    ?>
    -----------------

    Any ideas?

    //Martin


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •