Manipulating How single & list of records are displayed

3 posts by 2 authors in: Forums > CMS Builder
Last Post: November 30, 2009   (RSS)

By Mel - November 28, 2009

Hi,

I have a table name bios, this is the master table for all records

To display the record list for a specific location lets say Manchester I use the

list($biosRecords, $biosDetails) = getRecords(array(
'tableName' => 'bios',
'perPage' => '10',
'where' => ' areas_covered LIKE "%manchester%" ',
'orderBy' => 'view_order',

));

to display what parts of the master record I want to show I use this and add the fields that I want to show

<?php foreach ($biosRecords as $biosRecord): ?>
<?php echo $biosRecord['name'] ?>
etc
etc
etc
<?php endforeach ?>

and much the same set up for a single record page which works fine

What I want to do is this:

On both the Complete Record list for Manchester and the Details Page for individual records, I want to leave out certain fields in the display if a record meets a certain condition

So say I add a new field called area1 to the master table is it then possible to do an elseif to say if area1=y then display the record using this layout instead (certain fields would be omitted) ?

Thanks - Mel

Re: [mel] Manipulating How single & list of records are displayed

By gkornbluth - November 29, 2009

Me''s question about the correct use of the “if” statement to test for specific conditions keeps resurfacing.

I hope that this excerpt from the CMSB Cookbook clears up some of the confusion.

UNDERSTANDING AND USING IF STATEMENTS
The if statement is one of the most powerful and valuable operators that you can use when designing your pages. You can test for many different scenarios and have your page output (or not output) specific code, depending on the outcome of your tests.

The problem is that many times, if you’ve made a mistake in your coding, you’ll get a blank page displayed in your browser and have no idea what your mistake was.

So here’s a short primer on the correct way to use this powerful tool.

The first rule to remember is that every (series of) if statement(s) must be closed with an <?php endif ?> in order for them to work correctly.

The second rule is that if you’re using a series of if statements, then the first one is always an “if”, the last is always “else” and any in the middle are “elseif”.

So, if you’re using only one if statement to test for a particular condition. Say to test whether the field “your_field” is empty, then you’d use:

In a single record viewer:

<?php if ($your_tableRecord['your_field']): ?> ...your code...
<?php endif ?>


Or in a multi record viewer:

<?php foreach ($your_tableRecords as $record): ?>
<?php if ($record['your_field']): ?> ...your code...
<?php endif ?>
<?php endforeach; ?>


You can also put an if statement around a block of text to keep it from being run if there are no records available.

<?php if ($your_tableRecord['your_field']): ?>
<?php foreach ($your_tableRecord['your_field'] as $upload): ?>

... code to be run...

<?php endforeach ?>
<?php endif ?>


If you wanted to test for particular contents in “your_field”.

Say, if the contents of “your_field” equaled “apples” then, you’d use the exactly equal to “==” operator:

In a single record viewer:

<?php if ($your_tableRecord['your_field'] == 'apples'): ?> ...your code...
<?php endif ?>


Or in a multi record viewer:

<?php foreach ($your_tableRecords as $record): ?>
<?php if ($record['your_field'] == 'apples'): ?> ...your code...
<?php endif ?>
<?php endforeach; ?>



If you wanted to test for the absence of particular contents in “your_field”.

Say, if the contents of “your_field” was anything other than “apples” then, you can use the not operator “!” :

In a single record viewer:

<?php if (!$your_tableRecord['your_field'] == 'apples'): ?> ...your code...
<?php endif ?>


Or in a multi record viewer:

<?php foreach ($your_tableRecords as $record): ?>
<?php if (!$record['your_field'] == 'apples'): ?> ...your code...
<?php endif ?>
<?php endforeach; ?>


To use a series of if statements to test for a series of condition, then you’d use:

in a detail viewer:

<?php if ($your_tableRecord['your_field'] == 'apples'): ?> ...your field contains apples...
<?php elseif ($your_tableRecord['your_field'] == 'bananas'): ?> ...your field contains bananas...
<?php else: ?> ...your code...
<?php endif ?>


And in a list viewer:

<?php foreach ($your_tableRecords as $record): ?>
<?php if ($record['your_field'] == 'apples'): ?> ...your field contains apples...
<?php elseif ($record['your_field'] == 'bananas'): ?> ...your field contains bananas...
<?php else: ?> ...your code...
<?php endif ?>
<?php endforeach; ?>



You’re not limited to a single or a single field condition either.

To test for multiple conditions, you can use:

(Note: The “!” works in this too)

<?php if ($your_tableRecord['field_a'] == 'apples' && $your_tableRecord['field_b'] == 'bananas' && $your_tableRecord['field_c'] == 'pears'): ?> ...Your record has all three...
<?php endif ?>


Or

<?php foreach ($testRecords as $record): ?>
<?php if ($record['field_a'] == 'apples' && $record['field_b'] == 'bananas' && $record['field_c'] == 'pears'): ?> ...Your record has all three...
<?php endif ?>
<?php endforeach; ?>


If you wanted to test for three conditions in a list viewer record.

Say, “field_a” empty, “field_b” empty and “field_c” not empty, you could use:

<?php if (!$record['field_a'] && !$record['field_b'] && $record['field_c']): ?> ...your code...
<?php endif ?>


This is by far not a comprehensive list of the possibilities for using if statements, but it should be enough to get you started.



USING IF STATEMENTS TO DEFINE OUTPUT BASED ON A FILE EXTENSION
If you need to publish specific code that’s based on the type of file that you’ve uploaded into an upload field, like a .swf flash file, here’s the trick. Just use:

<?php if ($upload['extension'] == 'swf'): ?>
or <?php elseif ($upload['extension'] == 'swf'): ?>

You can use this for non upload fields as well:

<?php if ($your_fieldRecord['extension'] == 'swf'): ?>
or
<?php elseif ($your_fieldRecord['extension'] == 'swf'): ?>

Don’t forget that every (series of) if statement(s) must be closed with an <?php endif ?>



IF STATEMENTS THAT MEET MORE THAN ONE CONDITION
If you’ve got a table called “people and two fields “price” and “description” and you need a specific result depending on whether there’s information in one, the other or both fields, you can approach it this way. Remember that the operator “!” means not so !$record['price'] means that there’s no information in the price field.

<!-- If there’s no price and no description, just show the title. –>
<?PHP foreach ($peopleRecords as $record): ?>
<?PHP if (!$record['price'] && !$record['l_description']): ?><?PHP echo $record['title'] ?><?PHP endif ?>

<!-- If there’s no price, just a description , offer information only. –>
<?PHP if (!$record['print_price'] && $record['full_description']): ?><a href=”<?PHP echo $record['_link'] ?>”><?PHP echo $record['title'] ?></a><br>Click the title for more information.<?PHP endif ?>">

<!-- If there’s a price and no description, ask for the sale. –>
<?PHP if ($record['price'] && !$record['description']): ?><a href=”<?PHP echo $record['_link'] ?>”><?PHP echo $record['title'] ?></a>Click the title to buy.<?PHP endif ?>

<!-- If there’s a price and a description, offer information and ask for the sale. –>
<?PHP if ($record['price'] && $record[description']): ?><a href=”<?PHP echo $record['_link'] ?>”><?PHP echo $record['title'] ?></a>Click the title the for more information or to buy.<?PHP endif ?>
<?PHP endforeach ?>

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Manipulating How single & list of records are displayed

By Mel - November 30, 2009

Hi Jerry,

Thanks for the comprehensive reply, got it doing what I want it to do.

Will be getting the book[:)]