Page 1 of 1

how to pass parameters when using the include?

Posted: Sat Oct 01, 2011 2:14 am
by GiaTuan
Hi.I'm from Vietnam.I am a new member.Currently, I'm doing a php page book sales.It uses include.
my php pages:
center.php display information of the book
Left.php is a list of books.
When you click a list-> center.php will display all the books from the database the user has selected books.
main.php is the main page.
It will include the page left.php, center.php.
The problem is how to pass parameters from Left.php to center.php
If I have a main.php page, my list:

Code: Select all

echo "<td><a href='main.php?type=1'>novel</a><br><a href='main.php?type=2'>magazine</a><br></td>"
center after the data connection:

Code: Select all

$ type = $_REQUEST["type"];
$sql = "select idbook, name, type, cost from BOOK where type = '$type'"
$result = mysql_query ($sql);
echo "<table border=1>";
while($row=mysql_fetch_array($ result))
{
   //display information
   echo "<tr>";
   echo "<td>$row[1]....</td>";
}
How REQUEST ["type"] when using include? :?
And then displays the information of the book to center.php
thanks

Re: how to pass parameters when using the include?

Posted: Sat Oct 01, 2011 1:55 pm
by Christopher
All the included files will also have access to the $_REQUEST array.

Re: how to pass parameters when using the include?

Posted: Sun Oct 02, 2011 1:29 am
by GiaTuan
Thanks. :roll: The problem is simple.I just put this line into Left.php (list of books):

Code: Select all

echo "<a href='main.php?type=01'> novel </ a> <a href='main.php?type=02'> magazine </ a>";
in center.php (book information):

Code: Select all

 $type = $type;
   ...
   $sql = "select infotable.idbook,infotable.Author,infotable.Description,Listtable.Name from infotable,Listtable where Listtable.type = '$type' and Listtable.idbook = infotable.idbook";
  $re = mysql_query ($ sql);
  echo "<table>";
  while ($row = mysql_fetch_array ($re))
  {
        // display information
  }
  ....
Finally, include in main.php:

Code: Select all

<td> <? php include ("Left.php"); ?> </td>
<td> <? php
$type =$ _REQUEST["type"];
include ("center.php");
?>
</td>
Address is: main.php? type = <value> when customers click on the list.It will pass parameters when main.php? Type = <value> be opened.Center.php will transfer parameters from main.php
Sometimes everything in front of you, is not far

Re: how to pass parameters when using the include?

Posted: Sun Oct 02, 2011 7:53 pm
by Christopher
First, $type = $type; does not do anything. It is better to access values where they are used. The PHP superglobal variables are available in every PHP script.

Left.php:

Code: Select all

$type =$ _REQUEST["type"];     // you need to filter, validate and escape this value or your site will get hacked
   $sql = "select infotable.idbook,infotable.Author,infotable.Description,Listtable.Name from infotable,Listtable where Listtable.type = '$type' and Listtable.idbook = infotable.idbook";
  $re = mysql_query ($ sql);
  // check for database errors here
  echo "<table>";
  while ($row = mysql_fetch_array ($re))
  {
        // display information
  }
main.php:

Code: Select all

<td> <? php include ("Left.php"); ?> </td>
<td> <? php include ("center.php");
?>
</td>