Build html table from content of array created from SQL qury

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Judda
Forum Newbie
Posts: 1
Joined: Tue Feb 08, 2011 4:13 am

Build html table from content of array created from SQL qury

Post by Judda »

Hi there,

I'm returning to programming after 15+ years of doing other things 8O

I could do with some help, I've decided to jump into PHP, I like it, my previous languages were things like Visual Basic, Dbase and Foxbase 2.11 (anyone ?!)

I've created a MySQL database and tables and I can pull data from the tables into an array, no problem, however I'm running into difficulties when trying to create an HTML table from that data. Ideally I want to step through the array and reformat some of the data before displaying it in an html table. All of the tutorials I've managed to find are largely about building a dynamic html table, read in data, spit out data, done ! I want to read in data, reformat some of the data, or display a symbol based on the contents of the data and then build an html table that I have control of, IE: I say how big the cells are etc.

Could anyone point me in the direction of such a tutorial ?

Many Thanks
Judda
divedj
Forum Commoner
Posts: 47
Joined: Wed Dec 29, 2010 4:32 am
Location: Malta

Re: Build html table from content of array created from SQL

Post by divedj »

I am not sure of a toturial covering all of that but may be I can point you in a direction to look further...

Let's start with the array you pulled out of a db table:

Code: Select all

<html>
    <head>
        <!-- Style sheet example used to format the table --!>
        <style type="text/css">
            #myFormattedCell {
                background-color: #808080;
           }
</style>
    </head>
    <body>
        <?php
             //some code to get your array from a db table 
        ?>
        <table> <!-- Here we start our table and for easy formating we stay in html syntax --!>
            <?php
                 //here we loop through the array to pull out the db field content.
                 foreach($arrayName as $arrayVar)
                 {
                      echo "<tr>
                                     <td>".$arrayVar['dbfield1']."</td>  //simple displaying the db field data
                                     <td valign=\"middle\">".$arrayVar['dbfield2']."</td>  /*displaying the data with fixed formating of table cell*/
                                     <td>";
                                         if($arrayVar['dbfield3'] == 0)   /*here we use the if statement to check the content and display a image instead of the db content*/
                                         {
                                             echo "<img src=\"URL of image to display\">";
                                         }
                                         else
                                         {
                                             echo "<img src=\"URL of other image to display\">";
                                         }  
                            echo "</td>
                                    <td id=\"myformatedCell\">".$arrayVar['dbfield4']."</td>  /*here we give the datacell a name so we can format it using a css style sheet*/
                                    <td>";
                                         if($arrayVar['dbfield5'] != "foo")   /*here we format the text to change color depending on the field content*/
                                         {
                                              echo "<font color=\"#FF0000\">".$arrayVar['dbfield5']."</font>";
                                         }
                                         else
                                         {
                                              echo "<font color=\"#008000\">".$arrayVar['dbfield5']."</font>";
                                         }
                            echo "</td>
                               </tr>";
                 }
            ?>
        </table>
    </body>
</html>
Well, this is not a complete toturial but it might give you a few ideas to carry on with your project .
Post Reply