Pull Multilist into page via database list??

7 posts by 3 authors in: Forums > CMS Builder
Last Post: April 12, 2011   (RSS)

Re: [jonoc73] Pull Multilist into page via database list??

By Jason - September 23, 2010

Hi,

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

Hi Jason,
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

Hi Jono,

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.
All the best,
Chris

Re: [Chris] Pull Multilist into page via database list??

By (Deleted User) - April 9, 2011

This code works great. However I have noticed on some pages where none of the promos have been chosen, although they randomly display - sometimes two of them are the same! Can I set it so there are never the same promos on on page - there will always be more then 3 promos by the way.

Any ideas?
Thanks Jono

Re: [jonoc73] Pull Multilist into page via database list??

By Jason - April 11, 2011

Hi Jono,

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

Hi Jono,

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/