cbearhoney Posted April 5, 2010 Share Posted April 5, 2010 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; } Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted April 5, 2010 Share Posted April 5, 2010 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. Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted April 5, 2010 Share Posted April 5, 2010 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? Quote Link to comment Share on other sites More sharing options...
cbearhoney Posted April 5, 2010 Author Share Posted April 5, 2010 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.