I am trying to pass a page through require ($page . ".php"); however it just returns the code from the page. the variable $page is connected to a products page. code shown below. ...index page...
<?php
session_start();
require_once("connection.php");
if (isset($_GET['page'])){
$pages = array("products","cart");
if(in_array($_GET['page'],$pages)){
$page = $_GET['page'];
}else{
$page = "products";
}
}else {
$page = "products";
}
?>
<?php
require ($page . ".php");
?>
...products page...
<?php
session_start()
?>
<h1>Product list<h1>
<table>
<tr>
<th>name</th>
<th>Description</th>
<th>Price</th>
</tr>
<tr>
<?php
$sql="SELECT * FROM `products` ORDER BY name ASC";
$query=mysql_query($sql);
while ($row = mysql_fetch_array($query) or die (mysql_error()))
{
?>
<tr>
<td><?php echo $row['name'] ?></td>
<td><?php echo $row['description'] ?></td>
<td><?php echo $row['price'] ?></td>
<td><a href="index.php?page=products&action=add&id=<?php echo $row['id_product'] ?>">Add to cart</a></td>
</tr>
<?php
}
?>
</table>
Out put
��<�?php session_start() ?> <�h1>Product list<�h1> <�table> <�tr> <�th>name<�/th> <�th>Description<�/th> <�th>Price<�/th> <�/tr> <�tr> <�?php $sql="SELECT * FROM products ORDER BY name ASC"; $query=mysql_query($sql); while ($row = mysql_fetch_array($query) or die (mysql_error())) { ?> <�tr> <�td><�?php echo $row['name'] ?><�/td> <�td><�?php echo $row['description'] ?><�/td> <�td><�?php echo $row['price'] ?><�/td> <�td><�a href="index.php?page=products&action=add&id=<�?php echo $row['id_product'] ?>">Add to cart<�/a><�/td> <�/tr> <�?php } ?> <�/table>
What am I doing wrong? it works fine when the code from the products page is included in the index page however passing through the page doesnt work. Could someone please explain to me why its not working thanks
$page.'.php'outputs? Those��don't come from nowhere.productsORDER BY name ASC"; $query=mysql_query($sql); while ($row = mysql_fetch_array($query) or die (mysql_error())) { ?> <�tr> <�td><�?php echo $row['name'] ?><�/td> <�td><�?php echo $row['description'] ?><�/td> <�td><�?php echo $row['price'] ?><�/td> <�td><�a href="index.php?page=products&action=add&id=<�?php echo $row['id_product'] ?>">Add to cart<�/a><�/td> <�/tr> <�?php } ?> <�/table> thats the output