Link to top record only but when on the same list page

8 posts by 4 authors in: Forums > CMS Builder
Last Post: September 27, 2013   (RSS)

I have a list page which has some side navigation. I want one of the links to go to the first record of that list which is fine when on other pages however when I'm on that list page how do I tell that link to specifically go to the first record as the code at the top is telling it to pull all in records. ie. 

// load records from 'shows'
list($showsRecords, $showsMetaData) = getRecords(array(
'tableName' => 'shows',
'loadUploads' => true,
'allowSearch' => false,
));

Thanks!

Hannah

Hi Jason,

That's right. For all other pages:

// load records from 'shows'
list($showsRecords, $showsMetaData) = getRecords(array(
'tableName' => 'shows',
'limit' => '1',
'loadUploads' => true,
'allowSearch' => false,
));

<?php foreach ($showsRecords as $record): ?>
<a class="read-more" href="<?php echo $record['_link'] ?>"> Read more >></a>

and then on the list page I need the top to pull in all so:

// load records from 'shows'
list($showsRecords, $showsMetaData) = getRecords(array(
'tableName' => 'shows',
'loadUploads' => true,
'allowSearch' => false,
));

<a href="<?php echo $record['_link'] ?>" class="side">View next upcoming show >></a>

So how would I adjust that to allow the page to display all records but the side bar link only link to the top record in that list?

Thank you so much for your help so far!

By Jason - September 25, 2013

Hi Hannah,

What you can do is just pull the top record off of your $showRecords data set and display that like this:

<?php
// load records from 'shows'
list($showsRecords, $showsMetaData) = getRecords(array(
  'tableName' => 'shows',
  'loadUploads' => true,
  'allowSearch' => false,
));

$show = @$showRecords[0];

?>
<a href="<?php echo $show['_link'] ?>" class="side">View next upcoming show >></a>

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/

Hi Jason,

That's great except it's pulling in the last record from the list and not the top!!

Hannah

By Damon - September 26, 2013

Hi Hannah,

Try adding the orderBy option into the code:

<?php
// load records from 'shows'
list($showsRecords, $showsMetaData) = getRecords(array(
  'tableName' => 'shows',
  'loadUploads' => true,
  'orderBy'     => 'date DESC',
  'allowSearch' => false,
));

$show = @$showRecords[0];

?>

This will sort it by date, newest to oldest.  You can also sort on another field if needed.

Hope this helps!

Cheers,
Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Hi Damon,

I get MySQL Error: Unknown column 'date' in 'order clause' when I use that. I though maybe that's because my fields are listed differently so tried start_date but that then just generated a random record. Then I tried changing my sort order in my CMS to include start_date but I get 

Sorting Error: Check your 'Order By' fields in the sorting tab for invalid field names.
MySQL returned the following error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'start_date DESC LIMIT 1' at line 1

Any suggestions?

Hi Hannah,

Another options is to use the end function to get the last item from the array instead:

<?php
// load records from 'shows'
list($showsRecords, $showsMetaData) = getRecords(array(
  'tableName' => 'shows',
  'loadUploads' => true,
  'allowSearch' => false,
));

$show = end($showRecords);

?>

The end function will retrieve the last element from an array, and this avoids having to use the orderBy variable in the getRecords function.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com