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

Amanda Hemmerich <[email protected]>
Newsgroups gmane.comp.php.smarty.general
Message-ID <[email protected]>

Gary Smith wrote:

>Amanda,
>
>What happens if you
>print_r($projects);
>after
>$tpl->assign('projects', $projects);?
>
>  
>
Ok, I did this, and it prints out the array of project stuff, however, 
it doesn't seem to be saving the sub-project array in the array.  The 
two projects with sub projects have the same structure as those with no 
sub projects.  Here is an example of two projects, one with a sub 
project, one without:

Array ( [1] =>

Array ( [0] => CATPA [1] => Curation Analysis Tool for Protein 
Alignments [2] => [3] => As the genomes of more organisms become fully 
sequenced, there will be a greater opportunity to understand the 
function, structure, and evolution of genes. Typically, blah, blahblah. 
[4] => 34 [5] => )

[14] => Array ( [0] => Regulatory Elements [1] => Regulatory Elements 
[2] => [3] => Transcriptional factors regulate gene expressions by 
binding to elements in DNA sequences. These regulatory elements are ofen 
short (about 4-15 nucleotides long), hidden in the background of nearly 
random DNA sequences, which make their identification difficult. blah, 
blahblah. [4] => 0 [5] => )

)

Looks like the structure is:

[14] (project_id) => Array ( [0] => short_name [1] => title [2] 
=>short_description [3] => description [4] => file_id [5] => 
parent_project_cd)

I'm sort of at a loss.  I don't see any column names in the array, which 
I thought would be there, and it looks like adding the subs to the array 
did not work, either.

>Gary
>
>
>in article [email protected], Amanda Hemmerich at
>[email protected] wrote on 8/12/05 3:06 PM:
>
>  
>
>>Great!  That worked!  At least, it didn't get any errors.  :)
>>
>>Now, I am trying to loop through both arrays on the HTML/Smarty side,
>>but nothing is showing up.  The name of the project does not show up,
>>but for each iteration of the loop, it prints out "- id ()" and "No subs
>>for this project", even for projects that have sub projects.  I've never
>>used a Smarty "foreach" before - I normally use Smarty "sections", and
>>I'm not getting any errors, so I'm not sure what I'm doing wrong.
>>
>>{foreach from=$projects item='entry'}
>>  <b>{$entry.short_name}</b> - id ({$entry.project_id})<br />
>>  <ul>
>>  {foreach from=$entry.subs item='sub'}
>>      <li>{$sub.short_name} - {$sub.project_id}</li>
>>  {foreachelse}
>>      <li>No subs for this project</li>
>>  {/foreach}
>>  </ul>
>>{foreachelse}
>>  <b>No projects found</b>
>>{/foreach}
>>
>>
>>Gary Smith wrote:
>>
>>    
>>
>>>Hi Amanda,
>>>
>>>Try this:
>>>
>>>
>>>$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) {
>>>if (!empty($project['project_id'])){
>>>
>>>   $id = $project['project_id'];
>>>   $query ="SELECT * FROM projects WHERE project_id = ? ";
>>>
>>>   $sub = $db->getAssoc($query, false, array($id));
>>>   $projects[$key]['subs'] = $sub;
>>>}
>>>}
>>>
>>>$tpl->assign('projects', $projects);
>>>
>>>Gary
>>>
>>>
>>>in article [email protected], Amanda Hemmerich at
>>>[email protected] wrote on 8/12/05 9:14 AM:
>>>
>>> 
>>>
>>>      
>>>
>>>>$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) {
>>>>if (!empty($project[$key]['project_id'])){
>>>>
>>>>   $id = $project[$key]['project_id'];
>>>>   $query ="SELECT * FROM projects WHERE parent_project_id = ? ";
>>>>
>>>>   //$sub = $db-> getAssoc($query, false, array($id));
>>>>   //$projects[$key]['subs'] = $sub;
>>>>}
>>>>}
>>>>
>>>>$tpl->assign('projects', $projects);
>>>>   
>>>>
>>>>        
>>>>
>>> 
>>>
>>>      
>>>
>
>  
>

-- 
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.