Jump to content

Referencing a separate MySQL table from within a WHILE loop?


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.)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.