multiple records

29 posts by 4 authors in: Forums > CMS Builder
Last Post: October 12, 2010   (RSS)

By (Deleted User) - August 21, 2010

Hi Guys,

I'm trying to do the following, please let me know if it's possible:

I have two news sections at the moment (more to come) - they are technology and finance. Each news section has it's own section editor - however I want to display the latest stories added to each section in a single list on my homepage, instead of multiple lists.

Would this be possible?

regards
Shawn

Re: [smesa] multiple records

By Jason - August 23, 2010

Hi Shawn,

Yes, this would be possible. How many records would you want from each section? Let me know and I can try to give you a more specific example of how this would work.

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] multiple records

By (Deleted User) - August 23, 2010

Hi Jason,

Not really sure how many records - I basically need the list to display the latest item added from whichever section editor I use, i.e. so if I add something from technology then that will appear at the top. If I then add something from finance, that will appear one article above the technology one, etc etc.

It's basically one news feed using multiple section editors.

I would want to display about 5 records in total on the homepage but I might have several section editors.

regards
Shawn

Re: [smesa] multiple records

By Jason - August 23, 2010

Hi Shawn,

We can give this a try.

First, you'll need to put this function somewhere on your page. Near the bottom is usually good. It's only used to sort our array.

<?php
function sort_array ($array, $index, $order='asc', $natsort=FALSE, $case_sensitive=FALSE) {
if(is_array($array) && count($array)>0) {
foreach(array_keys($array) as $key) $temp[$key]=$array[$key][$index];
if(!$natsort) ($order=='asc')? asort($temp) : arsort($temp);
else {
($case_sensitive)? natsort($temp) : natcasesort($temp);
if($order!='asc') $temp=array_reverse($temp,TRUE);
}
foreach(array_keys($temp) as $key) (is_numeric($key))? $sorted[]=$array[$key] : $sorted[$key]=$array[$key];
return $sorted;
}
return $array;
}

?>


Now, near the top, we're going to use this code:

<?php
$tables = array('finance','technology');
$records=array();
$maxRecords = 5;

foreach($tables as $table){

list($tableRecords,$tableMetaData)=getRecords(array(
'tableName' => mysql_escape($table),
'allowSearch' => false,
'limit' => $maxRecords,
'orderBy' => "createdDate desc",
));

foreach($tableRecords as $tableRecord){
$records[]=$tableRecord;
}
}

$records=sort_array($records,'createdDate','desc');
?>


The variable $maxRecords you set to the maximum number of records you want to display. The variable $tables is a list of the tables you want to select from (this can be as many as you want). The code will select the maximum number of records from each table and put them into the array. Finally, it will use our sorting function to order these by the date they were created (newest first).

To output these records, we can use code that looks like this:
<?php $count=0; ?>

<?php foreach($records as $record): ?>
<?php if($count==$maxRecords){
break;
}
else{
$count++;
}?>

*YOUR OUTPUT CODE*
<?php endforeach ?>


This can be used to output the first $maxRecords from our array.

Give this a try and let me know how it works out for you.

Hope it 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] multiple records

By (Deleted User) - August 23, 2010

Hi Jason,

Thanks for this but I seem to be getting this error:

Fatal error: Call to undefined function getrecords() in /home/smesacoz/public_html/layout.php on line 9

regards
Shawn

Re: [smesa] multiple records

By Jason - August 23, 2010

Hi Shawn,

In php function names are case sensitive. Change it to getRecords. That should take care of the issue.

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: [Jason] multiple records

By (Deleted User) - August 23, 2010

Ok almost there - the problem I seem to be having now is displaying the images and next/previous links as they are using the $news_technology variable.

What do I change that variable to?

Re: [smesa] multiple records

By Jason - August 23, 2010

Hi,

Try using the variable $record. If that doesn't work, please attach the .php file you're working with and I can take a look at that for you.

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: [Jason] multiple records

By (Deleted User) - August 23, 2010

Hi Jason,

Attached is the PHP code I'm using.

regards
Shawn