while loop is acting up :(

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mongoose00318
Forum Newbie
Posts: 5
Joined: Wed Jun 25, 2008 8:05 pm

while loop is acting up :(

Post by mongoose00318 »

This is part to a dynamic page I made. It retrieves information from a database and then displays it. It retrieves communitys and then it retrieves the listings and puts each listing it pulls with the community it belongs to. The way it should display in a simplified format is below :

Community 1
<table starts>
<tr>
<td>Listing 1</td>
<td>Listing 2</td>
</tr>
</table ends>
Community 2
<table starts>
<tr>
<td>Listing 1</td>
<td>Listing 2</td>
</tr>
</table ends>

etc...

But it is doing..

Community 1
<table starts>
<tr>
<td>Listing 1</td>
</tr>
</table ends>
<table starts>
<tr>
<td>Listing 2</td>
</tr>
</table ends>
Community 2
<table starts>
<tr>
<td>Listing 1</td>
</tr>
</table ends>
<table starts>
<tr>
<td>Listing 2</td>
</tr>
</table ends>

I know it is doing this because the

Code: Select all

echo '</table>';
is inside of the while(); loop. But I can't figure out how to make it put the </table> after it is done echoing all the listings for a community. Any help? I hope I have been specific enough. Thanks!

Also, you can view the live script here...http://www.tristonehomes.com/inventory.php

Code: Select all

 
while($row = mysql_fetch_array($result)) { 
 if($row['name'] != $current_community) {
  $current_community = $row['name']; 
   echo('
    <br>
    <span class="community_title">'.ucfirst($current_community).'</span> <span class="location">'.ucfirst($row['c_city']).', '.ucfirst($row['c_state']).'</span>
    <div class="divider"></div>
    <span class="general_info">Price Range:</span> <span class="general_values">'.ucfirst($row['c_price_range']).'</span><span class="general_info">Phone:</span> <span class="general_values">'.$row['c_phone'].'</span><span class="general_info">Email:</span> <a href="mailto:'.$row['c_email'].'" class="email_link">'.$row['c_email'].'</a>
    <div class="general_links_container">
    <a href="community_features/'.$row['c_features'].'" class="general_links">Standard Features</a> | <a href="view_inventory.php?community_id='.$row['c_id'].'" class="general_links">View Current Inventory</a> | <a href="directions/'.$row['c_directions'].'" class="general_links">Community Details / Directions</a>
    </div>
    <span class="general_info">Community Summary</span>
    <br />
    <span class="community_summary">'.ucfirst($row['c_description']).'</span>
    <div class="divider"></div>
    <span class="floorplan_title">Floorplans</span> <span class="general_info">click on plan name for details</span>
    <br />
    <table border="0" cellspacing="0" cellpadding="0">
   ');
   $tracker = 0;
   }
  $title = strtolower($row['title']);
  $tracker = 1 - $tracker;
  echo '<tr class="row'.$tracker.'"><td class="cell_spacing"><a href="view_listing.php?listing_number='.$row['l_id'].'" class="specfic_info">'.ucfirst($title).'</a></td>';
  echo '<td class="cell_spacing"><span class="specfic_info">Square Footage : '.ucfirst($row['l_sq_ft']).'</span><br></td>';
  echo '<td class="cell_spacing"><span class="specfic_info">Base Price : $'.ucfirst($row['l_price']).'</span><br></td></tr>';  
  echo '</table>';
}
 
 
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: while loop is acting up :(

Post by WebbieDave »

Why not something like:

Code: Select all

$firstTime = true;
while($row = mysql_fetch_array($result)) {
    if($row['name'] != $current_community) {
        $current_community = $row['name'];
        if (!$firstTime) {
            echo "\n</table>\n";
        } else {
            $firstTime = false;
        }
        echo "\n<table>\n";
    }
    // etc...
}
mongoose00318
Forum Newbie
Posts: 5
Joined: Wed Jun 25, 2008 8:05 pm

Re: while loop is acting up :(

Post by mongoose00318 »

Hm, I am not really following how that controls when </table> will be put in. Could you explain how it is working? I am still somewhat new to PHP so it takes me a bit more to catch on sometimes.
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: while loop is acting up :(

Post by WebbieDave »

Which lines of my example code are you giving you trouble? It's pretty barebones so step through it in your head a few times while writing down the output and it should become clear. If not, let me know which part needs explaining.
mongoose00318
Forum Newbie
Posts: 5
Joined: Wed Jun 25, 2008 8:05 pm

Re: while loop is acting up :(

Post by mongoose00318 »

Well, I thought it through and understood your thought process with it. But it still is not working right. Take a look here..

http://www.tristonehomes.com/inventory.php

Any ideas?
mongoose00318
Forum Newbie
Posts: 5
Joined: Wed Jun 25, 2008 8:05 pm

Re: while loop is acting up :(

Post by mongoose00318 »

I know why that code isn't working...I think anyways. When it runs through the code that is outputting the listings it is checking that section of code that you came up with. Then, it resets the variable to false and after the first listing every listing after that is having a </table> at the end. Did that make sense?

Also, others suggestions are welcome! :)
mongoose00318
Forum Newbie
Posts: 5
Joined: Wed Jun 25, 2008 8:05 pm

Re: while loop is acting up :(

Post by mongoose00318 »

Nevermind, I solved the problem! Your code did work with a bit of a modification to it. Thanks, your awesome man!
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: while loop is acting up :(

Post by WebbieDave »

Fantastic! The code was just an example to get you thinking about the approach and by golly you've done it! Great work.
Post Reply