1

So I am able to extract data from MYSQL and iterate through it and populate a <table> using html tags to render in the browser. I also have a separate CSS file that will style the <table> for me.

My question is, does the rest of the webpage that contains the table have to be constructed from the same PHP file and/or with PHP? Please can someone advise?

My code thus far populating the table and creating the webpage is as such:

  // Creating and populating table for 'filename' and 'title'.
    echo '<link rel="stylesheet" type="text/css" href="css/main.css"/>';
    echo '<table>';
    echo "<tr><td class='tableHeader' colspan=2>List of Articles</td></tr>";
    echo '<tr> <th>Filename</th> <th>Title</th> </tr>';

    while($row = $result -> fetch_assoc()){
        // Fetch result row as an associative array or NULL if there are no more rows.
        $filename = $row['filename'];
        $title = $row['title'];

        echo "<tr> <td class='filename'>".$filename."</td> <td class='title'>".$title."</td></tr>";


    }//end of while loop


   echo '</table>';
D4V1D
  • 5,805
  • 3
  • 30
  • 65
dave
  • 475
  • 6
  • 17
  • 1
    Sorry..didnt get you..what was that? – Lal Mar 31 '15 at 17:53
  • the code above generates a webpafe that contains just my table. I would natually like to spruce up the page with banners, some photos etc.. would all this have to be done through the same php file that i am currently using? Not sure that makes sense.. – dave Mar 31 '15 at 17:55
  • why did you down vote the question without an explanation? – dave Mar 31 '15 at 17:57
  • No, i dint downvote..Who said i downvoted? – Lal Mar 31 '15 at 17:58
  • sorry..wasn't direct at you. – dave Mar 31 '15 at 17:58
  • well, you can use the php page itself..no probs. – Lal Mar 31 '15 at 18:00
  • You do know you can close `PHP` with `?>` to append `HTML` right? – D4V1D Mar 31 '15 at 18:00
  • i didn't know that. it is my first application. thank you. so should the file name be .php or .html or does it not matter when mixing up php and html? – dave Mar 31 '15 at 18:02

1 Answers1

3

You need to understand the following for coding HTML in PHP files (which have to be named by .php extension for PHP to be parsed by the server).

PHP is always surrounded by <?php and ?> (not always for the latter, we'll get back at this). That means you can do:

<table>
    <?php foreach($array as $key => $el) { ?>
       <tr>
          <td><?php echo $key;?></td>
          <td><?php echo $el;?></td>
       </tr>
    <?php } ?>
</table>

In that case, when you come to use these control structure, you should use the alternative ones. This is better for understanding code structure. Take the habit of writing PHP code in one line when doing a lot of HTML.

You can also set variables and do as many treatments you need just by following this rule.

<section>
    <?php for($i = 1; $i < 50; $i++): ?> <!-- note the alternative syntax -->
        <?php $class = 'myClass-'.$i; ?> 
        <div class="<?php echo $class;?>">
            <span>This is div #<?php echo $i;?></span>
        </div>
    <?php endfor;?> 
</section>

This is to get you used to template engines like Twig or Smarty.

Finally, you'll come to see many entire PHP files which don't finish with ?>. That is to prevent headers to be sent if file is included before header manipulation with PHP in other files in case there was a whitespace after the closing tag.

Community
  • 1
  • 1
D4V1D
  • 5,805
  • 3
  • 30
  • 65