Re: [SMARTY] Re: Nested queries/nested arrays

Matthew Weier O'Phinney <[email protected]>
Newsgroups gmane.comp.php.smarty.general
Message-ID <[email protected]>
* Amanda Hemmerich <[email protected]>:
> I've changed the code again, but I'm still getting an error.  I was 
> getting an error on this line:
>
> $id = $project[$key]['project_id'];
>
> but I added a check  $project[$key]['project_id'], and that got rid of 
> the following error:
>
> *Notice*: Undefined offset: 6 in 
> */data/web/www.cgb/devel/includes/code/welcome.php* on line *56

Good to see you're developing with error reporting set to E_ALL.... now
look at line 56. I've got a comment on that below.

> *Once I uncomment this line:
>
> //$sub = $db-> getAssoc($query, false, array($id));
>
> I get this error:
>
> *Warning*: Smarty error: unable to read resource: "welcome/Array.tpl" in 
> */usr/local/lib/php/Smarty/Smarty.class.php* on line *1088
>
> *I don't think it's any sort of set-up or permissions problem with the 
> templates because we've been using this page for over a year for other 
> queries and it works until I uncomment that one line. 

Are you using an {include} call in your template? and, if so, are you
dynamically pulling it? The reason I ask is that 'Array.tpl' looks like
you were trying to do someting like 

    {include file="welcome/$variable.tpl"}

Basically, the above error I've only ever seen when the template file is
missing or unreadable.

> Here is the code as it is now, which produces no errors and displays the 
> other stuff on the page correctly, but doesn't give me any sub-projects 
> (obviously!):
>
> $query ="SELECT * FROM projects WHERE parent_project_id is NULL OR 
> parent_project_id = ''";
>  
> $projects = $db->getAssoc($query, DB_FETCHMODE_ASSOC);

Check the syntax for the getAssoc() method. What DB abstraction layer
are you using -- PEAR::DB, PEAR::MDB, PEAR::MDB2, ADODB...? As I've
mentioned before, if you're using PEAR::DB, the syntax is:

    getAssoc($query, $force_array, $params, $fetchmode)

Typically, when using getAssoc, there should be no reason to set the
fetchmode (it's returning an associative array already) -- and, besides,
you'd need two more parameters between the $query and the $fetchmode.
You probably only need:

    $projects = $db->getAssoc($query);

> foreach ($projects as $key => $project) {
>    if (!empty($project[$key]['project_id'])){
>
>       $id = $project[$key]['project_id'];

The above should be:

    if (!empty($project['project_id'])) {
        $id = $project['project_id'];

>       $query ="SELECT * FROM projects WHERE parent_project_id = ? ";
>
>       //$sub = $db-> getAssoc($query, false, array($id));

Remove the space in the above statement: "$db->getAssoc()" instead of
"$db-> getAssoc()".

>       //$projects[$key]['subs'] = $sub;
>    }
> }
>
> $tpl->assign('projects', $projects);
>
> Any ideas what I can do now?  I've searched on that error, but I haven't 
> been able to find anything that pertains to me.

If you continue to have problems, you should probably create a dump of
the DB schema and then post a link to that and the PHP source you're
working with -- at this point, the suggestions myself and others have
made should give you something to work with, but there are many
assumptions we're having to make -- what DB abstraction layer you're
using, where files are located, etc.


> Matthew Weier O'Phinney wrote:
>
> > * Gary Smith <[email protected]> :
> >  
> >
> > > 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'];
> > >    
> > >
> >
> > The above should be:
> >    $id = $projects[$key]['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));
> > >    
> > >
> >
> > getAssoc(), in PEAR::DB at least expects 
> > ($sql (string), force (boolean), parameters (array)) as its arguments:
> >
> >    $sub = $db-> getAssoc($query, false, array($id));
> >
> >  
> >
> > > Thanks to Matthew Weier O'Phinney
> > >    
> > >
> >
> > Thanks.... I think... :-)
> >
> > I think the problem causing the notice the OP is receiving is more
> > likely to do with file location and/or permissions, and I've responded
> > to that under separate cover. However, the above is definitely an issue
> > as well.
> >
> >  
> >
> > > in article [email protected], Amanda Hemmerich at
> > > [email protected] 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?
> > > >      
> > > >
> >
> >  
> >


-- 
Matthew Weier O'Phinney
Zend Certified Engineer
http://weierophinney.net/matthew/

-- 
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.