1

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

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

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.