Jump to content

Referencing a separate MySQL table from within a WHILE loop?


RJP1

Recommended Posts

Hi guys,

 

I've been teaching myself OOP based PHP for a CMS project. I was wondering if you could help me with a slight sticking point? I have 2 tables, one for articles, one for sections.

 

In the articles table, each article is given a section ID (rather than the section name).

 

When I output articles in a loop (limit of 3, to test) I can get the title, the section ID and the content. However, how do I reference the section table to get the corresponding section ID name?

 

I've attached my code so you can see:

 

public function display_public() {
    require_once('includes/DbConnector.php');
    $connector = new DbConnector();
    $result = $connector->query('SELECT * FROM article ORDER BY created DESC LIMIT 3');

    if ( $result !== false && mysql_num_rows($result) > 0 ) {
      while ( $a = mysql_fetch_assoc($result) ) {
        $title = stripslashes($a['title']);
        $section = stripslashes($a['section']);
        $bodytext = stripslashes($a['bodytext']);

#### Article variables are out put here as part of the loop ####

}

 

Thanks for the help! RJP1

The variable named $result is the same variable throughout the scope of that code. If each reference to the same variable name was not to the same variable, it would be impossible to write most code (loops or no loops.)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.