Accessing variables on different pages

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James

    #1

    Accessing variables on different pages

    Hi, I am trying to build a shopping cart for my DVD website and am having
    trouble reading variable over different pages.

    I have a page that allows the user to add things to their cart and this page
    also display what they have selected. I also am trying to build a 'checkout'
    page that displays the DVDs they have chosen, the cost and allows them to
    remove a DVD from their cart.

    However, I can't even get the checkout page to display the list of DVDs that
    is displayed on the other page. Globals variables have been turned off on
    the server I am using.

    Any help with this would be great.

    Here is the code I was referring to:

    <?php

    // this section creates a session, connects to the db and handles the two
    buttons being pressed.
    session_start() ;
    session_registe r("trolley");

    require ('mysql.php');
    mysql_connect ($host, $user, $passwd);
    mysql_select_db ($dbName);

    if (isset ($_GET['add']))
    $_SESSION['trolley'][] = $_GET['add'];
    else if ($_GET['op'] === 'clear')
    $_SESSION['trolley'] = "";

    $_GET['getdvds_query'];
    $_GET['search_text'];
    $_GET['search_in'];
    $_GET['genrename'];
    ?>

    <html>
    <head>
    <title>PHP & MySQL</title>
    </head>

    <table>
    <?php

    if ($search_in == "title") {
    $getdvds_query = mysql_query ("SELECT dvdid, title, duration, rel, descr,
    genre, stock, price FROM dvd where title LIKE '%$search_text% '");
    }
    else if ($search_in == "director") {
    $getdvds_query = mysql_query ("select dvd.dvdid, dvd.title, dvd.duration,
    dvd.rel, dvd.descr, dvd.genre, dvd.stock, dvd.price from dvd, director where
    director.name like '%$search_text% '");
    }
    else if ($search_in == "actor") {
    $getdvds_query = mysql_query ("select dvd.dvdid, dvd.title, dvd.duration,
    dvd.rel, dvd.descr, dvd.genre, dvd.stock, dvd.price from dvd, actordvd,
    actor where actor.actorid = actordvd.actori d and actordvd.dvdid = dvd.dvdid
    and actor.name like '%$search_text% '");
    }

    else

    $getdvds_query = mysql_query ("SELECT dvdid, title, duration, rel, descr, ge
    nre, stock, price FROM dvd where genre LIKE '%$genrename%'" );

    if(!$getdvds_qu ery) {
    die("No result, error: ".mysql_error() );
    }

    echo "<table bgcolor=\"ddeef f\"><thead><tr> ";
    for ( $i = 1 ; $i < mysql_num_field s($getdvds_quer y) ; $i++ ) {
    echo "<th bgcolor=\"abcde f\">" .
    mysql_field_nam e($getdvds_quer y,$i) . "</th>\n";
    }

    if ( mysql_num_rows( $getdvds_query) > 0 )
    {
    while ( $row = mysql_fetch_ass oc($getdvds_que ry) )
    {
    $dvd[$row['dvdid']] = $row;
    echo "<tr>\n";
    echo "<td>{$row['title']}</td>\n";
    echo "<td>{$row['duration']}</td>\n";
    echo "<td>{$row['rel']}</td>\n";
    echo "<td>{$row['descr']}</td>\n";
    echo "<td>{$row['genre']}</td>\n";
    echo "<td>{$row['stock']}</td>\n";
    echo "<td>{$row['price']}</td>\n";

    echo "<td><a
    href=\"start.ph p?main=search&a dd={$row[dvdid]}\">add to basket</a></td>";
    echo "</tr>\n";
    }
    }

    ?>
    </table>
    <a href="start.php ?main=search&op =clear">empty trolley</a><br /><br />
    <a href="start.php ?main=checkout& trolley<?=SID?> ">Checkout</a><br /><br />

    <?php
    //this section outputs basket contents
    if (!empty ($_SESSION['trolley']))
    {
    echo 'Trolley contents:<br />';

    foreach ($_SESSION['trolley'] as $trolley_item)
    echo $dvd[$trolley_item]['title']."<br />";
    }
    else
    echo 'Trolley is empty!';
    ?>

    </body>
    </html>

    Here is my checkout page:

    <?php

    // this section creates a session, connects to the db and handles the two
    buttons being pressed.
    session_start() ;
    session_registe r("trolley");
    session_registe r("add");

    require ('mysql.php');
    mysql_connect ($host, $user, $passwd);
    mysql_select_db ($dbName);

    $_SESSION['trolley'];

    if (!empty ($_SESSION['trolley']))
    {

    echo 'Trolley contents:<br />';
    echo "<table bgcolor=\"ddeef f\"><thead><tr> ";

    foreach ($_SESSION['trolley'] as $trolley_item)
    echo "<tr>\n";
    echo $dvd[$trolley_item]['title']."<br />";
    echo $dvd[$trolley_item]['price']."<br />";
    echo "</tr>\n";
    }
    else
    {
    echo 'Trolley is empty!';
    }

    ?>




  • Pedro Graca

    #2
    Re: Accessing variables on different pages

    James wrote:
    (snip)[color=blue]
    > Any help with this would be great.
    >
    > Here is the code I was referring to:
    >
    ><?php[/color]

    // make PHP report the use of unintialized variables
    error_reporting (E_ALL);
    ini_set('displa y_errors', '1');
    [color=blue]
    >
    > // this section creates a session, connects to the db and handles the two
    > buttons being pressed.
    > session_start() ;
    > session_registe r("trolley");[/color]

    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    Caution: If your script uses session_registe r() it will not work in
    environments where the PHP directive *register_globa ls* is disabled.


    (snip)[color=blue]
    ></table>
    ><a href="start.php ?main=search&op =clear">empty trolley</a><br /><br />
    ><a href="start.php ?main=checkout& trolley<?=SID?> ">Checkout</a><br /><br />[/color]
    _______________ _______________ ___^^^^^^^^^^^^ ^^^^

    Something like "&trolleyfedcba 9876543210fedcb a9876543210" ?
    Are you perhaps missing a '=' after "trolley" ?

    (snip)


    You have quite a few statements without effect throughout your code:

    <?php

    $_GET['id']; // statement without effect
    // AFAICT this does absolutely nothing

    ?>
    --
    USENET would be a better place if everybody read: : mail address :
    http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
    http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
    http://www.expita.com/nomime.html : to 10K bytes :

    Comment

    • James

      #3
      Re: Accessing variables on different pages

      Pedro, thanks for your advice. It's a bit late now but I'll work on the code
      some more tomorrow. If anyone else has any more help I would be very
      grateful.


      Comment

      Working...