Namtip Posted November 30, 2010 Share Posted November 30, 2010 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>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/220198-separating-the-backend-from-the-frontend/ Share on other sites More sharing options...
trq Posted November 30, 2010 Share Posted November 30, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/220198-separating-the-backend-from-the-frontend/#findComment-1141219 Share on other sites More sharing options...
JakeTheSnake3.0 Posted November 30, 2010 Share Posted November 30, 2010 Also, on an unrelated note, you can shorten the odd/even echo this way: <?php echo '<tr class="' . ($odd) ? 'odd' : 'even' . '_row">';$odd = !$odd; ?> Quote Link to comment https://forums.phpfreaks.com/topic/220198-separating-the-backend-from-the-frontend/#findComment-1141220 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.