how to pass parameters when using the include?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
GiaTuan
Forum Newbie
Posts: 14
Joined: Sat Oct 01, 2011 1:19 am

how to pass parameters when using the include?

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: how to pass parameters when using the include?

Post by Christopher »

All the included files will also have access to the $_REQUEST array.
(#10850)
GiaTuan
Forum Newbie
Posts: 14
Joined: Sat Oct 01, 2011 1:19 am

Re: how to pass parameters when using the include?

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: how to pass parameters when using the include?

Post 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>
(#10850)
Post Reply