Pull Multilist into page via database list??
7 posts by 3 authors in: Forums > CMS Builder
Last Post: April 12, 2011 (RSS)
By (Deleted User) - September 23, 2010
I have made a multi record list for some site promos. I have included a small image, some text and title and a link.
I would like to include the promos in three boxes at the bottom of each page and to be able to choose which ones I include where.
Here is the basic promo as a multi-record list:
<?php foreach ($site_promosRecords as $record): ?>
<h3><?php echo $record['title'] ?></h3>
<a href="<?php echo $record['link'] ?>" title="<?php echo $record['link_text'] ?>" ><img src="<?php echo $record['image_url'] ?>" width="60" height="45" alt="<?php echo $record['link_text'] ?>" /></a>
<p><?php echo $record['content'] ?><br /><a href="<?php echo $record['link'] ?>" class="promo-link" title="<?php echo $record['link_text'] ?>"><?php echo $record['link_text'] ?></a></p>
<?php endforeach ?>
In each page across the site I have 3 pulldown lists for left_promo, middle_promo and right_promo. The pulldown gets options from the database.
Section Tablename: site_promos
Use this field for option values: num
Use this field for option labels: title.
How do I include the item selected in each page and if nothing has been selected to randomly choose one making sure it isn't the same as promo_middle and promo_right selection??
Any help would be great.
Thanks Jono
Re: [jonoc73] Pull Multilist into page via database list??
By Jason - September 23, 2010
Here is an example of how you can select the left promo record.
In this example, it's assuming your using the variable $record to output the content of your page.
Try this:
if($record['left_promo']){
list($left_promoRecord,)=getRecords(array(
'tableName' => 'site_promos',
'allowSearch' => false,
'limit' => 1,
'where' => "num=".intval($record['left_promo']),
));
}
else{
list($left_promoRecord,)=getRecords(array(
'tableName' => 'site_promos',
'allowSearch' => false,
'limit' => 1,
'orderBy' => 'RAND()',
'where' => "num!=".intval($record['right_promo'])." AND num!=".intval($record['middle_promo']),
));
}
You can use similar code to select the right and middle promo records. You may need to change some names to match what you have in the database.
Hope this helps.
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] Pull Multilist into page via database list??
By (Deleted User) - September 23, 2010
Thanks for trying to help. I am getting errors at the very top of the page:
Notice: Undefined variable: record in /home/yourvita/public_html/index.php on line 16 Notice: Undefined variable: record in /home/yourvita/public_html/index.php on line 28 Notice: Undefined variable: record in /home/yourvita/public_html/index.php on line 28 Notice: Undefined variable: record in /home/yourvita/public_html/index.php on line 29 Notice: Undefined variable: record in /home/yourvita/public_html/index.php on line 41 Notice: Undefined variable: record in /home/yourvita/public_html/index.php on line 41 Notice: Undefined variable: record in /home/yourvita/public_html/index.php on line 42 Notice: Undefined variable: record in /home/yourvita/public_html/index.php on line 54 Notice: Undefined variable: record in /home/yourvita/public_html/index.php on line 54
Here is what's at the top of the pages php:
<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/home/yourvita/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."); }
list($newsRecords, $newsMetaData) = getRecords(array(
'tableName' => 'news',
'limit' => '2',));
list($homepageRecords, $homepageMetaData) = getRecords(array(
'tableName' => 'homepage',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',));
list($sliding_promosRecords, $sliding_promosMetaData) = getRecords(array(
'tableName' => 'sliding_promos',
'loadUploads' => '0',));
if($record['left_promo']){
list($left_promoRecord,)=getRecords(array(
'tableName' => 'site_promos',
'allowSearch' => false,
'limit' => 1,
'where' => "num=".intval($record['left_promo']), ));}
else{
list($left_promoRecord,)=getRecords(array(
'tableName' => 'site_promos',
'allowSearch' => false,
'limit' => 1,
'orderBy' => 'RAND()',
'where' => "num!=".intval($record['right_promo'])." AND num!=".intval($record['middle_promo']),));}
if($record['middle_promo']){
list($left_promoRecord,)=getRecords(array(
'tableName' => 'site_promos',
'allowSearch' => false,
'limit' => 1,
'where' => "num=".intval($record['middle_promo']), ));}
else{
list($middle_promoRecord,)=getRecords(array(
'tableName' => 'site_promos',
'allowSearch' => false,
'limit' => 1,
'orderBy' => 'RAND()',
'where' => "num!=".intval($record['left_promo'])." AND num!=".intval($record['right_promo']),));}
if($record['right_promo']){
list($right_promoRecord,)=getRecords(array(
'tableName' => 'site_promos',
'allowSearch' => false,
'limit' => 1,
'where' => "num=".intval($record['right_promo']), ));}
else{
list($right_promoRecord,)=getRecords(array(
'tableName' => 'site_promos',
'allowSearch' => false,
'limit' => 1,
'orderBy' => 'RAND()',
'where' => "num!=".intval($record['left_promo'])." AND num!=".intval($record['middle_promo']),));}
$homepageRecord = @$homepageRecords[0];?>
And here's the three promo boxes:
<div class="span-24"><div class="hrbg_underintro"></div>
<div class="span-7 colborder">
<div class="box1">
<?php foreach ($left_promoRecord as $record): ?>
<h3><?php echo $record['title'] ?></h3>
<a href="<?php echo $record['link'] ?>" title="<?php echo $record['link_text'] ?>" ><img src="<?php echo $record['image_url'] ?>" width="60" height="45" alt="<?php echo $record['link_text'] ?>" /></a>
<p><?php echo $record['content'] ?><br /><a href="<?php echo $record['link'] ?>" class="promo-link" title="<?php echo $record['link_text'] ?>"><?php echo $record['link_text'] ?></a></p>
<?php endforeach ?>
</div>
</div>
<div class="span-7 colborder">
<div class="box1">
<?php foreach ($middle_promoRecord as $record): ?>
<h3><?php echo $record['title'] ?></h3>
<a href="<?php echo $record['link'] ?>" title="<?php echo $record['link_text'] ?>" ><img src="<?php echo $record['image_url'] ?>" width="60" height="45" alt="<?php echo $record['link_text'] ?>" /></a>
<p><?php echo $record['content'] ?><br /><a href="<?php echo $record['link'] ?>" class="promo-link" title="<?php echo $record['link_text'] ?>"><?php echo $record['link_text'] ?></a></p>
<?php endforeach ?>
</div>
</div>
<div class="span-7 last">
<div class="box1">
<?php foreach ($right_promoRecord as $record): ?>
<h3><?php echo $record['title'] ?></h3>
<a href="<?php echo $record['link'] ?>" title="<?php echo $record['link_text'] ?>" ><img src="<?php echo $record['image_url'] ?>" width="60" height="45" alt="<?php echo $record['link_text'] ?>" /></a>
<p><?php echo $record['content'] ?><br /><a href="<?php echo $record['link'] ?>" class="promo-link" title="<?php echo $record['link_text'] ?>"><?php echo $record['link_text'] ?></a></p>
<?php endforeach ?>
</div>
</div>
</div>
I'm sure I have a little tiny error that is throwing it all out!!!
Any ideas? Thanks Jono
Re: [jonoc73] Pull Multilist into page via database list??
By Chris - September 23, 2010
You'll want to move this line above all the IFs you just added:
$homepageRecord = @$homepageRecords[0];
Then, you'll want to replace $record in the code that Jason gave you with $homepageRecord.
Does that help? Please let me know if you have any questions.
Chris
Re: [Chris] Pull Multilist into page via database list??
By (Deleted User) - April 9, 2011
Any ideas?
Thanks Jono
Re: [jonoc73] Pull Multilist into page via database list??
By Jason - April 11, 2011
Could you please attach the .php file you're working with so we can see the latest version of the code you're using?
Thanks
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [jonoc73] Pull Multilist into page via database list??
By Jason - April 12, 2011
From the look of your code you need to maintain an array of promo records that have been selected and in each of your queries, make sure none of those records get selected again. In your code where you're selecting your left, middle, and right promos, try this:
$selectedPromos = array(0);
if($homepageRecord['left_promo']){
list($left_promoRecord,)=getRecords(array(
'tableName' => 'site_promos',
'allowSearch' => false,
'limit' => 1,
'where' => "num=".intval($homepageRecord['left_promo']),
));
$selectedPromos = array_merge($selectedPromos, array_pluck($left_promoRecord, 'num'));
}
else{
list($left_promoRecord,)=getRecords(array(
'tableName' => 'site_promos',
'allowSearch' => false,
'limit' => 1,
'orderBy' => 'RAND()',
'where' => "num!=".intval($homepageRecord['right_promo'])." AND num!=".intval($homepageRecord['middle_promo']),
));
$selectedPromos = array_merge($selectedPromos, array_pluck($left_promoRecord, 'num'));
}
if($homepageRecord['middle_promo']){
list($middle_promoRecord,)=getRecords(array(
'tableName' => 'site_promos',
'allowSearch' => false,
'limit' => 1,
'where' => "num=".intval($homepageRecord['middle_promo']). " AND num NOT IN (". join(",", $selectedPromos) .") AND num NOT IN (". join(",", $selectedPromos) .")",
));
$selectedPromos = array_merge($selectedPromos, array_pluck($middle_promoRecord, 'num'));
}
else{
list($middle_promoRecord,)=getRecords(array(
'tableName' => 'site_promos',
'allowSearch' => false,
'limit' => 1,
'orderBy' => 'RAND()',
'where' => "num!=".intval($homepageRecord['left_promo'])." AND num!=".intval($homepageRecord['right_promo']). " AND num NOT IN (". join(",", $selectedPromos) .")",
));
$selectedPromos = array_merge($selectedPromos, array_pluck($middle_promoRecord, 'num'));
}
if($homepageRecord['right_promo']){
list($right_promoRecord,)=getRecords(array(
'tableName' => 'site_promos',
'allowSearch' => false,
'limit' => 1,
'where' => "num=".intval($homepageRecord['right_promo']). " AND num NOT IN (". join(",", $selectedPromos) .")",
));
$selectedPromos = array_merge($selectedPromos, array_pluck($right_promoRecord, 'num'));
}
else{
list($right_promoRecord,)=getRecords(array(
'tableName' => 'site_promos',
'allowSearch' => false,
'limit' => 1,
'orderBy' => 'RAND()',
'where' => "num!=".intval($homepageRecord['left_promo'])." AND num!=".intval($homepageRecord['middle_promo']). " AND num NOT IN (". join(",", $selectedPromos) .")",
));
$selectedPromos = array_merge($selectedPromos, array_pluck($middle_promoRecord, 'num'));
}
Hope this helps
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/