Pulling records from another table.

15 posts by 3 authors in: Forums > CMS Builder
Last Post: October 25, 2010   (RSS)

Still getting the same message:
Notice: Undefined offset: 0 in /home/davgol20/goldcastleholdings.com/property.php on line 26

Goto this Suite page to see the Leasing Agent info (it works great)
http://173.236.177.72/suite.php?510-514-3

Go to this page where I want it to work but it won't:
http://173.236.177.72/property.php?name=Castle%20Building

Re: [Dan Maitland] Pulling records from another table.

By Chris - October 22, 2010

Hi Dan,

I suspect that either (a) your 'agent' field in your "Castle Building" record is blank or references an agent that no longer exists or (b) your 'agents' section has a 'name' field which is automatically trying to filter based on the query string (to fix this, we'll set 'allowSearch' => false).

Let's add some sanity-checking code to make sure that we're getting records back, as well as disabling allowSearch in your lookup: (new code in red)

list($buildingsRecords, $buildingsMetaData) = getRecords(array(
'tableName' => 'buildings',
'limit' => '1',
));
$buildingsRecord = @$buildingsRecords[0];
if (!$buildingsRecord) { header("HTTP/1.0 404 Not Found"); print "Building not found!"; exit; }

list($agentsRecords, $agentsMetaData) = getRecords(array(
'tableName' => 'agents',
'where' => mysql_escapef('num = ?', $buildingsRecord['agent']),
'allowSearch' => false,
'limit' => 1,
));
$agentsRecord = @$agentsRecords[0];
if (!$agentsRecord) { header("HTTP/1.0 404 Not Found"); print "Agent not found!"; exit; }


Does that help? Please let me know if you have any questions.
All the best,
Chris

Re: [chris] Pulling records from another table.

Forgot to add the "'allowSearch' => false, " now it's working perfectly.

Why would one page need that extra cose and the other doesn't? They are both trying to do the same thing so why would one work and not the other?

Re: [Dan Maitland] Pulling records from another table.

By Chris - October 25, 2010 - edited: October 25, 2010

Hi Dan,

It's because of your form input:

?name=Castle%20Building

Any getRecords() calls with "allowSearch" will automatically filter using form input that matches up with field names. allowSearch is enabled by default. Since your "agent" section has a "name" field, getRecords() is automatically adding an extra condition to your where clause to find only records which have name = "Castle Building".

It's probably good practice to disable allowSearch everywhere except in getRecords() calls where you explicitly want searching to occur.

Does that clear things up?
All the best,
Chris