My 1st php script. What do you think?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vBR
    Senior Member
    • Apr 2002
    • 1768

    My 1st php script. What do you think?

    This is my first attempt at coding in php.
    The website I am writing for, http://www.balletrusse.com, previously accomplished most tasks with flat HTML files and much java script.
    It required a lot of editing time to add / move items and to generally keep it up-to-date.
    Most time consuming were the 'tour dates' and 'venue finder' systems.
    Thus I took the plunge and rewrote the entire thing in php with a db back end.

    You can see the scripts in action here:-
    Get this domain name before someone else does. Quick and painless shopping. Affordable payment options available.

    Get this domain name before someone else does. Quick and painless shopping. Affordable payment options available.

    Get this domain name before someone else does. Quick and painless shopping. Affordable payment options available.


    As this is all new to me I would appreciate any pointers you could give as to potential bugs in the code, optimization, etc.

    Thank you.


    Now for the scripts:

    'Now, Next, Then' script.
    Displays next two performances on home page
    Get this domain name before someone else does. Quick and painless shopping. Affordable payment options available.


    PHP Code:
    <?php

    // ##########################################################
    // #           Ballet Russe Tour Dates (Next) v1.0          #
    // # Written by: Darius Hopkins - [email protected] #
    // # Copyright: ©2003 Darius Hopkins.                       #
    // # Created for: Ballet Russe - http://www.balletrusse.com #
    // ##########################################################

    // ################################ start initialise ################################

    //link to required files
    require("config.php");

    //connect to database server
    $connection mysql_connect("$servername","$dbusername","$dbpassword") or die ("Cannot connect to server.");

    //select database
    $db mysql_select_db("$dbname"$connection) or die ("Could not select database.");

    // ################################ start query database ################################

    // create tour dates sql statement
    $sql "SELECT tour.startdate, tour.enddate, production.production, production.html, venue.venue, venue.addressc FROM (tour LEFT JOIN production USING (productionid)) LEFT JOIN venue ON tour.venueid = venue.venueid WHERE startdate >= UNIX_TIMESTAMP(CURRENT_DATE) OR enddate >= UNIX_TIMESTAMP(CURRENT_DATE) ORDER BY tour.startdate $order LIMIT 2";

    //execute tour dates sql query
    $sql_result mysql_query($sql$connection) or die ("Could not execute query.");

    // ################################ start generate html ################################

    // HTML lead in
    echo "<table><tr>";

    // ################################ start format output ################################

    $dospacer=1;
    $slot=0;
    while (
    $row mysql_fetch_array($sql_result)) {
      
      if (
    $row["enddate"]==""){
        
    $startdate date("D dS M Y",$row["startdate"]);
        
    $to "";
        
    $enddate "";
      }
      else {
        
    $to " ~ ";
        if (
    date("m",$row["startdate"])==date("m",$row["enddate"])){
          
    $startdate date("D dS",$row["startdate"]);
          
    $enddate date("D dS M Y",$row["enddate"]);
        }
        else {
          if (
    date("Y",$row["startdate"])==date("Y",$row["enddate"])){
            
    $startdate date("D dS M",$row["startdate"]);
            
    $enddate date("D dS M Y",$row["enddate"]);
          }
          else {
            
    $startdate date("D dS M Y",$row["startdate"]);
            
    $enddate date("D dS M Y",$row["enddate"]);
          }
        }
      }

      
    $venue $row["venue"];  
      
    $place $row["addressc"];
      if (
    $row["html"]==""){ 
        
    $production $row["production"];
      }
      else {
        
    $production $row["html"];
      }

      if (
    $slot==0){
        if (
    $row["startdate"]<=time()){
          
    $performing "Now Performing";
          
    $step=0;
        }
        else {
          
    $performing "Next Performance";
          
    $step=1;
        }
      }
      else {
        if (
    $step==0){
          
    $performing "Next";
        }
        else {
          
    $performing "Then";
        }
      }

    // HTML for tour dates
    echo "
    <td>
    <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
    <tr><td colspan=\"2\"><b>
    $performing:</b> $production</td></tr>
    <tr><td valign=\"top\">Where?&nbsp;</td><td>
    $venue$place</td></tr>
    <tr><td valign=\"top\">When?</td><td>
    $startdate$to$enddate</td></tr>
    </table>
    </td>
    "
    ;

    // add spacer
    if ($dospacer){
    echo
    "<td><p><img src=\"http://www.balletrusse.com/images/blank.gif\" width=\"10\" height=\"1\"></p></td>";
    }

    $dospacer=0;
    $slot=1;
    }

    // HTML lead out
    echo "
    </tr>
    <tr><td><br><a href=\"http://www.balletrusse.com/tour/tour.php\" target=\"_Blank\">View all tour dates</a></td></tr>
    </table>"
    ;

    // ################################ start clean up ################################

    //disconnect
    mysql_free_result($sql_result);
    mysql_close($connection);

    // Have a nice day :)

    ?>



    'Tour Dates' script
    Displays future or past tour dates.
    Get this domain name before someone else does. Quick and painless shopping. Affordable payment options available.


    PHP Code:
    <?php

    // ##########################################################
    // #           Ballet Russe Tour Dates Viewer v1.2          #
    // # Written by: Darius Hopkins - [email protected] #
    // # Copyright: ©2003 Darius Hopkins.                       #
    // # Created for: Ballet Russe - http://www.balletrusse.com #
    // ##########################################################

    // ################################ start initialise ################################

    //link to required files
    require("config.php");

    //connect to database server
    $connection mysql_connect("$servername","$dbusername","$dbpassword") or die ("Cannot connect to server.");

    //select database
    $db mysql_select_db("$dbname"$connection) or die ("Could not select database.");

    // define time
    if ($HTTP_POST_VARS['time']) {
        
    $time $HTTP_POST_VARS['time'];
    } else if (
    $HTTP_GET_VARS['time']) {
        
    $time $HTTP_GET_VARS['time'];
    }

    // if no time defined, use default time
    if (!isset($time) or $time=="") {
      
    $time="future";
    }

    // define print
    if ($HTTP_POST_VARS['print']) {
        
    $print $HTTP_POST_VARS['print'];
    } else if (
    $HTTP_GET_VARS['print']) {
        
    $print $HTTP_GET_VARS['print'];
    }

    if (
    $print){
      
    $class="class=\"printf\"";
      
    $toplink="";
    }
    else {
     
    $class="";
     
    $toplink="<tr><td align=\"right\">back to <a href=\"#top\">top <img src=\"http://www.balletrusse.com/images/up.gif\" border=\"0\"></a></td></tr>";
    }



    // ################################ start past tour dates ################################

    if ($time=="past") {

    $operator "<";
    $order "DESC";
    $swap "future";
    $title "Past Tour Dates";

    }

    // ################################ start future tour dates ################################

    else {

    $operator ">=";
    $order "ASC";
    $swap "past";
    $title "Tour Dates";

    }

    // ################################ start query database ################################

    // create tour dates sql statement
    $sql "SELECT tour.startdate, tour.enddate, tour.venueid, production.production, production.html, venue.venue, venue.addressc, venue.infinder FROM (tour LEFT JOIN production USING (productionid)) LEFT JOIN venue ON tour.venueid = venue.venueid WHERE startdate $operator UNIX_TIMESTAMP(CURRENT_DATE) OR enddate $operator UNIX_TIMESTAMP(CURRENT_DATE) ORDER BY tour.startdate $order";

    //execute tour dates sql query
    $sql_result mysql_query($sql$connection) or die ("Could not execute query.");

    if (!
    $print){
    // create navigation sql statement
      
    $sql2 "SELECT max(startdate) AS max, min(startdate) AS min FROM tour WHERE startdate $operator UNIX_TIMESTAMP(CURRENT_DATE) OR enddate $operator UNIX_TIMESTAMP(CURRENT_DATE)";

    //execute navigation sql query
      
    $sql_result2 mysql_query($sql2$connection) or die ("Could not execute query.");
    }

    // ################################ start generate html ################################

    // HTML lead in
    echo "
    <html>
    <head>
    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
    <title>Tour Dates</title>
    <LINK href=\"../css/br.css\" type=text/css rel=stylesheet>
    <script language=\"javascript\" src=\"../js/popup.js\"></script>
    <script language=\"javascript\" src=\"../js/printerfriendly.js\"></script>
    </head>
    <body 
    $class>
    <a name=\"top\">
    <center>
    <br><img src=\"../images/brheading.gif\"><br>
    <br>
    <font class=\"title\">
    $title</font><br>
    <br>
    "
    ;

    // ################################ start navigation ################################

    if (!$print){

      
    $dates=MYSQL_FETCH_ARRAY($sql_result2 ); 
      
    $yearmin date("Y",$dates["min"]);
      
    $yearmax date("Y",$dates["max"]);

      if (
    $order=="DESC"){
        echo 
    "<a href=\"#$yearmax\">$yearmax</a> ";
        for (
    $i=$yearmax-1;$i>=$yearmin;$i--) {
          echo 
    " | <a href=\"#$i\">$i</a> ";
        }
      }
      else{
        echo 
    "<a href=\"#$yearmin\">$yearmin</a> ";
        for (
    $i=$yearmin+1;$i<=$yearmax;$i++) {
          echo 
    " | <a href=\"#$i\">$i</a> ";
        }
      }

      echo 
    "
    <br>
    <br>
    View <a href=\"tour.php?time=
    $swap\">$swap tour dates</a>
    <br>
    <br>
    For further information about contacting or locating the venues at which we perform, try our <a href=\"venue.php\">venue finder</a>.<br>
    Click the '<img src=\"http://www.balletrusse.com/images/find.gif\">' icon next to a venue to go straight to that venue's contact details.<br>
    <br>
    View <a href=\"tour.php?time=
    $time&print=1\" target=\"_Blank\">printer-friendly</a> version of this page.<br>
    <br>
    <br>
      "
    ;
    }

    // ################################ start format output ################################

    $thismonth "";
    $thisyear "";
    while (
    $row mysql_fetch_array($sql_result)) {
      
    $lastmonth $thismonth;
      
    $thismonth date("F",$row["startdate"]);
      
    $lastyear $thisyear;
      
    $thisyear date("Y",$row["startdate"]);
      
    $startdate date("D dS",$row["startdate"]);
      if (
    $row["enddate"]==""){
        
    $to "";
        
    $enddate "";
      }
      else {
        
    $to " ~ ";
        
    $enddate date("D dS",$row["enddate"]);
      }
      
    $venue $row["venue"];  
      
    $place $row["addressc"]; 

      if (
    $row["html"]=="" || $print){ 
        
    $production $row["production"];
      }
      else {
        
    $production $row["html"];
      }

      
    $id $row["venueid"];
      
    $infinder $row["infinder"];
      if (
    $infinder && !$print){
        
    $find "&nbsp;<a href=\"venue.php?venue=$id\"><img src=\"http://www.balletrusse.com/images/find.gif\" border=\"0\"></a>";
      }
      else {
        
    $find "";
      }
      

    // add year headings
      
    if ($lastyear==""){
        echo 
    "<a name=\"$thisyear\"><table><tr><td><table border=\"1\"><tr><td align=\"center\"><font class=\"title\">Tour Dates $thisyear</font></td></tr><tr><td><table border=\"1\">";
      }
      else if (
    $thisyear!==$lastyear){
    // HTML for year headings
        
    echo "</table></td></tr></table></td></tr>$toplink</table><br><br><a name=\"$thisyear\"><table><tr><td><table border=\"1\"><tr><td align=\"center\"><font class=\"title\">Tour Dates $thisyear</font></td></tr><tr><td><table border=\"1\">";
      }

    // add month headings
      
    if ($thismonth!==$lastmonth){
    // HTML for month headings
        
    echo "<tr><td colspan=\"3\"><big><b>$thismonth</b></big></td></tr>";
      }

    // HTML for tour dates
    echo "<tr><td valign=\"top\">$startdate$to$enddate</td><td valign=\"top\">$venue$place$find</td><td valign=\"top\">$production</td></tr>";

    }

    // HTML lead out
    echo "</table></td></tr></table></td></tr>$toplink</table><br><hr width=\"85%\"><hr width=\"95%\"><hr width=\"85%\"></center></body></html>";

    // ################################ start clean up ################################

    //disconnect
    mysql_free_result($sql_result);
    if (!
    $print){
      
    mysql_free_result($sql_result2);
    }
    mysql_close($connection);

    // Have a nice day :)

    ?>



    'Venue Finder' script.
    Displays, contact details of venues.
    Get this domain name before someone else does. Quick and painless shopping. Affordable payment options available.


    PHP Code:
    <?php

    // ##########################################################
    // #             Ballet Russe Venue Finder v1.0             #
    // # Written by: Darius Hopkins - [email protected] #
    // # Copyright: ©2003 Darius Hopkins.                       #
    // # Created for: Ballet Russe - http://www.balletrusse.com #
    // ##########################################################

    // ################################ start initialise ################################

    //link to required files
    require("config.php");

    //connect to database server
    $connection mysql_connect("$servername","$dbusername","$dbpassword") or die ("Cannot connect to server.");

    //select database
    $db mysql_select_db("$dbname"$connection) or die ("Could not select database.");

    // select venue to find
    if ($HTTP_POST_VARS['venue']) {
        
    $select $HTTP_POST_VARS['venue'];
    } else if (
    $HTTP_GET_VARS['venue']) {
        
    $select $HTTP_GET_VARS['venue'];
    }

    // ################################ start query database ################################

    // create venue list sql statement
    $sql "SELECT venueid, venue, addressc FROM venue Where infinder = 1 ORDER BY addressc, venue ASC";

    // execute venue list sql query
    $sql_result mysql_query($sql$connection) or die ("Could not execute query.");

    if (
    $select){
    // create venue details sql statement
      
    $sql2 "SELECT venue, addressa, addressb, addressc, addressd, postcode, botelno, url FROM venue WHERE venueid = $select";

    // execute venue details sql query
      
    $sql_result2 mysql_query($sql2$connection) or die ("Could not execute query.");

    }

    // ################################ start generate html ################################

    // HTML lead in
    echo "
    <html>
    <head>
    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
    <title>Venue Finder</title>
    <LINK href=\"../css/br.css\" type=text/css rel=stylesheet>
    </head>
    <body>
    <center>
    <br><img src=\"../images/brheading.gif\"><br>
    <br>
    <font class=\"title\">Venue Finder</font><br>
    <br>
    <table>
    <tr><td rowspan=\"2\" valign=\"top\"><img src=\"../images/point1.gif\"></td>
    <td align=\"center\">
    Select your chosen venue from the list below.
    <form name=\"vform\" action=\"venue.php\">
    <select name=\"venue\" onChange=\"javascript: if(this.form.venue.value!==''){this.form.submit()}\">
    <option value =\"\">Choose a venue
    <option value =\"\">---------------------------
    "
    ;

    // ################################ start format output ################################

    $thisltr "";
    while (
    $row mysql_fetch_array($sql_result)) {
      
    $lastltr $thisltr;
      
    $thisltr strtoupper($row["addressc"]{0});
      
    $id $row["venueid"];
      
    $name $row["venue"];
      
    $place $row["addressc"];

    // add letter headings
       
    if ($thisltr!==$lastltr){
    // HTML for letter headings
       
    echo "<option value =\"\"><option value =\"\">$thisltr:<option value =\"\">----";
       }

       if (
    $id==$venue){
         
    $selected "SELECTED";
       }
       else {
         
    $selected "";
       } 

    // HTML for venues
    echo "<option value =\"$id\" $selected>$place$name";

    }

    // continue HTML
    echo "
    </select>
    </form>
    </td>
    </tr>
    <tr><td><hr></td></tr>
    <tr>
    <td rowspan=\"2\" valign=\"top\"><img src=\"../images/point2.gif\"></td>
    <td align=\"center\">
    The venue's contact details are:-<br><br>
     <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"bbbbff\">
     <tr>
     <td><img src=\"../images/tl2.gif\"></td>
     <td><img src=\"../images/blank.gif\"></td>
     <td><img src=\"../images/tr2.gif\"></td>
     </tr>
     <tr>
     <td><img src=\"../images/blank.gif\"></td>
     <td align=\"center\">"
    ;

    // ################################ start venue details ################################

    if ($select){
      
    $row mysql_fetch_array($sql_result2);
      
    $name $row["venue"];
      
    $addressa $row["addressa"];
      
    $addressb $row["addressb"];
      
    $addressc $row["addressc"];
      
    $addressd $row["addressd"];
      
    $pc $row["postcode"];
      
    $bo $row["botelno"];
      
    $url $row["url"];

      echo 
    "
    <table>
    <tr>
    <td align=\"right\" valign=\"top\">Venue:</td>
    <td><b>
    $name</b></td>
    </tr>
    <tr>
    <td align=\"right\" valign=\"top\">Address:</td>
    <td>
      "
    ;

      if (
    $addressa){
        echo 
    "<b>$addressa</b><br>";
      }
      if (
    $addressb){
        echo 
    "<b>$addressb</b><br>";
      }
      if (
    $addressc){
        echo 
    "<b>$addressc</b><br>";
      }
      if (
    $addressd){
        echo 
    "<b>$addressd</b><br>";
      }
      if (
    $pc){
        echo 
    "<b>$pc</b><br>";
      }

     echo 
    "
    </td>
    </tr>
      "
    ;

      if (
    $bo){
        echo 
    "
    <tr>
    <td align=\"right\" valign=\"top\">Box Office:</td>
    <td><b>
    $bo</b></td>
    </tr>
        "
    ;
      }

      if (
    $url){
        echo 
    "
    <tr>
    <td align=\"right\" valign=\"top\">www:</td>
    <td><b><a href=\"
    $url\" target=\"_Blank\">$url</b></td>
    </tr>
        "
    ;
      }

      echo 
    "
    </table>
      "
    ;

    }
    else {
      echo 
    "{No Venue Selected}";
    }


    echo
    " </td>
     <td><img src=\"../images/blank.gif\"></td>
     </tr>
     <tr>
     <td><img src=\"../images/bl2.gif\"></td>
     <td><img src=\"../images/blank.gif\"></td>
     <td><img src=\"../images/br2.gif\"></td>
     </tr>
     </table>
    </td>
    </tr>
    <tr><td><hr></td></tr>
    <tr>
    <td valign=\"top\"><img src=\"../images/point3.gif\"></td>
    <td align=\"center\">Need help finding the venue?<br><br>
    "
    ;

    // ################################ start map url ################################

    if ($select){

      if (
    $pc){
        
    $mpc str_replace("-",""$pc);
        
    $title urlencode("$name$addressc"); 
        echo 
    "<a href=\"http://www.multimap.com/p/browse.cgi?pc=$mpc&title=$title\" target=\"multimap\">";
      }
      else {
        echo
    "<a href=\"javascript:alert('We are sorry.\\n\\nWe are currently unable to bring you the street map for this venue')\">";
      }

    }
    else {
      echo 
    "<a href=\"javascript:alert('Venue not valid\\nor\\nNo venue selected')\">";
    }
    echo
    "
    View local street map</b></a> (Supplied by Multi-Map)</td></tr>
    </table>
    <br>
    <br>
    <hr width=\"85%\"><hr width=\"95%\"><hr width=\"85%\">
    </center>
    </body>
    </html>"
    ;

    // ################################ start clean up ################################

    //disconnect
    mysql_free_result($sql_result);
    if (
    $select){
      
    mysql_free_result($sql_result2);
    }
    mysql_close($connection);

    // Have a nice day :)

    ?>
    Last edited by vBR; Thu 13 Mar '03, 6:05am.
  • Anthony White
    New Member
    • Mar 2003
    • 9

    #2
    That looks very professional. I even like that little bit on the end. Great job.

    Comment

    • Beorn
      Senior Member
      • May 2002
      • 451

      #3
      You may have an interesting effect if the user enters time as there_is_no_time_like_the_present
      Change:
      PHP Code:
      if ($time=="future" ) { 
      $operator ">="
      $order "ASC"
      $swap "past"
      $title "Tour Dates"

      // ################################ start past tour dates ################################ 
      if ($time=="past" ) { 
      $operator "<"
      $order "DESC"
      $swap "future"
      $title "Past Tour Dates"

      to:
      PHP Code:
      if ($time=="future" ) { 
      $operator ">="
      $order "ASC"
      $swap "past"
      $title "Tour Dates"
      } else { 
      $operator "<"
      $order "DESC"
      $swap "future"
      $title "Past Tour Dates"

      Comment

      • vBR
        Senior Member
        • Apr 2002
        • 1768

        #4
        Originally posted by Beorn
        You may have an interesting effect if the user enters time as there_is_no_time_like_the_present
        Thank you.
        I had not thought of that.
        It is intended for the site to pass it URLs not for visitors to enter them.
        However, I can see this happening.
        The change you suggested would not work as 'future' should be the default.

        This is what I have used.
        PHP Code:
        // ################################ start past tour dates ################################

        if ($time=="past") {

        $operator "<";
        $order "DESC";
        $swap "future";
        $title "Past Tour Dates";

        }

        // ################################ start future tour dates ################################

        else {

        $operator ">=";
        $order "ASC";
        $swap "past";
        $title "Tour Dates";


        Comment

        widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
        Working...