Re: [chris] Sub-categories Tutorial

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

Chris,
I am trying to pull off this category menu thing and followed your instructions but just can't get it to work. I have a category menu set up as regions and a multi record called listings. I created the list pulldown with these settings:

Field Label: City/Town
Field Name: city_town
Field Type: list

Field Options

Display As: pulldown
List Options: Get options from database (advanced)

Section Tablename: regions
Use this field for option values: num
Use this field for option labels: name

I keep getting this error:

Fatal error: Call to undefined function getregion() in /home/gselect/public_html/dev/listing.php on line 38

Here is the code in my page:


<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php

require_once "/home/gselect/public_html/cmsAdmin/lib/viewer_functions.php";

list($listingsRecords, $listingsMetaData) = getRecords(array(
'tableName' => 'listings',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));


beta_lookupRelatedFields(array(
'table' => 'listings',
'recordList' => &$listingsRecords,
'fieldList' => array( 'agent')
));
$listingsRecord = @$listingsRecords[0]; // get first record

// show error message if no matching record is found
if (!$listingsRecord) {
header("HTTP/1.0 404 Not Found");
print "Record not found!";
exit;
}

list($agentsRecords, $agentsMetaData) = getRecords(array(
'tableName' => 'agents',
));


?>
<?php

require_once "/home/gselect/public_html/cmsAdmin/lib/viewer_functions.php";

list($regionsRecords, $selectedCategory) = getRegions(array(
'tableName' => 'regions',
));

list($listingsRecords, $listingsMetaData) = getRecords(array(
'tableName' => 'listings',
));

?>



In the body:

<ul>
<?php foreach ($regionsRecords as $regionsRecord): ?>
<?php echo $categoryRecord['_listItemStart'] ?>

<?php if ($categoryRecord['_isSelected']): ?><b><?php endif ?>
<li><a href="listings.php?category=<?php echo $categoryRecord['num'] ?>"><?php echo $categoryRecord['name'] ?></a> </li>
<?php if ($categoryRecord['_isSelected']): ?></b><?php endif ?>

<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endforeach ?>
</ul>

What am I missing here?

Re: [Dan Maitland] Sub-categories Tutorial

By Chris - March 31, 2010

Hi Dan,

At line 38,

list($regionsRecords, $selectedCategory) = getRegions(array(

... should be:

list($regionsRecords, $selectedCategory) = getCategories(array(

And you'll need to match up your variable names in your body; this:

<?php foreach ($regionsRecords as $regionsRecord): ?>

... should be:

<?php foreach ($regionsRecords as $categoryRecord): ?>

Also, you're overwriting your $listingsRecords variable with that last getRecords call. You'll want to remove this:

list($listingsRecords, $listingsMetaData) = getRecords(array(
'tableName' => 'listings',
));


And finally, I just have to mention that you can get away with using a WHERE clause instead of beta_lookupRelatedFields (since you're only dealing with one record here.) If you wanted to, you could replace this:

beta_lookupRelatedFields(array(
'table' => 'listings',
'recordList' => &$listingsRecords,
'fieldList' => array( 'agent')
));
$listingsRecord = @$listingsRecords[0]; // get first record


// show error message if no matching record is found
if (!$listingsRecord) {
header("HTTP/1.0 404 Not Found");
print "Record not found!";
exit;
}


... with this:

$listingsRecord = @$listingsRecords[0]; // get first record

// show error message if no matching record is found
if (!$listingsRecord) {
header("HTTP/1.0 404 Not Found");
print "Record not found!";
exit;
}

list($listingAgentsRecords, ) = getRecords(array(
'tableName' => 'agents',
'where' => "num = '" . $listingsRecord['agent'] . "'",
));
$listingAgent = @$listingAgentsRecords[0];


Then you could use $listingAgent['my_agent_field'] instead of $listingsRecord['agent']['my_agent_field']. Go ahead and use whichever approach you're more comfortable with.

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

Re: [Dan Maitland] Sub-categories Tutorial

By Chris - April 1, 2010

Hi Dan,

You'll need to change the field name in the URL to match yours. Change this:

<li><a href="region-listings.php?category=<?php echo $categoryRecord['num'] ?>"><?php echo $categoryRecord['name'] ?></a> </li>

... to this:

<li><a href="region-listings.php?city_town=<?php echo $categoryRecord['num'] ?>"><?php echo $categoryRecord['name'] ?></a> </li>

Also, to fix your "Undefined variable" error, you'll probably want to do change this:

<h1><?php echo $listingsRecord['city_town'] ?> Listings</h1>

... to something like this:

<?php if ($selectedCategory): ?>
<h1><?php echo $selectedCategory['city_town'] ?> Listings</h1>
<?php else: ?>
<h1>All Listings</h1>
<?php endif ?>


I hope this helps. Please let me know if you have any questions.
All the best,
Chris
I can't thank you enough for the help that you guys have given me over the past week or so. Exceptional really. I have asked questions in countless forums for several different applications that we use and no one even comes close to the kind of support that you guys give. Ultra impressed.

BUT...LOL, one final question and it's really not a deal breaker whatsoever but is there a way that I can get the name of the region to show up in the URL instead of a number? Like I said it's not a deal breaker but it really would be nice and a bit more SEO friendly. Thanks again.

Re: [Dan Maitland] Sub-categories Tutorial

By Chris - April 1, 2010

Hi Dan,

We're only too happy to help. :)

You can add the name of the region to the URL (but you'll still need the number there). Try replacing this:

<li><a href="region-listings.php?city_town=<?php echo $categoryRecord['num'] ?>"><?php echo $categoryRecord['name'] ?></a> </li>

... with this:

<?php
$name = $categoryRecord['name'];
$name = preg_replace('/[^a-z0-9\.\-\_]+/i', '-', $name); # replace non-url-friendly characters with hyphens
$name = preg_replace("/(^-+|-+$)/", '', $name); # remove leading and trailing hyphens
?>
<li><a href="region-listings.php?name=<?php echo $name ?>&city_town=<?php echo $categoryRecord['num'] ?>"><?php echo $categoryRecord['name'] ?></a> </li>



I hope that helps! Please let us know if you have any questions. :)
All the best,
Chris
Amazing!! That is exactly what I wanted. Thank you very much Chris you have been super helpful. Please tell Dave and Jason thank you for me as they also helped me out with issues I have been having with this project.
Back hopefully one last time. So now on both home page and the generic listing page(where it is showing all the listings The region is showing up as a number. Here is the code for the Listings page:

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php

require_once "/home/gselect/public_html/cmsAdmin/lib/viewer_functions.php";

list($listingsRecords, $listingsMetaData) = getRecords(array(
'tableName' => 'listings',
'perPage' => '6',
'orderBy' => 'featured',
'where' => 'featured = 1',

));

?>
<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php

require_once "/home/gselect/public_html/cmsAdmin/lib/viewer_functions.php";

list($listingsRecords, $listingsMetaData) = getRecords(array(
'tableName' => 'listings',
'perPage' => '6',

));

?>
<?php

require_once "/home/gselect/public_html/cmsAdmin/lib/viewer_functions.php";

list($regionsRecords, $selectedCategory) = getCategories(array(
'tableName' => 'regions',
));


?>



Body code:

<?php foreach ($listingsRecords as $record): ?>
<div class="product2">
<?php foreach ($record['photos'] as $upload): ?>
<div class="product-thumb2">
<div class="<?php echo $record['sold'] ?>"></div>
<a href="<?php echo $record['_link'] ?>"><img src="<?php echo $upload['thumbUrlPath2'] ?>"width="<?php echo $upload['thumbWidth2'] ?>" height="<?php echo $upload['thumbHeight2'] ?>" alt='' title="<?php echo $record['title'] ?><?php echo $record['city_town'] ?>" /></a></div>
<?php break; ?>
<?php endforeach ?>
<div class="product-description2">
<h2><?php echo $record['title'] ?></h2>
<h3><?php echo $record['city_town'] ?> | <?php echo $record['price'] ?> | MLS#: <?php echo $record['mls'] ?></h3><br />
<p><?php echo $record['summary'] ?></p>
</div>
<div class="product-button2">

<a href="<?php echo $record['_link'] ?>"><img src="images/learn-more.jpg" alt="Learn More" /></a> </div>
<div style="clear:both;"></div>
</div>
<?php endforeach ?>
<?php if ($listingsMetaData['invalidPageNum']): ?>

Results page '<?php echo $listingsMetaData['page']?>' not found, <a href="<?php echo $listingsMetaData['firstPageLink'] ?>">start over &gt;&gt;</a><br/>
<br/>
<?php elseif (!$listingsRecords): ?>
<div class="no-results">
Sorry, we currently have no listings in this region!
</div>
<?php endif ?>
<!-- /STEP2: Display Records -->


Remember that on this page it will show all the listings that are entered.

You can see that it is displaying a number instead of the region name.

http://www.gselection.com/dev/listings.php

Re: [Dan Maitland] Sub-categories Tutorial

By Chris - April 5, 2010

Hi Dan,

Firstly, it looks like you're putting records into the same variable ($listingsRecords) twice. You'll probably want to get rid of one of those getRecords() calls.

You could solve this with beta_lookupRelatedFields. Or, since you're already loading all your regions, you could do your own (simple) lookups:

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php

require_once "/home/gselect/public_html/cmsAdmin/lib/viewer_functions.php";

list($listingsRecords, $listingsMetaData) = getRecords(array(
'tableName' => 'listings',
'perPage' => '6',
'orderBy' => 'featured',
'where' => 'featured = 1',
));

list($regionsRecords, $selectedCategory) = getCategories(array(
'tableName' => 'regions',
));

$regionsByNum = array_combine(array_pluck($regionsRecords, 'num'), $regionsRecords);

?>


then replace this:

<h3><?php echo $record['city_town'] ?> | <?php echo $record['price'] ?> | MLS#: <?php echo $record['mls'] ?></h3><br />

with this:

<h3><?php echo $regionsByNum[$record['city_town']]['name'] ?> | <?php echo $record['price'] ?> | MLS#: <?php echo $record['mls'] ?></h3><br />

Does that help? Please let me know if you have any questions.
All the best,
Chris
Boy-O-Boy am I struggling with this. I never would have imagined that pulling a name from another table would be so tough. Ok so every solution you gave me has worked but I always end up with the same problem but on another page. This is the last page that is not displaying the region correctly so there really shouldn't be anymore questions left after this one.

Ok so every page is now showing the correct region except the actual listing page. You can view it here:

http://www.gselection.com/dev/listing.php?353-Querbes-308-2

Where it says "N.D.I.P" it should be pulling "Vaudreuil-Dorion" which is the correct region. Here is the code that's on this page:

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php

require_once "/home/gselect/public_html/cmsAdmin/lib/viewer_functions.php";

list($listingsRecords, $listingsMetaData) = getRecords(array(
'tableName' => 'listings',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));


beta_lookupRelatedFields(array(
'table' => 'listings',
'recordList' => &$listingsRecords,
'fieldList' => array( 'agent')
));
$listingsRecord = @$listingsRecords[0]; // get first record

// show error message if no matching record is found
if (!$listingsRecord) {
header("HTTP/1.0 404 Not Found");
print "Record not found!";
exit;
}

list($agentsRecords, $agentsMetaData) = getRecords(array(
'tableName' => 'agents',
));


?>
<?php

require_once "/home/gselect/public_html/cmsAdmin/lib/viewer_functions.php";

list($regionsRecords, $selectedCategory) = getCategories(array(
'tableName' => 'regions',
));

$regionsByNum = array_combine(array_pluck($regionsRecords, 'num'), $regionsRecords);

?>

Body code:

<h1><?php echo $listingsRecord['title'] ?></h1>
<div class="subtitle"><?php if ($selectedCategory): ?><?php echo $selectedCategory['name'] ?><?php endif ?>, Quebec | MLS#: <?php echo $listingsRecord['mls'] ?> | $<?php echo $listingsRecord['price'] ?><br />
Bedrooms: <?php echo $listingsRecord['bedrooms'] ?> | Bathrooms: <?php echo $listingsRecord['bathrooms'] ?>
</div>

Honestly Chris this is the last page that is not displaying the region correctly. I'm confident that you will know what is going on once to review the code. Thanks again.