I managed to solve this with a little help from a friend see
code below. The reason why I needed this
feature is so that I can place paid listings in my directory in order according
to ranking (how much client paid for listing) while still shuffling the
listings so that there is no preference who comes top from their respective
rank . I have three types of listing Premium, Enhanced and Free and Premium
always needs to be above Enhanced which needs to be above Free.
This is for tpl_basic_list template by jasongallagher. I created a rank field using select simple
with 3 options 1 – Premium, 2 – Enhanced, 3 – Free. Then placed the rank field in the ordering tab
(Ascending) and in the list tab in the backend. Then the following code in
template:
-----------------------------------------------------------------------------
<?php
$items = $cck->getItems();
shuffle($items); //Shuffles the all the entries
$premium = array(); //create new array for premium
$enhanced = array(); //create new array for enhanced
$free = array(); //create new array for free
foreach( $items as $item ) {
if($item->getValue('rank') == 1) {
array_push($premium, $item);
}
else if($item->getValue('rank') == 2){
array_push($enhanced, $item);
}
else if($item->getValue('rank') == 3){
array_push($free, $item);
}
$items = array_merge($premium, $enhanced, $free); //MERGES ARRAYS IN ORDER
}
foreach( $items as $item ) {
$count++;
?>
---------------------------------------------------------------------------------------------------------
Another useful feature I have is the ability to place
adverts in between listings by inserting a joomla image module after this code
----------------------------------------------
<?php if ($count == 1 ) : ?>
<?php elseif ($count == 3) :?>
Which after the first two listings it places the module. If
you want to add more:
<?php elseif ($count == 7) : ?>
--call module
--------------------------------------------------------------------------------------------------
HERE IS THE FULL CODE FOR THE TEMPLATE INDEX.PHP FILE
<?php
/**
* blank template by Jason Gallagher: www.jasongallagher.org
* @copyrightCopyright (C) 2012 Jason Gallagher. All Rights Reserved.
**/
// No Direct Access
defined( '_JEXEC' ) or die;
?>
<?php
require_once dirname(__FILE__).'/config.php';
$cck=CCK_Rendering::getInstance();
if ( $cck->init() === false ) { return; }
$document =& JFactory::getDocument();
$document->addStyleSheet(JURI::base().'templates/'. $this->template. "/css/list.css");
?>
<?php
$items = $cck->getItems();
shuffle($items); //Shuffles the all the entries
$premium = array(); //create new array for premium
$enhanced = array(); //create new array for enhanced
$free = array(); //create new array for free
foreach( $items as $item ) {
if($item->getValue('rank') == 1) {
array_push($premium, $item);
}
else if($item->getValue('rank') == 2){
array_push($enhanced, $item);
}
else if($item->getValue('rank') == 3){
array_push($free, $item);
}
$items = array_merge($premium, $enhanced, $free); //MERGES ARRAYS IN ORDER
}
foreach( $items as $item ) {
$count++;
?>
<!-- IMPORTANT call your fields like $item->get('the_field')->value INSTEAD OF $cck->get('the_field')->value -->
<!-- CALL ALL THE LISTING FIELDS HERE -->
<?php echo $item->renderField('art_title'); ?>
<!-- INSERT JOOMLA MODULE HERE -->
<?php if ($count == 1 ) : ?>
<?php elseif ($count == 3) :?>
<?php echo $item->renderField('advert_p1'); ?><!-- JOOMLA MODULE FOR EXAMPLE RANDOM IMAGE -->
<?php elseif ($count == 7) : ?>
<!-- CALL MODULE ON THE 7TH ENTRY -->
<?php elseif ($count == 11) : ?>
<!-- CALL MODULE ON THE 11TH ENTRY -->
<?php endif;$count++; ?>
<?php } ?>
<?php $cck->finalize(); ?>