How can get a variable from another page?

Hi
how my page (header.php) can get a variable from the page index.php, and dispaly the variable ?
can anyone help me ?

the variable $qty cannot dusplay here:
header.php

<table width="900" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr> <div id="tete">
    <td width="644" height="80" bgcolor="#E0F93B" class="tete">mushroom <br><td>

    <td width="84" bgcolor="#E0F93B"><img src="photos/panier1.JPG" width="106" height="116" /></td>
    <td width="172" bgcolor="#E0F93B">Counter:<?php echo "$qty=qty";?></br>
      </td>
    </div>
  </tr>
</table>

index.php

<?php

       $qty=0;
       $totalprice=0;
       foreach($listproduit as $produit) {
	   $subqty=$produit['quantity'];
       $subtotal=$produit['quantity']*$produit['price'];
       $totalprice+=$subtotal;
	   $qty+=$subqty;
	
?> 

I want to make output $qty in the page header.php

There are several ways:

You need to PASS the information to the script in the other page somehow. This can be done in several ways:

  1. You can use the URL. myDomain.com/mypage.php?variable=value&anothervar=50
    $_GET[‘variable’] will now equal ‘value’
    You can use this with forms, or just straight URL. That’s the easiest way but it’s also the least secure, for obvious reasons

  2. If you are submitting you can use the POST method ( you can use hidden INPUTs …<input name=‘someX’ value=‘value’ type=‘hidden’> to pass the data , and read them with $_POST[‘someX’] ; similar to what I showed above with $_GET

  3. You could also use a session. That is the most versatile and secure way to pass info… but it also takes some setting up:

header.php
<?php
session_start();
// store session data
$_SESSION[$qty]=1;
?>

index.php
<?php
session_start();
// read session data
$qty=$_SESSION[‘views’] ;
?>

btw, i am thinking that header.php and index. are SEPARATE documents, and you aren’t using include() to put one script inside the other.

hope that hepls

thank you so much

I prefer the third way, my script is:
<body>
<?php require_once(“header.php”)
.
.
.
</body>
so, I cannot use the third way ?
and $qty=$_SESSION[‘views’] ;
where is the “views” from ?
for the header.php
can I put
$_SESSION[$qty]=0;
because my counter begin from 0,

If a session is set up you can use it so long as you have “session_start()” somewhere in the page of your PHP code.

Using your code from the first post:

index.php:


[B]       session_start();[/B]
       $qty=0; 
       $totalprice=0; 
       foreach($listproduit as $produit) {   
            $subqty=$produit['quantity']; 
            $subtotal=$produit['quantity']*$produit['price']; 
            $totalprice+=$subtotal; 
            $qty+=$subqty;
       }
[B]       $_SESSION["qty"] = $qty;[/B]

That’s where I’d put the session (the bits in bold are my changes). Then in your other page, you can do:

Somewhere in your header.php:


     session_start();
     echo $_SESSION["qty"];

…to display the session variable. Note that if you have user-submitted content (such as a message in a post) in sessions it’s best to validate it first.

@sweeldesign
I did as you said, but the output is an error: in the line 5, syntax error, unexpected ‘<’ in C:\wampserver32\www\sale\head.php on line 5
which mean’s: <table width=“900” border=“0” align=“center” cellpadding=“0” cellspacing=“0”>
I d’ont understand why it’s an error;

the header script:
header.php
<?php
session_start();
<table width=“900” border=“0” align=“center” cellpadding=“0” cellspacing=“0”>
<tr> <div id=“tete”>
<td width=“644” height=“80” bgcolor=“#E0F93B” class=“tete”>mushroom <br>
<span class=“petit”>(Morille, Cèpe…)</span></td>
<td width=“84” bgcolor=“#E0F93B”><img src=“photos/panier1.JPG” width=“106” height=“116” /></td>
<td width=“172” bgcolor=“#E0F93B”>Counter: echo $_SESSION[“qty”];</br> </td>
</div>
</tr>
</table>
?>

If you use include it’s the SAME as if you had that script as part of the other one, so there is no NEED to pass the value ( no need for GET/POST or SESSION)

My GUESS is that you are including the wrong files and in the wrong order.

THE WAY you have explained it here you will ABSOLUTELY NEED to include INDEX.php at the beginning of HEADER.php for it to work.

It will not be possible otherwise…

so… header.php needs to begin with:
require_once(“index.php”) ;
where your variables will be defined… then the rest of header.php can use then for there

hoep that makes sense

it is my index.php, but the output is an error: in the line 5, syntax error, unexpected ‘<’ in C:\wampserver32\www\sale\head.php on line 5
which mean’s: <table width=“900” border=“0” align=“center” cellpadding=“0” cellspacing=“0”>

<?php require_once(“header.php”) ?>
<body>
<table width=“900” border=“0” cellspacing=“0” cellpadding=“0” align=“center” bgcolor=“#C5DF0F”>
<tr>
<td>
<?php
session_start();
require_once ‘panier.php’;
$panier = new Panier(‘produits’);
$listproduit = $panier->getPanier();
?>
<?php if(!$listproduit){?>
<p> votre panier est vide </p>
<?php } else {?>
<table border=“0” width=“50%” align=“center” bgcolor=“#C5DF0F”>
<tr>
<th bgcolor=“#ABBC37"height=“30” >Product</th>
<th bgcolor=”#ABBC37"height=“30” >Price</th>
<th bgcolor=“#ABBC37"height=“30” >Quantity</th>
<th bgcolor=”#ABBC37"height=“30” >Amount</th>
<th bgcolor="#ABBC37"height=“30” >Cancel</th>
</tr>
<?php
$qty=0;
$totalprice=0;
foreach($listproduit as $produit) {
$subqty=$produit[‘quantity’];
$subtotal=$produit[‘quantity’]*$produit[‘price’];
$totalprice+=$subtotal;
$qty+=$subqty;
$_SESSION[“qty”] = $qty;
?>

&lt;tr&gt;
   &lt;td&gt;&lt;?php print $produit['name'] ?&gt;&lt;/td&gt;
   &lt;td&gt;&lt;?php print $produit['price'] ?&gt;&lt;/td&gt;
   &lt;td&gt;&lt;?php print $produit['quantity'] ?&gt;&lt;/td&gt;	  
    &lt;td&gt;&lt;?php print($produit['quantity']*$produit['price'])?&gt;&#8364; 
    &lt;td&gt; &lt;a href="cancel.php?name=&lt;?php print  $produit['name'] ?&gt;"&gt;cancel&lt;/a&gt;&lt;/td&gt;	

&lt;/tr&gt;	

&lt;?php } ?&gt;
&lt;tr&gt;
    &lt;td bgcolor="#C5DF0F"&gt; &lt;?php echo "Total: $totalprice";?&gt;&#8364; TTC&lt;/br&gt;&lt;/td&gt;        
   &lt;/tr&gt;         

</table>

&lt;?php echo "Item quantity: $qty"; ?&gt;

</table>
<?php } ?>
<table width=“900” height=“30” border=“0” align=“center” cellpadding=“0” cellspacing=“0” >
<tr>
<th><a href=“index.php”>Continuer vos achats</a> </th><th><a href=“formulaire.php”>Commander</a></th>
</tr>
</table>

&lt;/td&gt;

</tr>
</table>

</body>

it is my header.php

<?php
session_start();
<table width=“900” border=“0” align=“center” cellpadding=“0” cellspacing=“0”>
<tr> <div id=“tete”>
<td width=“644” height=“80” bgcolor=“#E0F93B” class=“tete”>mushroom <br>
<span class=“petit”>(Morille, Cèpe…)</span></td>
<td width=“84” bgcolor=“#E0F93B”><img src=“photos/panier1.JPG” width=“106” height=“116” /></td>
<td width=“172” bgcolor=“#E0F93B”>Panier a: echo $_SESSION[“qty”];</br> </td>
</div>
</tr>
</table>
?>