0

Hello,

I am trying to the print the variables from the URL. I have tried some methods but it is working. There are variables in the URL. it is just I am not able to print them out.

<?php 

    if(isset($_SESSION['cart'])){
    echo '<ol>';
    foreach ($_GET as $name => $value)
    {

         echo $key . " : " . $value . "<br />\r\n";
    }
    echo '</ol>';

}else{
    echo 'Your Cart is empty';

}
    ?>

This is where I am trying to print them out

Below is the code from where the products are retrieved from the database and the variables are added to the URL

<?php
     require "connect.php";
     $query = "SELECT `DVDID`, `NameOfTheDVD`, `Quantity` FROM `DVD`";
     $stmt = $dbhandle->prepare($query);
     $stmt->execute();

     $num = $stmt->rowCount();

     if($num>0){
      while ($row = $stmt->fetch(PDO::FETCH_OBJ)){
       if(!isset($_SESSION['cart'])){
        echo "<table border='3' cellpadding='10' position='relative` bottom= '450px';>";//start table
        echo '<tr>';
        echo '<div class="DVD ID">'; 
        echo 'DVD Id : '.$row->DVDID.'<br>';
        $mydvd = $row->DVDID;
        $name = $row->NameOfTheDVD;


        echo 'Name Of the DVD : '.$row->NameOfTheDVD.'<br>';
        echo 'Quantity :  '.$row->Quantity.' ';
        echo '<form method="post" action="basket.php?'.$mydvd . $name. '">';
        echo '<input type="hidden" name="id" value="DVDID">';
        echo '<input type="hidden" name="item" value="NameOfTheDVD">';
        echo '<input type="hidden" name="Quantity" value="Quantity">';
        echo '<input type="submit" value="Add To Basket">';
        echo '</tr>';
        }

        }

        echo "</table>";
        }



        // no products in the database
        else{
          echo "No products found.";
        }



     ?>

ANY HELP WOULD BE APPRECIATED. THANK YOU

2
Contributors
22
Replies
33
Views
3 Years
Discussion Span
Last Post by Mohamed_26
Featured Replies
  • 1

    Replace the values with your php variables: eg `echo '<input type="hidden" name="id" value="DVDID">';` would become `echo "<input type='hidden' name='id' value='$mydvd'>";` Read More

  • 1

    echo '<input type="hidden" name="id" value="'.$mydvd.'">'; Sorry i meant. Read More

  • 1

    echo '<input type="hidden" name="id" value="'.$mydvd.'">'; Sorry I meant to type that. Read More

  • 1

    Passing the value into a urlstring is now redundant as you are using POST `echo '<form method="post" action="basket.php?'.$mydvd . $name. $Quantity. '">';` would become echo '<form method="post" action="basket.php">'; and it should still work. Read More

  • 1

    $query = "SELECT `DVDID`, `NameOfTheDVD`, `Quantity` FROM `DVD`"; you would need to add a 'where' clause to that query. eg select stuff where id='3' Sorry I got to go now. Read More

0

Your form method is using post not get? And you need to separate you get vars with the & symbol.

Edited by iamthwee

0

do you know what this error means

Notice: Undefined variable: key in /home/stud/0/1104107/public_html/basket.php on line 46 : Quantity

0
<?php
     require "connect.php";
     $query = "SELECT `DVDID`, `NameOfTheDVD`, `Quantity` FROM `DVD`";
     $stmt = $dbhandle->prepare($query);
     $stmt->execute();

     $num = $stmt->rowCount();

     if($num>0){
      while ($row = $stmt->fetch(PDO::FETCH_OBJ)){
       if(!isset($_SESSION['cart'])){
        echo "<table border='3' cellpadding='10' position='relative` bottom= '450px';>";//start table
        echo '<tr>';
        echo '<div class="DVD ID">'; 
        echo 'DVD Id : '.$row->DVDID.'<br>';
        $mydvd = $row->DVDID;
        $name = $row->NameOfTheDVD;
        $Quantity = $row -> Quantity;


        echo 'Name Of the DVD : '.$row->NameOfTheDVD.'<br>';
        echo 'Quantity :  '.$row->Quantity.' ';
        echo '<form method="post" action="basket.php?'.$mydvd .  $name. $Quantity. '">';
        echo '<input type="hidden" name="id" value="DVDID">';
        echo '<input type="hidden" name="item" value="NameOfTheDVD">';
        echo '<input type="hidden" name="Quantity" value="Quantity">';
        echo '<input type="submit" value="Add To Basket">';
        echo '</tr>';
        }

        }

        echo "</table>";
        }



        // no products in the database
        else{
          echo "No products found.";
        }



     ?>

This is where the products are retreived from the database and added to the cart

<?php 


    echo '<ol>';
    foreach ($_POST as $mydvd => $value)
    {

         echo   " : " . $value  ."<br />\r\n";
    }
    echo '</ol>';


    ?>

and this is where it prints them out. By the way, I am getting quite close now. Its just prints out
: DVDID
: NameOfTheDVD
: Quantity

1

Replace the values with your php variables:

eg
echo '<input type="hidden" name="id" value="DVDID">';

would become

echo "<input type='hidden' name='id' value='$mydvd'>";

0

I have done that. Now it is just printing this
: $mydvd
: $name
: $Quantity

0

these are the rrors i am getting Use of undefined constant DVDID - assumed 'DVDID'for the other 2 variables as well

1

Passing the value into a urlstring is now redundant as you are using POST

echo '<form method="post" action="basket.php?'.$mydvd . $name. $Quantity. '">';

would become

echo '<form method="post" action="basket.php">';

and it should still work.

Edited by iamthwee

0

Ahh okk. Thank you for that and last final thing is no matter which product I click, it still displays

: 1
: Gangster
: 10

BUT THANK YOU AGAINN. SERIOUSLY, YOU ARE AN ABSOLUTE LEGEND

1
$query = "SELECT `DVDID`, `NameOfTheDVD`, `Quantity` FROM `DVD`";

you would need to add a 'where' clause to that query.

eg select stuff where id='3'

Sorry I got to go now.

0

When I do the where clause saying DVDID = 3 , it only displays one of the products. Reply, when you are free. Its no rush

1

You're designing a cart. So...

1. Query your db to get your products
2. Build a template called item-detail.php and populate it as such
   (you may need drop downs... save these user choices using form = post.
3. When your process this in your add-to-cart.php make sure you are sending
   the product id as well as the choices.
4. You're done.

Hopefully, you can implement the logic

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.