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>';
5
  • 2
    Sorry..didnt get you..what was that? Commented Mar 31, 2015 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.. Commented Mar 31, 2015 at 17:55
  • well, you can use the php page itself..no probs. Commented Mar 31, 2015 at 18:00
  • You do know you can close PHP with ?> to append HTML right? Commented Mar 31, 2015 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? Commented Mar 31, 2015 at 18:02

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.