1
votes

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

1
Sorry, could you please post what that $page.'.php' outputs? Those �� don't come from nowhere. - arkascha
it just outputs the code from the products page it doesnt process it - Zac Connolly
Naything in the error log file? - arkascha
��<�?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> thats the output - Zac Connolly
I'd say that is the output of the code you posted. Not that of that require statement. - arkascha

1 Answers

1
votes

There seems to be a special character between the < and ?. Then it does not recognize it as start of php code.

Maybe your required file has some weird encoding? Which Editor are you using?