Dynamic Menu creation

3 posts by 2 authors in: Forums > CMS Builder
Last Post: July 2, 2011   (RSS)

By willydoit - June 30, 2011 - edited: July 1, 2011

Hi,

I apologise if this is very basic stuff but I have searched the forum, purchased the cms cookbook and still canot get to grips with what I want to do which is as follows;

I have a simple section in CMSB which is called products designed to hold content for advertising items, this contains a field called "item" which is a drop down list of potential categories that an advertiser can place their item for sale in, ie tractor, car, caravan etc.

What I want to do is to use this list of items to create a dynamic menu which will a) show the number of listings in that category and b) provide a hyperlink which will refresh the page showing only the items for sale in that category.

I expect the menu will look something like this;

4x4's [2] (hyperlink something like http://www.findatractor.co.uk/listings.php?item=4X4's or /listings.php?item=4X4's[/#336699])
Access Platforms [5]
ATV's [9]
Bale Handlers [1]
Bale Sledges [2]
Bale Spikes [0]

I have tried various things based on snippets of info found in the forums and the cms cookbook but just end up totally confused, I did manage to get something listed but it only listed categories which had a value where as I would prefer it to list all categories regardless of whether it contains any items for sale and for some reason it only displayed the first character of the field name and I hadnt even started on how to generate the links.[unsure]

I have tried to set up a menu_list field as a relatedRecord field selecting products as the related table assuming I can craete a new field for the menu_list containing all the items which are in the item field of the same table. Unfortunately this presented me with the Mysql where box and the More "Search" Link box and I hadnt a clue where to go from there, didnt even know whether I needed to amend the content in those boxes or just copy them to add to my listing page somewhere such is my level of knowledge of php and mysql.

I have copied the generated list code below in case it helps (you can ignore the date fields for now as they were created for something later on in the project) but any help with something that I thought would be pretty simple would be appreciated. At this moment I am back to starting from scratch so any help provided should start right at the begining I think and assume nothing, especially knowledge.[crazy]

Any help on this before the weekend would be appreciated if someone can spare the time.

Thanks in advance.

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


// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/home/sites/findatractor.co.uk/public_html/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load records
list($productsRecords, $productsMetaData) = getRecords(array(
'tableName' => 'products',
'orderBy' => 'RAND()',
));

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<style type="text/css">
body { font-family: arial; }
.instructions { border: 3px solid #000; background-color: #EEE; padding: 10px; text-align: left; margin: 25px}
</style>
</head>
<body>

<!-- INSTRUCTIONS -->
<div class="instructions">
<b>Sample List Viewer - Instructions:</b>
<ol>
<?php ?>
<li><b>Remove any fields you don't want displayed.</b> (Most list pages only have title and link fields.)</li>
<li>Rearrange remaining fields to suit your needs.</li>
<li>Copy and paste code into previously designed page (or add design to this page).</li>
</ol>
</div>
<!-- /INSTRUCTIONS -->

<!-- STEP2: Display Records (Paste this where you want your records to be listed) -->
<h1>Products - List Page Viewer</h1>
<?php foreach ($productsRecords as $record): ?>
Record Number: <?php echo $record['num'] ?><br/>
Start Date: <?php echo date("D, M jS, Y g:i:s a", strtotime($record['start_date'])) ?><br/>
<!-- For date formatting codes see: http://www.php.net/date -->
Renewal Date: <?php echo date("D, M jS, Y g:i:s a", strtotime($record['renewal_date'])) ?><br/>
<!-- For date formatting codes see: http://www.php.net/date -->
Expiry Date: <?php echo date("D, M jS, Y g:i:s a", strtotime($record['expiry_date'])) ?><br/>
<!-- For date formatting codes see: http://www.php.net/date -->
Contact Name: <?php echo $record['contact_name'] ?><br/>
Contact Telephone: <?php echo $record['contact_telephone'] ?><br/>
Contact Mobile: <?php echo $record['contact_mobile'] ?><br/>
Price: <?php echo $record['price'] ?><br/>
Location: <?php echo $record['location'] ?><br/>
Item: <?php echo $record['item'] ?><br/>
Summary: <?php echo $record['summary'] ?><br/>
Description: <?php echo $record['description'] ?><br/>
Views: <?php echo $record['views'] ?><br/>
_link : <a href="<?php echo $record['_link'] ?>"><?php echo $record['_link'] ?></a><br/>


<!-- STEP 2a: Display Uploads for field 'images' (Paste this anywhere inside STEP2 to display uploads) -->
<!-- Upload Fields: num, createdTime, tableName, fieldName, recordNum, preSaveTempId, filePath, filename, extension, thumbFilePath, isImage, hasThumbnail, urlPath, width, height, thumbUrlPath, thumbWidth, thumbHeight, info1, info2, info3, info4, info5 -->
<?php foreach ($record['images'] as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" /><br/>

<?php elseif ($upload['isImage']): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="" /><br/>

<?php else: ?>
<a href="<?php echo $upload['urlPath'] ?>">Download <?php echo $upload['filename'] ?></a><br/>

<?php endif ?>
<?php endforeach ?>
<!-- STEP2a: /Display Uploads -->


<hr/>
<?php endforeach ?>

<?php if (!$productsRecords): ?>
No records were found!<br/><br/>
<?php endif ?>
<!-- /STEP2: Display Records -->

</body>
</html>

Re: [willydoit] Dynamic Menu creation

By Djulia - July 2, 2011

Hi,

There is certainly several approach possible, but if you use only one table :
for a, you can use the function "mysql_select_count_from" ;
for b, you can use conditions ($where).

Djulia