DISPLAYING LISTS based on fields

22 posts by 3 authors in: Forums > CMS Builder
Last Post: February 19, 2014   (RSS)

Hello! 

I've figured out my original dilemma of getting a list of a specific breed to show under individual headers using <?PHP if ($record['breed'] == "Arabian"): ?> so I deleted that post.  I have 2 small issues left that I would really appreciate help with. 

#ONE:  2 COLUMN LIST
Any breed list that follows a listing of just one stallion causes the next list to skip the top right cell. Scroll down to Danish Warmblood (1 stallion) and Dutch (multiple stallions). I've tried A LOT of variations and I'm sure it's super obvious. 

http://www.superiorequinesires.com/stallion-list6breeds.php

<table width="260" border="1" cellpadding="3" cellspacing="0" bordercolor="#D7C59C" >                     <tr>                       <?php foreach ($stallion_listRecords as $record): ?>                       <?PHP if ($record['breed'] == "Anglo-Arabian"): ?>                       <td align="left" valign="top">                                                                     <table width="244" cellpadding="0" cellspacing="2">                           <tr>                             <td height="20" valign="bottom"><h3><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['title']) ?></a></h3></td>                           </tr>                           <tr>                             <td class="content"><?php echo htmlencode($record['price']) ?>                                 <?php if ($record['status'] == 'New') : ?>                                 <img src="http://www.superiorequinesires.com/images/icon-star-new-stallions.png" width="26" height="26" />                                 <?PHP endif ?>                                 <?php if ($record['status'] == 'Special') : ?>                                 <img src="http://www.superiorequinesires.com/images/icon-star-new-stallions.png" width="26" height="26" />                                 <?PHP endif ?>                                 <?php echo join(', ', $record['status:labels']); ?> </td>                           </tr>                           <tr>                             <td><?php if (!$stallion_listRecords): ?>                               No records were found!                               <?php endif ?>                     <!-- /STEP2: Display Records -->                            </td>                           </tr>                       </table>                      </td>   <?php $maxCols=2; if (@++$count % $maxCols == 1): ?>                     </tr>                     <tr>                       <?php endif; ?>                       <?php endif; ?>                       <?php endforeach ?>                     </tr>                 </table>

#TWO: STALLION STATUS LIST
Using a copy of the Breeds display page (referred to above), I made a Status display page. For instance, if in the multi-check option "new" and "in-stock" are both checked, I would like that stallion's name to show up under both the "new" header and the "in-stock" header.  I've noticed the difference between the breed display page (above) and this one is that the breeds are chosen from a drop down list and only one breed can be selected, however, the status has many checkbox options that can be checked.  I tried "status", "status:values" & "status:labels" but nothing displays so I'm obviously messing that part up. 

status list: <?php if ($record['status:labels'] == "In-Stock"): ?>

http://www.superiorequinesires.com/stallion-list6status.php

<table width="260" border="1" cellpadding="3" cellspacing="0" bordercolor="#D7C59C" >                       <tr>                         <?php foreach ($stallion_listRecords as $record): ?>                         <?php if ($record['status:labels'] == "In-Stock"): ?>                         <td align="left" valign="top"><table width="244" cellpadding="0" cellspacing="2">                             <tr>                               <td height="20" valign="bottom"><h3><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['title']) ?></a></h3></td>                             </tr>                             <tr>                               <td class="content"><?php echo htmlencode($record['price']) ?>                                   <?php if ($record['status'] == 'New') : ?>                                   <img src="http://www.superiorequinesires.com/images/icon-star-new-stallions.png" width="26" height="26" />                                   <?PHP endif ?>                                   <?php if ($record['status'] == 'Special') : ?>                                   <img src="http://www.superiorequinesires.com/images/icon-star-new-stallions.png" width="26" height="26" />                                   <?PHP endif ?>                                   <?php echo join(', ', $record['status:labels']); ?> </td>                             </tr>                             <tr>                               <td><?php if (!$stallion_listRecords): ?>                                 No records were found!                                 <?php endif ?>                         <!-- /STEP2: Display Records -->                              </td>                             </tr>                         </table></td>                         <?php $maxCols=2; if (@++$count % $maxCols == 1): ?>                       </tr>                       <tr>                         <?php endif; ?>                         <?php endif; ?>                         <?php endforeach ?>                       </tr>                   </table>

Thanks so much!! Tina

on this same note... if I have a status called "archived".  Can it be set so if any stallion's status = "archived" then it won't show on this page?  I'll put the entire "archived" stallions on their own page. 

I'm so glad I checked this again. I replied yesterday but it's not here. it's happened to everything I've touched for at least a week now. 

PHP source code is attached.

I am not against using divs instead of tables. I tried it on a few tests but then the list goes completely horizontal in 1 row rather than 2 rows like the breed list ( http://www.superiorequinesires.com/stallion-list6breeds.php ) . I'm sure it's really obvious. :( 

By Chris - January 29, 2014 - edited: January 29, 2014

Hi Tina,

You're having trouble with our forum deleting your posts? If so, please shoot me an email at chris@interactivetools.com and we'll look into that!

Here's an example of how to use floated divs instead of tables for two-column data. If you like, throw this in an HTML page and check it out in your browser. But this is just for reference, I've addressed your </tr><tr> table approach in the next message below.

<style>
.container { width: 205px; } /* container width should be cols * cell width, but add a few extra pixels for IE -- as long as it's less than (cols+1) * cell width, you're safe! */
.cell      { width: 100px; float: left; }
.clear     { clear: both; }
</style>

<h2>Container Start</h2>
<div class="container">
  <div class="cell">Testing...</div>
</div>
<div class="clear"></div>

<h2>Container Start</h2>
<div class="container">
  <div class="cell">Testing...</div>
  <div class="cell">Testing...</div>
</div>
<div class="clear"></div>

<h2>Container Start</h2>
<div class="container">
  <div class="cell">Testing...</div>
  <div class="cell">Testing...</div>
  <div class="cell">Testing...</div>
</div>
<div class="clear"></div>

<h2>Container Start</h2>
<div class="container">
  <div class="cell">Testing...</div>
  <div class="cell">Testing...</div>
  <div class="cell">Testing...</div>
  <div class="cell">Testing...</div>
</div>
<div class="clear"></div>

<h2>Container Start</h2>
<div class="container">
  <div class="cell">Testing...</div>
  <div class="cell">Testing...</div>
  <div class="cell">Testing...</div>
  <div class="cell">Testing...</div>
  <div class="cell">Testing...</div>
</div>
<div class="clear"></div>

All the best,
Chris

By Chris - January 29, 2014

Your </tr><tr> code will work fine, but you'll need to reset $count to zero before every foreach. The problem is that the code is "remembering" the column count from the last table, so it'll sometimes jump down to the second row after the first cell. Just add this line before every foreach:

<?php $count = 0; ?>
<?php foreach ($stallion_listRecords as $record): ?>

Your approach of filtering records with if ($record['status'] == ... should also work fine, but the problem is probably that the data isn't exactly what you're expecting. Inspecting the data in your records might show what the problem is. Can you try adding this one line of code right before your include_header.php include?

<?php if (@$_REQUEST['debug']) { showme($stallion_listRecords); exit; } ?>
<?php include "include_header.php"; ?>

Then we can go to http://www.superiorequinesires.com/stallion-list6status.php?debug=1 and see what the status and breed fields are set to.

If that's not possible for whatever reason, please let me know and I'll try and think of another approach.

All the best,
Chris

Hi Chris, 

Thanks so much! Maybe it's because everything I've touch for about a week and a half has gone crazy... I'm really close with this but obvious did something wrong: 
I need to rows for content display. Top row is the stallions name/link. Bottom row is price, status, etc.
Am I making this harder than it has to be? :) 

Also, Is there any way possible to change the background cell color if a Stallion's status is marked as "new'?  (request from client)

Thanks so much! Tina

<style>
.container { width: 205px; } /* container width should be cols * cell width, but add a few extra pixels for IE -- as long as it's less than (cols+1) * cell width, you're safe! */
.cell      { width: 100px; float: left; }
.clear     { clear: both; }
</style>

<?php foreach ($stallion_listRecords as $record): ?>
<?PHP if ($record['breed'] == "Brandenburg"): ?>
<div class="container">
   <div class="cell">   
      <div><h3>   
         <?php if($record['stallion_url']) : ?> <a href="<?php echo $record['stallion_url']; ?>"><?php echo htmlencode($record['title']); ?></a>
         <?php else : ?> <a href="<?php echo $record['_link']; ?>">
         <?php echo htmlencode($record['title']); ?></a>
         <?php endif; ?> </h3>
      </div>
    <div>   
         <?php echo htmlencode($record['price']) ?>             
         <?php if ($record['status'] == 'New') : ?><img src="http://www.superiorequinesires.com/images/icon-star-new-stallions.png" width="26" height="26" />             
         <?PHP endif ?>             
         <?php if ($record['status'] == 'Special') : ?><img src="http://www.superiorequinesires.com/images/icon-star-new-stallions.png" width="26" height="26" />             
         <?PHP endif ?>             
         <?php echo join(', ', $record['status:labels']); ?> <?php if (!$stallion_listRecords): ?> No records were found!             
         <?php endif ?>
     </div>
   </div>
</div>
<div class="clear"></div>
<?php endif; ?>
<?php endforeach ?>

Oops. Here's the page so you can see the result of that:

http://www.superiorequinesires.com/stallion-list7breeds.php

ABOUT THE STATUS DISPLAY PAGE:  http://www.superiorequinesires.com/stallion-list6status.php

I added the debug code
http://www.superiorequinesires.com/stallion-list6status.php?debug=1

I've attached a screenshot of my status list plus the list6status.php just in case. 

Thanks again for your help! it's much appreciated. 

Hi csdesign,

I think I've found out why your list items have stopped displaying. When you have a single list item, CMS Builder stores the value without any formatting, for example "1" or "In-Stock". But if you have a multi checkbox list, it stores the values in a tab separated list for example "    In-stock     " or "    In-stock     Special    ". So instead of using equal to, you should search for the value instead:

<style>
.container { width: 205px; } /* container width should be cols * cell width, but add a few extra pixels for IE -- as long as it's less than (cols+1) * cell width, you're safe! */
.cell      { width: 100px; float: left; }
.clear     { clear: both; }
</style>

<?php foreach ($stallion_listRecords as $record): ?>
<?PHP if ($record['breed'] == "Brandenburg"): ?> 
<div class="container"> 
   <div class="cell">    
      <div><h3>    
         <?php if($record['stallion_url']) : ?> <a href="<?php echo $record['stallion_url']; ?>"><?php echo htmlencode($record['title']); ?></a> 
         <?php else : ?> <a href="<?php echo $record['_link']; ?>">
         <?php echo htmlencode($record['title']); ?></a> 
         <?php endif; ?> </h3>
      </div> 
    <div>    
         <?php echo htmlencode($record['price']) ?>             
         <?php if (strpos($record['status'],"\tNew\t") !== false) : ?><img src="http://www.superiorequinesires.com/images/icon-star-new-stallions.png" width="26" height="26" />             
         <?PHP endif ?>             
         <?php if (strpos($record['status'],"\tSpecial\t") !== false) : ?><img src="http://www.superiorequinesires.com/images/icon-star-new-stallions.png" width="26" height="26" />             
         <?PHP endif ?>             
         <?php echo join(', ', $record['status:labels']); ?> <?php if (!$stallion_listRecords): ?> No records were found!             
         <?php endif ?> 
     </div> 
   </div> 
</div> 
<div class="clear"></div> 
<?php endif; ?>
<?php endforeach ?>

So the strpos (http://ca2.php.net/strpos) function is used to search a string for the contents of another string. In this case we're searching the $record['status'] field to see if it contains new or special, if it doesn't then the item is skipped.

The \t values ensure that new/special starts and ends with tab spaces, to ensure we only return the value new and not newest, or another value that contains new.

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com