Re: [SMARTY] Re: Nested queries/nested arrays
| From: | Amanda Hemmerich | Date: | Fri, 12 Aug 2005 00:55:19 +0000 |
| Subject: | Re: [SMARTY] Re: Nested queries/nested arrays | ||
| References: | 1 2 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-12950@lists.php.net to get a copy of this message | ||
Thanks for the help!
When I change the code, I get this error for each loop through the foreach loop:
*Notice*: Undefined index: parent_project_id in */data/web/www.cgb/devel/includes/code/welcome.php* on line *52*
Line 52 is:
$id = $projects['parent_project_id'];
and the code block is:
$query ="SELECT * FROM projects WHERE parent_project_id is NULL OR parent_project_id = ''";
$projects = $db->getAssoc($query, DB_FETCHMODE_ASSOC);
foreach ($projects as $key => $project) {
$id = $projects['parent_project_id'];
$query ="SELECT * FROM projects WHERE parent_project_id = ? ";
$sub = $db->getAssoc($query, array($id));
$projects[$key]['subs'] = $sub;
}
$tpl->assign('projects', $projects);
I'm not sure what that means. I'll do a search for it, but if anyone has any advice to offer, I would appreciate it.
Thanks!
Amanda
Gary Smith wrote:
Hi Amanda, I'm no expert but I think if you change this: $query ="SELECT * FROM projects WHERE parent_project_id = $projects[$key]['project_id']"; to this $id = $projects['parent_project_id']; $query ="SELECT * FROM projects WHERE parent_project_id = ? "; And this: $sub = $db->getAssoc($query, DB_FETCHMODE_ASSOC); to this $sub = $db->getAssoc($query, array($id)); It might work. Thanks to Matthew Weier O'Phinney Gary in article 42FB988A.8030002@indiana.edu, Amanda Hemmerich at ahemmeri@indiana.edu wrote on 8/11/05 8:27 AM:Hello! I'm using PHP and Smarty to try to build an array of arrays using the results from nested queries. I am just learning about nested arrays, and I'm not sure what I'm doing wrong. I am hoping someone can give me a hint as to what I am doing wrong. If I remove the PHP foreach loop, it works fine, except, of course, no sub projects show up. :) The error must be in there, but I'm just not seeing it. I get the following error with the code below: Warning: Smarty error: unable to read resource: "welcome/Object.tpl" in /usr/local/lib/php/Smarty/Smarty.class.php on line 1088 PHP STUFF $query ="SELECT * FROM projects WHERE parent_project_id is NULL OR parent_project_id = ''"; $projects = $db->getAssoc($query, DB_FETCHMODE_ASSOC); foreach ($projects as $key => $project) { $query ="SELECT * FROM projects WHERE parent_project_id = $projects[$key]['project_id']"; $sub = $db->getAssoc($query, DB_FETCHMODE_ASSOC); $projects[$key]['subs'] = $sub; } $tpl->assign('projects', $projects); SMARTY STUFF {foreach from=$projects item='entry'} <b>{$entry.short_name}</b><br /> <ul> {foreach from=$entry.subs item='sub'}<li>{$sub.short_name}</li>{foreachelse}<li>No subs for this project</li>{/foreach} </ul> {foreachelse} <b>No projects found</b> {/foreach} Can anyone point me in the right direction? Thanks, Amanda