Jump to content

Separating the backend from the frontend.


Namtip

Recommended Posts

I'm going through my project trying to find a way of separating the backend (the query and php code) from the frontend (html/css). I want to bring the two together with the include function. I've done other but I'm getting confused because the while loops html code. I don't want someone to do this for me, I just what someone to highlight the most logical way.

 

<?php
include 'auth.inc.php';
include 'db.inc.php';
include 'buy.inc.php';

//connect to database.
$db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or
    die ('Unable to connect. Check your connection parameters.');
mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db));

//select orders that this user has bought.	
$query2 = sprintf('SELECT
        d.name_id, d.order_id, d.order_qty, d.product_code, feedback, p.title, c.email
        FROM
        order_details d LEFT JOIN product p ON d.product_code = p.product_code
	LEFT JOIN contact c ON d.name_id = c.name_id
        WHERE
        d.buyer_id = "%u"', mysql_real_escape_string($_SESSION['name_id']));
    $result2 = mysql_query($query2, $db) or die(mysql_error());


//echo out the information in a while loop, add pagination?
$odd = true;
while ($row2 = mysql_fetch_array($result2)) {
    echo ($odd == true) ? '<tr class="odd_row">' : '<tr class="even_row">';
    $odd = !$odd; 
    echo '<td style="text-align: center; width:100px;">' . $row2['title'] . '
        <table>
      <tr>
       <th colspan="2">Shipping Information</th>
      </tr><tr>
       <td>First Name:</td>
       <td>' . $row2['title']. '</td>
      </tr><tr>
       <td>First Name:</td>
       <td>' . $row2['order_qty']. '</td>
      </tr><tr>
       <td>Last Name:</td>
       <td>' . $row2['product_code']. '</td>
      </tr><tr>
       <td>Billing Address:</td>
       <td>' . $row2['email'] . '</td>
   <td>' . $row2['order_id'] . '</td>
      </tr>
     </table>
    </tr>';
if ($row2['feedback'] == 0){
echo '  <form action="reputation.php" method="POST"><p>Please give the seller feedback on how you have received your product.
<td><input type="hidden" name="name_id" value="' . $row2['name_id'] . '"></td>
<td><input type="hidden" name="order_id" value="' . $row2['order_id'] . '"></td>
     <td><input type="submit" name="submit" value="Feedback"/></td>
  </form>';
  }
}
?>

This is generally done by creating an array within your php logic, and simply including that into your 'template' code.

 

So, you would have a template that looked like....

 

template.php

<?php foreach ($records as $record): ?>
    <?php echo ($odd) ? '<tr class="odd_row">' : '<tr class="even_row">';$odd = !$odd; ?> 
    <td style="text-align: center; width:100px;"><?php echo $record['title']; ?>
    <table>
      <tr>
       <th colspan="2">Shipping Information</th>
      </tr><tr>
       <td>First Name:</td>
       <td><?php echo $record['title']; ?></td>
      </tr><tr>
       <td>First Name:</td>
       <td><?php echo $record['order_qty']; ?></td>
      </tr><tr>
       <td>Last Name:</td>
       <td><?php echo $record['product_code']; ?></td>
      </tr><tr>
       <td>Billing Address:</td>
       <td><?php echo $record['email']; ?></td>
       <td><?php echo $record['order_id']; ?></td>
      </tr>
     </table>
    </tr>
    <?php if (!$record['feedback']): ?>
        <form action="reputation.php" method="POST"><p>Please give the seller feedback on how you have received your product.
            <td><input type="hidden" name="name_id" value="' . $row2['name_id'] . '"></td>
            <td><input type="hidden" name="order_id" value="' . $row2['order_id'] . '"></td>
            <td><input type="submit" name="submit" value="Feedback"/></td>
        </form>
    <?php endif ?>
<?php endforeach; ?>

 

And your php.....

 

whatever.php


<?php
include 'auth.inc.php';
include 'db.inc.php';
include 'buy.inc.php';

//connect to database.
$db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or
    die ('Unable to connect. Check your connection parameters.');
mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db));

$query2 = sprintf('SELECT
        d.name_id, d.order_id, d.order_qty, d.product_code, feedback, p.title, c.email
        FROM
        order_details d LEFT JOIN product p ON d.product_code = p.product_code
LEFT JOIN contact c ON d.name_id = c.name_id
        WHERE
        d.buyer_id = "%u"', mysql_real_escape_string($_SESSION['name_id']));
    $result2 = mysql_query($query2, $db) or die(mysql_error());
$odd = true;
$records = array();
while ($row2 = mysql_fetch_array($result2)) {
    $records[] = $row2;
}

include 'template.php';
?>

 

That is the basic idea. Plenty of room for improvement however. You should take a look at the MVC pattern and in particular how allot of frameworks handle views.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.