tags:

views:

98

answers:

3

header

$id = intval($_GET['id']);
$query = mysql_query("SELECT * FROM table WHERE id = $id");

view

while ($row = mysql_fetch_array($query)) {
 $column1 = $row['column1'];
 $column2 = $row['column2'];
 $column3 = $row['column3'];

 echo $column1.......
}

How can I keep the above code in my header file?
So my designers wont have to see it?

Can you save the data into variables and print it out on the page?

Thanks!

A: 

You could create a function which takes the $GET_['id'] value as a parameter and returns whatever is supposed to be echo'd.

Something like: (inc.php)

<?php
function queryCall($id)
{
    $query = mysql_query("SELECT * FROM table WHERE id = $id");
    $ret = "";
    while ($row = mysql_fetch_array($query)) {
        $column1 = $row['column1'];
        $column2 = $row['column2'];
        $column3 = $row['column3'];

        $ret .= $column1.......
    }
    return $ret;
}
?>

and then in your main file:

<?php include 'inc.php'; ?>

...

<?php
echo queryCall(intval($_GET['id']));
?>
...
DeadHead
A: 

I suggest you intval() the ID inline just so it is clear that the query is safe from SQL injection.

Put all of the code in a file called header.php. Use the loop to create an array(). Call the array, say, $Rows.

Then in your main html file (.php), put this:

<?php include('header.php'); ?>
<html>
   <head>...</head>
   <body>
      <table>
          <?php foreach($Rows as $row) { ?>
             <tr>
                <td><?php echo htmlspecialchars($row['column1']); ?></td>
                <td><?php echo htmlspecialchars($row['column2']); ?></td>
                <td><?php echo htmlspecialchars($row['column3']); ?></td>
             </tr>
          <?php } ?>
      </table>
   </body>
</html>
gahooa
+1  A: 

You could use the MVC pattern to separate logic from presentation.

The simplest way would be to just save up all data you want to present into either local variables or an array perhaps and then require a view file.

The view file just echos the data in a html template.

If you want to dig deeper you could check out a framework like codeigniter or cakephp.

pcguru