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

Member Avatar for iamthwee

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

I tried with post as well and it seems to be not working

Member Avatar for iamthwee

http://www.w3schools.com/PHP/php_forms.asp

Your example doesn't make sense. If you want to parse the variables out of a url string do.

<a href='somefile.php?test=1&test2=2'>click_me</a>

somefile.php

echo($_GET['test']);
echo($_GET['test2']);

do you know what this error means

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

Its fine. I have sorted the error out

Member Avatar for iamthwee

post your full code again

<?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

Member Avatar for iamthwee

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'>";

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

Member Avatar for iamthwee
echo '<input type="hidden" name="id" value="'.DVDID.'">';

try that instead

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

Member Avatar for iamthwee
echo '<input type="hidden" name="id" value="'.$mydvd.'">';

Sorry i meant.

Member Avatar for iamthwee

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

Sorry I meant to type that.

ok it is printing out the VALUES..OH MY GODDD .. THANK YOU SO MUCH...YOU ARE A LEGENDDDD.

the only other thing is that in the URL it is showing this
http://mi-linux.wlv.ac.uk/~1104107/basket.php?3Skyfall3

and it is printing out

: 1
: Gangster
: 10

OTHER THAN THAT THANK YOU SO MUCH. I HAVE BEEN STUCK ON THIS FOR SO LONG

Member Avatar for iamthwee

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.

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

Member Avatar for iamthwee
$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.

Ahh okk. Thank you anyways

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

Member Avatar for iamthwee

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

Thank You for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.