Jump to content

Recommended Posts

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

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

 

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>';
}

Link to comment
https://forums.phpfreaks.com/topic/111955-solved-while-loop-acting-up/
Share on other sites

It is grabbing both. Here is that code.

 

$result = mysql_query("SELECT communities.name, communities.city AS c_city, communities.state AS c_state, communities.price_range AS c_price_range, communities.features AS c_features, communities.id AS c_id, communities.phone AS c_phone, communities.email AS c_email, communities.directions AS c_directions, communities.description AS c_description, listings.title, listings.city AS l_city, listings.id AS l_id, listings.state AS l_state, listings.sq_ft AS l_sq_ft, listings.price AS l_price FROM communities, listings WHERE (communities.id = listings.community_id) && (listings.quick_movein = 'Yes') ORDER BY communities.id ASC");
$number_of_listings = mysql_num_rows($result);
if($number_of_listings > 0) {
while($row = mysql_fetch_array($result)) { 
//then the code that I had in my previous code

well the lowest level it is grabbing is the listings right?  I know you are pulling communtiy data in addition to the listing but the while loop has the listings.

 

what you need to do is strucutre your while loop with some conditions

<?php
$com_id = "";
while($row = mysql_fetch_assoc($r)){
#if its a new "community" lets start a new table
if($row['c_id'] != $com_id){
#conditional ending of previous tables
if(!empty($com_id)){echo "</table>";}
echo "<table>";
}
#print out listing stuff

#make $com_id be the current community ID
$com_id = $row['c_id'];
}
echo "</table>";
?>

Make sense?

try

<?php
<?php
$c_id = "";
while($row = mysql_fetch_array($result)) { 
#conditinoal statement is here
if($row['c_id'] != $c_id) {
if(!empty($c_id){
echo "</table>";
}
echo "<table>";
}
  $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;
   $c_id = $row['c_id'];
   }
  $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>';
}
?>

I tryed that and now I am getting a syntax error with a bracket somewhere. I can't even find where the syntax error is now. This is so frustrating.. ??? I always seem to get horrible at coding when I get overwhelmed or have been doing it all day. I guess that is why I am more dominanat as a designer heh.

 

Would it help if you have my whole file? I have .rar'ed it up and put it up if that might help.

 

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

 

And this is the syntax error I am getting...

 

Parse error: syntax error, unexpected '{' in /home/tristone/public_html/inventory.php on line 87

 

Let me know if that helps. Thanks so much for the help!

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.