Returning a partial list

16 posts by 2 authors in: Forums > CMS Builder
Last Post: April 19, 2010   (RSS)

By dennisoneil - April 16, 2010

I have if currently setup in three categories:

<2500
2500-3500
>3500

If I did an "if <2500, else" I'd get all the other plans in the "else". What I want is to say:

If some plans <2500 exist, do this
If none <2500 exist, do this
If some plans 2500-3500 exist, do this
If none 2500-3500 exist, do this
If some plans >3500 exist, do this
If none >3500 exist, do this


All of these are displayed on the same page.

Thoughts?

-Dennis

Re: [dennisoneil] Returning a partial list

By Jason - April 16, 2010

Hi Dennis,

I think we found a solution. First, we're going to need to find out if there are any records for each condition. Put this code somewhere below where you do your query, but before your output:

<?php
$square_foot[0]=0;
$square_foot[1]=0;
$square_foot[2]=0;

foreach($productRecords as $record){
if($record['square_footage']<25)
$square_foot[0]++;
else if($record['square_footage']>=2500 && $record['square_footage']<=3000 )
$square_foot[1]++;
else if($record['square_footage']>=3000)
$square_foot[2]++;
}
?>


We now have an array that tells us how many records exist that fit each condition (0 if there are none)

Now we can output like this:

<div>
<?php foreach($productRecords as $record): ?>
<?php if($square_foot[0]==0): ?>
*NO RECORDS*
<?php elseif($record['square_footage']<2500): ?>
*RECORD*
<?php endif ?>
<?php endforeach ?>
</div>

<div>
<?php foreach($productRecords as $record): ?>
<?php if($square_foot[1]==0): ?>
*NO RECORDS*
<?php elseif($record['square_footage']>=2500 && $record['square_footager']<=3000): ?>
*RECORD*
<?php endif ?>
<?php endforeach ?>
</div>

<div>
<?php foreach($productRecords as $record): ?>
<?php if($square_foot[2]==0): ?>
*NO RECORDS*
<?php elseif($record['square_footage']>3000): ?>
*RECORD*
<?php endif ?>
<?php endforeach ?>
</div>


Here, you can replace *NO RECORDS* with whatever you want to display if there are no records for that condition. You can also replace *RECORD* with whatever you want if there are records that match that category.

Give that a try and let me know if that works for you.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [dennisoneil] Returning a partial list

By Jason - April 19, 2010

Hi,

Okay, I have a better understanding of what you're trying to do now. We can try this, just changing our code slightly:
<ul class="community-floorplans-list">
<?php if($square_foot[0]!=0): ?>
<li><a id="small-homes-button" href="#">2,500 sq ft and below</a></li>
<?php endif ?>
<?php if($square_foot[1]!=0): ?>
<li><a id="large-homes-button" href="#">2,000 to 3,000 sq ft</a></li>
<?php endif ?>
<?php if($square_foot[2]!=0): ?>
<li><a id="larger-homes-button" href="#">3,000 sq ft and up</a></li>
<?php endif ?>
</ul>


So we're just determining whether or not to display a button, so we won't need the foreach loops we were using. Give that a try.

If it doesn't work, put in this piece of code for testing:

<?php
echo $square_foot[0]."<br/>;
echo $square_foot[1]."<br/>;
echo $square_foot[2]."<br/>;
?>


Let me know what that outputs (should be 3 numbers, each on a separate line).

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By dennisoneil - April 19, 2010

Hi Jason,

After no success with the revised code, but a successful "test" with the code you provided, I realized the error I was making. I was also filtering my results on the page by an second criteria. There WERE homes that fit the (<2500, etc) size criteria, but none were displayed because I was also filtering based on the presence of a price. I revised my page with the code below and its now working.


<?php
$square_foot[0]=0;
$square_foot[1]=0;
$square_foot[2]=0;

foreach($homesRecords as $record){
if($record['size']<2500)
$square_foot[0]++;
else if($record['size']>=2500 && $record['size']<=3000 )
$square_foot[1]++;
else if($record['size']>=3000)
$square_foot[2]++;
}
?>
<?php $field="price_".$_SERVER['QUERY_STRING']; ?>


<ul class="community-floorplans-list">
<?php foreach($homesRecords as $record): ?>
<?php if($square_foot[0]==0): ?>
<?php elseif($record['size']<2500 && $record[$field] != ""): ?>
<li><a id="small-homes-button" href="#">2,500 sq ft and below</a></li>
<?php break; ?>
<?php endif ?>
<?php endforeach ?>
<?php foreach($homesRecords as $record): ?>
<?php if($square_foot[1]==0): ?>
<?php elseif($record['size']>=2500 && $record['size']<=3000 && $record[$field] != ""): ?>
<li><a id="large-homes-button" href="#">2,500 to 3,000 sq ft</a></li>
<?php break; ?>
<?php endif ?>
<?php endforeach ?>
<?php foreach($homesRecords as $record): ?>
<?php if($square_foot[2]==0): ?>
<?php elseif($record['size']>3000 && $record[$field] != ""): ?>
<li><a id="larger-homes-button" href="#">3,000 sq ft and up</a></li>
<?php break; ?>
<?php endif ?>
<?php endforeach ?>
</ul>


Thanks so much for all your help.

-Dennis

Re: [dennisoneil] Returning a partial list

By Jason - April 19, 2010

Awesome! Glad it all works. :)
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/