html - Not displaying SQL data through PHP when using a template file -
i have used template.php file create layout of page , inserting content through $content variable stored in separate file. example of file:
<?php $content = ' <table id="job_table"> <tr> <th id="jobtitle">catering assistant</th> <th id="r_code">ref code: 14407773</th> </tr> <tr> <td id="location">northampton</td> <td id="salery">£2,000 monthly</td> </tr> <tr> <td id="description">description</td> <td> assist in catering workers in canteen area. </td> </tr> <tr> <td><a href="apply.php" class="applybutton">apply now!</a></td> <td></td> </tr> </table> '; include 'template.php'; ?> then inside of template using echo display content:
<div id="content_area"> <?php echo $content; ?> </div> when use following code inside of template.php file works intended:
<div id="content_area"> <?php echo $content; ?> <?php $server = '192.168.56.2'; $username = 'student'; $password = 'student'; $schema = 'travelcompanydatabase'; $pdo = new pdo('mysql:dbname=' . $schema . ';host=' . $server, $username, $password); $results = $pdo->query('select * user'); foreach ($results $row) { echo '<p>' . $row['firstname'] . ' ' . $row['lastname'] . '</p>'; } ?> </div> but when use same code within content php file error this:
<?php $title = "jobs"; $content = ' <?php $server = '192.168.56.2'; $username = 'student'; $password = 'student'; $schema = 'travelcompanydatabase'; $pdo = new pdo('mysql:dbname=' . $schema . ';host=' . $server, $username, $password); $results = $pdo->query('select * user'); foreach ($results $row) { echo '<p>' . $row['firstname'] . ' ' . $row['lastname'] . '</p>'; } ?> '; include 'template.php'; ?>
it's better if this:
<?php $title = "jobs"; $content = ''; // create empty string $server = '192.168.56.2'; $username = 'student'; $password = 'student'; $schema = 'travelcompanydatabase'; $pdo = new pdo('mysql:dbname=' . $schema . ';host=' . $server, $username, $password); $results = $pdo->query('select * user'); foreach ($results $row) { // add every result $content using .= $content .= '<p>' . $row['firstname'] . ' ' . $row['lastname'] . '</p>'; } echo $content;
Comments
Post a Comment