segfault
segfault

Reputation: 13

What is the best way to display HTML content using PHP?

I have a php function as shown below, which takes in an array that has values from an SQL query (in another, not shown function). In this case, the SQL query returns a set of filenames and their associated picture names.

// Take in an array of filters;
// each element is a dictionary of column name 
// to the value of that row from SQL query
function displayFilters($filters) {
    foreach($filters as $filter){
        //output images in img tags, with their appropriate classes
        echo "  <div class='w3-quarter'>
                    <div class='w3-card-2'>
                        <img src='images/".$filter['File_Name']."' style='width:100%'>
                            <div class='w3-container'>
                                <h4>".$filter['Filter_Name']."</h4>
                            </div>
                    </div>
                </div>";
    }

} 

Now, this works fine and will properly display the images as I want. However, I keep having to modify this code, change classes, properties and what not, and if I need to add/modify to the div, I have to go into this function to change everything. Is there a better way to do this than going into the echo function to change it? I'd rather not have to use Javascript if possible, but if that is the only clean way to do it, can someone point me to a way to do this?

Upvotes: 1

Views: 1536

Answers (3)

Amarinder Singh Thind
Amarinder Singh Thind

Reputation: 140

I have written how to retrieve query and how to display in below code. You can use this one. you can also use image tag within table <tr> tag.

  $con=mysqli_connect('localhost','username','password','databasename');
  $sql="SELECT  `COL 1` ,  `COL 2` ,  `COL 3` ,  `COL 4` ,  `COL 5` ,  `COL 12` FROM  `TABLE` WHERE 1 ORDER BY  `COL 3`  ;";
  $result=mysqli_query($con,$sql);


<table style="width:100%"  >
 <tr>
 <th>Col1</th>
 <th>Clo2</th> 
 <th>Col3</th>
  </tr>

<?
while ($row=mysqli_fetch_row($result))
{

      echo "<tr><td>".$row[0]."</td><td>".$row[1]."\t".$row[2]."\t".$row[3]."\t".$row[4]."</td><td>".$row[5]."</td></tr>";  
 }

?>

     </table>

Upvotes: 1

Bharat D Bhadresha
Bharat D Bhadresha

Reputation: 779

    <?php    function displayFilters($filters) {
            foreach($filters as $filter){ ?>
                //output images in img tags, with their appropriate classes
                <div class='w3-quarter'>
                            <div class='w3-card-2'>
                                <img src='images/<?=$filter['File_Name']>' style='width:100%'>
                                    <div class='w3-container'>
                                        <h4><?=$filter['Filter_Name']></h4>
                                    </div>
                            </div>
                        </div>
<?php
            }

        } ?>

I do it this way and it seems easy than learning a new template engine

Upvotes: 2

erikvimz
erikvimz

Reputation: 5476

Use a PHP Template Engine


http://www.smarty.net/

Smarty is fast and lean with a small memory footprint.


http://twig.sensiolabs.org/ (Looks remarkably like Dwoo)

Twig is a modern template engine for PHP (Fast, Secure, Flexible)


http://dwoo.org/ (Inspired by Smarty)

Dwoo is a templating system used to make creating websites easier and more structured.


http://platesphp.com/ (Inspired by Twig)

Plates is a native PHP template system that’s fast, easy to use and easy to extend.


http://phptal.org/

PHPTAL is a PHP implementation of ZPT work. To be short, PHPTAL is a XML/XHTML template library for PHP.

Upvotes: 1

Related Questions