Display first record in the table by default, no matter what the recordNum?

5 posts by 2 authors in: Forums > CMS Builder
Last Post: March 17, 2008   (RSS)

By dougdrury - March 15, 2008

I have CMS Builder 1.5.

I have a page (news.php) that has a list viewer for the 'news' table on the left hand side. On the same page (on the right), I have page viewer code for any of the news stories chosen from the list viewer.

My client frequently deletes all the news stories and adds new ones. Upon him doing that, my initial view of the page fails by not displaying the first 'news' record in the 'news' table. I get an error "getUploads: no 'recordNum' value specified!".

How do I tell the page reader to read the first record in the database table no matter what the record number is?

I am sure I am doing somethings stupid.

I will paste my code:

lister code:
<!-- STEP1: Load Record List (Paste this above other steps) -->
<?php
require_once "../cmsAdmin/lib/viewer_functions.php";
$options = array(); // NOTE: see online documentation for more details on these options
$options['tableName'] = 'news'; // (REQUIRED) MySQL tablename to list record from. Example: 'article';
$options['titleField'] = 'title'; // (optional) MySQL fieldname used in viewer url for search engines. Example: 'title' would display: viewer.php/article_title_here-123
$options['viewerUrl'] = 'news.php'; // (optional) URL of viewer page. Example: '/articles/view.php';
$options['perPage'] = ''; // (optional) The number of records to display per page. Example: '5'; Defaults to 10.
$options['orderBy'] = 'menu_order'; // (optional) Fieldnames to sort by. Example: 'field1, field2 DESC, field3';
$options['pageNum'] = ''; // (optional) Page number of results to display. Example: '1'; Defaults to number on end of url, then 1
$options['where'] = ''; // (ADVANCED) Additional MySQL WHERE conditions. Example: 'fieldname = "value"'
$options['useSeoUrls'] = ''; // (ADVANCED) Set this to '1' for search engine friendly urls: view.php/123 instead of view.php?123 (not supported on all web servers, and viewerUrl must be absolute path such as '/path/viewer.php')
list($listRows, $listDetails) = getListRows($options);
?>
<!-- /STEP1: Load Record List -->

<UL>
<!-- STEP2: Display Record List (Paste this where you want your records to be listed) -->
<?php foreach ($listRows as $record): ?>
<li><a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a></li>
<?php endforeach ?>

<?php if (empty($listRows)): ?><!-- Display "No Records Found" Message -->
No records were found!<br/><br/>
<?php endif ?><br/>
<!-- /STEP2: Display Record List -->
</UL>


page code:
<!-- STEP1: Load Record (Paste this above other step) -->
<?php
require_once "../cmsAdmin/lib/viewer_functions.php";
$options = array(); // NOTE: see online documentation for more details on these options
$options['tableName'] = 'news'; // (REQUIRED) MySQL tablename to list record from. Example: "article";
$options['recordNum'] = '1'; // (optional) Record number to display. Example: "1"; Defaults to number on end of url, then 1
$options['where'] = ''; // (ADVANCED) MySQL WHERE conditions to use INSTEAD of recordNum to look up record. Example: "fieldname = 'value'"
$record = getRecord($options);
?>
<!-- /STEP1: Load Record -->

<H1><?php echo $record['title'] ?></H1>
<?php echo $record['content'] ?>
<!-- STEP3: Display Uploads from 'files' (Paste this where you want your uploads displayed) -->
<!-- Upload Program Fields : num, createdTime, tableName, fieldName, recordNum, preSaveTempId, filePath, filename, extension, thumbFilePath -->
<!-- Upload Image Fields : isImage, hasThumbnail, urlPath, width, height, thumbUrlPath, thumbWidth, thumbHeight -->
<!-- Upload Info Fields : info1, info2, info3, info4, info5 -->
<?php foreach (getUploads($options['tableName'], 'files', $record['num']) as $upload): ?>
.
.
.


If I force the recordNum with "$options['recordNum'] = '1';" it works, but I can't keep going in and changing the actual record number each time he cleans his news stories.

I have tried "$options['recordNum'] = '';" as well. Still get the error.

Thank you for any help.

Doug

Re: [dougdrury] Display first record in the table by default, no matter what the recordNum?

By Dave - March 15, 2008

Hi Doug,

No problem, there's two things we can do. First, we get rid of that "getUploads" error. We actually added extra code for that in v1.06. Just add this around your getUploads code:

<?php if ($record): ?>

<?php foreach (getUploads ... ): ?>
...
<?php endforeach ?>

<?php endif ?>


That way, if no record is loaded (for whatever reason) it won't try to load the uploads for a record that isn't there and get an error.

Next, you want to always have your page viewer (which is on the same page) display the first record in the list. As long as the viewer code for the list viewer comes first in the page source code, we can do that like this:

$options['recordNum'] = @$listRows[0]['num'];

The $listRows is just a list of records, so the [0] gets the first one (PHP starts counting at zero) and ['num'] is the fieldname we want. The @ in front means "hide any errors if this variable isn't defined".

Hope that helps! Let me know how it goes!
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Display first record in the table by default, no matter what the recordNum?

By dougdrury - March 17, 2008

Dave,
Thanks for the info on the getUploads. I will upadate my pageviewer code with that foreach.

As far as the listviewer/pageviewer issue...
This is so close to what I want... with one thing... (of course huh?)

If the page (with listviewer and pageviewer code onit) gets loaded without any number at the end of the URL, I want the first record to display in the page viewer (I won't know the record number since it could be brand new), but then if someone clicks on a link in the page list, I want that other page (the record num in the url) to be displayed by the page viewer. I tried the code you sent, but it always displays the first record even if the url has a record number in it.

Is there a way to test if the URL has a record num in it and if not, use the @$listRows[0]['num'] ?

Am I making any sense? [:/]

Re: [dougdrury] Display first record in the table by default, no matter what the recordNum?

By Dave - March 17, 2008

>Is there a way to test if the URL has a record num in it and if not, use the @$listRows[0]['num']?

Sure, try this instead (it's a few more lines):

$firstRecordNum = @$listRows[0]['num'];
$numOnEndOfUrl = getNumberFromEndOfUrl();
if ($numOnEndOfUrl) { $options['recordNum'] = $numOnEndOfUrl; }
else { $options['recordNum'] = $firstRecordNum; }


Let me know if that does what you need.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Display first record in the table by default, no matter what the recordNum?

By dougdrury - March 17, 2008

PERFECTO!
That worked like a champ!

Thank you sir!
~ Doug