If statement

16 posts by 4 authors in: Forums > CMS Builder
Last Post: June 7, 2010   (RSS)

By design9 - March 16, 2010

Hello, I have a marketplace section where I list various advertisers and show if they have a featured ad, coupon or classified. The problem I am having is getting all the types of ads to show up on the details page. When the user goes to the main page , I have the table that shows the advertisers name and what they offer. When you click on the advertiser name it goes to the details page where I want all the types of ads they offer to appear. Example, on my main page, the advertiser Lake Norman Dance has a featured ad as well as a classified ad but when you go to the details page, the wrong classified ad is showing up. I have tried writing an if statement but I am missing something because it is not pulling in the ads only for that advertiser. I have three sections: a featured ad section, a coupon section and a classified section. I have a checkbox in each section that I am using with the if statement. Here are the pages below to see the source code.

Here is my main page: http://site471.mysite4now.com/charlottepar/test/marketplace/index.php

Here is my details page: http://site471.mysite4now.com/charlottepar/test/marketplace/detailsfa.php

Thank you,

April

Re: [apdance9] If statement

By gkornbluth - March 16, 2010

Hi April,

Could you attach your list and detail viewers

The live pages don't show your code.

No personal information like user names and passwords on the Forum please.

Thanks,

Jerry Kornbluth
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] If statement

By design9 - March 17, 2010

Sorry...not thinking.

Here is my main page: [/#000000][url "http://www.interactivetools.com/forum/forum.cgi?url=http%3A%2F%2Fsite471.mysite4now.com%2Fcharlottepar%2Ftest%2Fmarketplace%2Findex.php"]http://site471.mysite4now.com/charlottepar/test/marketplace/index.php[/#003366][/url] (Chris orginally helped me with this coding, this page works great)[/#000000]

Code:[/#000000]

<?php
require_once "my hosting information";

list($categoryRecords, $categoryMetaData) = getRecords(array(
'tableName' => 'category',
));
[/#000000]

list($marketplaceRecords, $marketplaceMetaData) = getRecords(array(
'tableName' => 'marketplace',
));
[/#000000]

list($marketplace_couponsRecords, $marketplace_couponsMetaData) = getRecords(array(
'tableName' => 'marketplace_coupons',
));
[/#000000]

list($marketplace_classifiedsRecords, $marketplace_classifiedsMetaData) = getRecords(array(
'tableName' => 'marketplace_classifieds',
));
[/#000000]

// a collection of our records
$recordSets = array(
'marketplace' => $marketplaceRecords,
'coupon' => $marketplace_couponsRecords,
'classified' => $marketplace_classifiedsRecords,
);

// index item records in recordSets by their titles
$rowsByTitle = array();
//foreach ( $recordSets as $recordSetName => &$records ) {
foreach ( array_keys( $recordSets ) as $recordSetName ) {
$records =& $recordSets[$recordSetName];

//foreach ( $records as &$record ) {
foreach ( array_keys($records) as $key ) {
$record =& $records[$key];

// skip processing records without a title
if ( strlen($record['title']) === 0 ) { continue; }

// lookup an existing row by this record's title or create a new one
if ( !array_key_exists($record['title'], $rowsByTitle) ) {
$rowsByTitle[ $record['title'] ] = array();
$rowsByTitle[ $record['title'] ][ 'title' ] =& $record['title'];
$rowsByTitle[ $record['title'] ][ '_link' ] =& $record['_link'];
$rowsByTitle[ $record['title'] ][ 'cats' ] = array();
}
$row =& $rowsByTitle[ $record['title'] ];

// add this record's cats to the row's existing cats
if ( $record['cats'] !== "" ) {
$recordCats = explode("\t", trim($record['cats'], "\t"));
$row['cats'] = array_unique(array_merge( $row['cats'], $recordCats ));
}

// create row-to-record reference keyed on recordSetName
$row[ $recordSetName ] =& $record;
}
}

// index categoryRecords by name
$categoryRecordsByTitle = array();
//foreach ($categoryRecords as &$category) {
foreach ( array_keys($categoryRecords) as $key ) {
$category =& $categoryRecords[$key];
$category['rows'] = array();
$categoryRecordsByTitle[ $category['name'] ] =& $category;
}

// insert rows into categories
//foreach ( $rowsByTitle as &$row ) {
foreach ( array_keys($rowsByTitle) as $rowTitle ) {
$row =& $rowsByTitle[$rowTitle];

//foreach ( $row['cats'] as &$categoryName ) {
foreach ( array_keys($row['cats']) as $index ) {
$categoryName =& $row['cats'][$index];

$categoryRecordsByTitle[ $categoryName ][ 'rows' ][] =& $row;
}
}
// strip categories with no rows
function testCategoryHasRows($category) {
return (count($category['rows']) > 0);
}
$nonEmptyCategories = array_filter($categoryRecordsByTitle, 'testCategoryHasRows');
[/#000000]

?>[/#000000][/#000000]


[/#000000]<?php //foreach ($nonEmptyCategories as $category): ?>
<?php foreach ( array_keys($nonEmptyCategories) as $categoryKey ): ?>
<?php $category =& $nonEmptyCategories[$categoryKey]; ?>
<?php echo $category['name'] ?>

<?php //foreach ($category['rows'] as $row): ?>
<?php foreach ( array_keys($category['rows']) as $rowIndex ): ?>
<?php $row =& $category['rows'][$rowIndex]; ?>

<a href="<?php echo $row['_link']; ?>"><?php echo $row['title'] ?></a>
<?php if (array_key_exists('marketplace', $row) && $row['marketplace']['featured_ad']): ?><img src="../images/check.png" width="27" height="23" /><?php endif ?>

<?php if (array_key_exists('classified', $row) && $row['classified']['featured_classified']): ?><img src="../images/check.png" width="27" height="23" /><?php endif ?>

<?php if (array_key_exists('coupon', $row) && $row['coupon']['featured_coupon']): ?><img src="../images/check.png" width="27" height="23" /><?php endif ?>

<?php endforeach; ?>
<?php endforeach; ?>

<?php $mergedArray = array_merge_recursive($marketplaceRecords, $marketplace_couponsRecords, $marketplace_classifiedsRecords); ?>
<?php if (!$mergedArray): ?>No records were found!<br />
<?php endif ?>

[/#000000]
Here is my details page:
[/#000000][url "http://www.interactivetools.com/forum/forum.cgi?url=http%3A%2F%2Fsite471.mysite4now.com%2Fcharlottepar%2Ftest%2Fmarketplace%2Fdetailsfa.php"]http://site471.mysite4now.com/charlottepar/test/marketplace/detailsfa.php[/#003366][/url] (this is the page I am having the trouble) [/#000000]

<?php
require_once "my hosting information";

list($marketplaceRecords, $marketplaceMetaData) = getRecords(array(
'tableName' => 'marketplace',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$marketplaceRecord = @$marketplaceRecords[0]; // get first record

list($marketplace_couponsRecords, $marketplace_couponsMetaData) = getRecords(array(
'tableName' => 'marketplace_coupons',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$marketplace_couponsRecord = @$marketplace_couponsRecords[0]; // get first record

list($marketplace_classifiedsRecords, $marketplace_classifiedsMetaData) = getRecords(array(
'tableName' => 'marketplace_classifieds',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$marketplace_classifiedsRecord = @$marketplace_classifiedsRecords[0]; // get first record
[/#000000]

list($categoryRecords, $selectedCategory) = getCategories(array(
'tableName' => 'category',
'selectedCategoryNum' => '', // defaults to getNumberFromEndOfUrl()
'categoryFormat' => 'showall', // showall, onelevel, twolevel
));

?>
[/#000000]

[/#000000]

<a href="#"><?php echo $marketplaceRecord['title'] ?></a>
[/#000000]

<?php if ($marketplaceRecord['featured_ad']): ?><?php echo $marketplaceRecord['feat'] ?><br /><?php endif ?>

<?php if ($marketplace_couponsRecord['featured_coupon']): ?>
Expires:</span> <?php echo $marketplace_couponsRecord['couponexpiration_date'] ?><br /><br>
<?php echo $marketplace_couponsRecord['coucode'] ?><br /><?php endif ?>
[/#000000][/#000000]

<?php if ($marketplace_classifiedsRecord['featured_classified']): ?><?php echo $marketplace_classifiedsRecord['content'] ?><br /><?php endif ?>


<?php if (!$marketplaceRecords): ?>No records were found!<br/>
<?php endif ?>
[/#000000]

[/#000000]

Re: [apdance9] If statement

By gkornbluth - March 17, 2010 - edited: March 17, 2010

April,

This is such a stab in the dark...

Assuming that you’re (possibly) using a checkbox in your Editor, when I test to see if a checkbox is checked, I find that I have to use <?php if ($marketplaceRecord['featured_ad'] == “1"): ?> to get the correct result. (actually I'm not sure if you need the " around the 1 or not.)

I’ve also sometimes found issues with one or both of these lines when pulling up records other than record # 1

'where' => whereRecordNumberInUrl(1), and
'limit' => '1',

Maybe???

Or maybe Chris will have to come to the rescue once again.

Best,

Jerry Kornbluth
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] If statement

By Chris - March 17, 2010

Hi apdance9,

I remember your very tricky configuration: you're associating records which share the same "title" field value.

To get detail pages working, you'll need to do two things.

(1) Use links which specify the "title". In index.php, change this line:

$rowsByTitle[ $record['title'] ][ '_link' ] =& $record['_link'];

to this:

$rowsByTitle[ $record['title'] ][ '_link' ] =& "detailsfa.php?title=" . urlencode($record['title']);

(2) Change your detail page to look up records using the title. Actually, it'll automatically do that already using the URL (title=), but you'll need to remove the whereRecordNumberInUrl filtering. Remove all three of these lines:

'where' => whereRecordNumberInUrl(1),

I hope this helps! Please let me know if you have any questions.
All the best,
Chris

Re: [gkornbluth] If statement

By design9 - March 19, 2010

Thank you both for your help!

Chris,
I want to just clarify so I make sure I understand correctly...I just need to put this coding/changes on my details page? Do I need to change any of the coding on the main page?

Thanks!
April

Re: [apdance9] If statement

By Chris - March 19, 2010

Hi April,

The (1) above is a change you'll need to make for your main page. It'll change your detail links from this:

detailscoupon.php?PetsMart-1

into this:

detailscoupon.php?title=PetsMart

Does that help? Please let me know if you have any questions.
All the best,
Chris

Re: [chris] If statement

By design9 - March 22, 2010

Thanks Chris!

When I change the coding on the main page to:

$rowsByTitle[ $record['title'] ][ '_link' ] =& "detailsfa.php?title=" . urlencode($record['title']);

I get the following error:

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting T_NEW or T_STRING or T_VARIABLE or '$' in D:\hosting\member\charlottepar\test\marketplace\index.php on line 44

Thanks!

April

Re: [apdance9] If statement

By Chris - March 22, 2010 - edited: March 22, 2010

Hi April,

Can you try this instead:

$link = "detailsfa.php?title=" . urlencode($record['title']);
$rowsByTitle[ $record['title'] ][ '_link' ] =& $link;


Please let me know if that fixes things. :)
All the best,
Chris