Jump to content

While loop question - help!


cbearhoney

Recommended Posts

I have a  function that should display addresses in this format:

 

Streetname

House#1    Apt#      Name

House#2    Apt#      Name

House#3    Apt#      Name

 

 

It should display multiple addresses under each streetname. My below code only returns the first row and not all results for the street. What's wrong? I'm sure I'm missing a loop somewhere.....

(You may see a couple of drupal functions...but you get the idea)

 

 

 
    $page_content = '';

    /*Get address detail*/
    $result = db_query('SELECT street FROM {terr_master} AS m
                                    WHERE tid = 130
                                    GROUP BY m.street
                                    ORDER BY m.street ASC');

    while($data = db_fetch_object($result)) {
        $streets = $data->street;
        $addresses = db_fetch_object(db_query('SELECT `house`, `apt`, `name`
                                    FROM {terr_master} AS m
                                    WHERE tid = 130
                                    AND street = 'Flatbush'
                                    ORDER BY m.house ASC'));

        $page_content .= '<p>' . $streets . '<br/>';

        $page_content .= $addresses->house . ' ' . $addresses->apt .' ' .  $addresses->name;
    }

    return $page_content;
}

Link to comment
Share on other sites

The problem is that you get a street and build a loop around it:

 

$result = db_query('SELECT street FROM {terr_master} AS m
                                    WHERE tid = 130
                                    GROUP BY m.street
                                    ORDER BY m.street ASC');

    while($data = db_fetch_object($result)) {

 

But when you get the individual addresses, you don't loop through the results:

 

$addresses = db_fetch_object(db_query('SELECT `house`, `apt`, `name`
                                    FROM {terr_master} AS m
                                    WHERE tid = 130
                                    AND street = 'Flatbush'
                                    ORDER BY m.house ASC'));
        $page_content .= '<p>' . $streets . '<br/>';
        $page_content .= $addresses->house . ' ' . $addresses->apt .' ' .  $addresses->name;

 

You need to loop through the addresses too.

Link to comment
Share on other sites

Also just noticed, your 2nd query should look like this:

 

$addresses = db_fetch_object(db_query('SELECT `house`, `apt`, `name`
                                    FROM {terr_master} AS m
                                    WHERE tid = 130
                                    AND street = \'Flatbush\'
                                    ORDER BY m.house ASC'));

 

You need to escape the slashes for the street or you're telling PHP to end the string early. Surprised this didn't result in a PHP parse error?

Link to comment
Share on other sites

Got it! Thanks. I changed my code to this and it works:

 

    /*Get address detail*/
    $result = db_query('SELECT street FROM {terr_master} AS m
                                    WHERE tid = %d
                                    GROUP BY m.street
                                    ORDER BY m.street ASC', $tid);

    while($data = db_fetch_object($result)) {
        $streets = $data->street;
        $result2 = (db_query('SELECT `house`, `apt`, `name`
                                    FROM {terr_master} AS m
                                    WHERE tid = %d
                                    AND street = \'%s\'
                                    ORDER BY m.house ASC', $tid, $data->street));
        $page_content .= '<strong>' . $streets . '</strong><br/>';
        while ($addresses = db_fetch_object($result2)){
            $cells = $addresses->house . ' ' . $addresses->apt .' ' .  $addresses->name . '<br/>';
            $page_content .= $cells;
        }
    }
    return $page_content;

 

I'm actually using placeholders in my query where I escape the streetname string. I didn't show that in my previous post. Thanks again!

Link to comment
Share on other sites

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.