Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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

share|improve this question
    
Sorry, could you please post what that $page.'.php' outputs? Those �� don't come from nowhere. –  arkascha Feb 21 at 13:26
    
it just outputs the code from the products page it doesnt process it –  Zac Connolly Feb 21 at 13:28
    
Naything in the error log file? –  arkascha Feb 21 at 13:29
    
��<�?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 Feb 21 at 13:29
    
I'd say that is the output of the code you posted. Not that of that require statement. –  arkascha Feb 21 at 13:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.