Link to home
Start Free TrialLog in
Avatar of aguawebdesign
aguawebdesignFlag for United States of America

asked on

Smarty PHP Nested foreach loop

I am developing a site using the Smarty template engine and I need to display a list of links grouped in categories like this:

Category 1
     link
     link
     link
Category 2
     link
     link
     link
 

I am using the following code, but it keeps returning a "Query was empty" error.  Any ideas?
// PHP CODE
 
$rs_linkcatlist = mysql_query("SELECT * FROM tbl_link_category ORDER BY linkcat_position ASC");
while($linklist = mysql_fetch_array($rs_linkcatloop))
{
 $rs_linklist = mysql_query("SELECT dir_id, dir_status, dir_name, dir_url, dir_description, dir_link_library FROM tbl_company_directory WHERE dir_active = 1 AND dir_status >  1 AND dir_link_library = '" . $linklist['linkcat_id'] . "' ORDER BY dir_name ASC");
 while($links = mysql_fetch_array($rs_linklist))
 {
   // Organize an array
   $links_array[$linklist['linkcat_id']][] = $links;
  }
 $linkcats[] = $linklist;
}
$smarty->assign(array(
 'linkcats' => $linkcats,
 'links_array' => $links_array
 )
);
 
 
// TPL CODE
{foreach from=linkcats item=loopcats}
  <div class="section_border_bottom">
    <a name="{$loopcats.dir_link_library}" id="{$loopcats.dir_link_library}"></a>
    <h2>{$loopcats.linkcat_name}</h2>
    {foreach from=game_array[$loopcats.id] item=looplinks}
      <p><a href="{$looplinks.dir_url}" target="_blank">{$looplinks.dir_name}</a>
      {if $looplinks.dir_status eq 3}
        - <span class="smallfont"><span class="redbold">MARKETING PARTNER</span></span>
      {/if}
      <br />
      {$looplinks.dir_description}</p>
      <p class="right"><a href="#top">Top Of Page</a></p>
    {/foreach}
  </div><!--end section_border_bottom -->
{/foreach}

Open in new window

Avatar of aguawebdesign
aguawebdesign
Flag of United States of America image

ASKER

JUST NOTICED AN ERROR IN MY .TPL CODE.  CORRECTED CODE IS:

{foreach from=linkcats item=loopcats}
  <div class="section_border_bottom">
    <a name="{$loopcats.dir_link_library}" id="{$loopcats.dir_link_library}"></a>
    <h2>{$loopcats.linkcat_name}</h2>
    {foreach from=links_array[$loopcats.linkcat_id] item=looplinks}
      <p><a href="{$looplinks.dir_url}" target="_blank">{$looplinks.dir_name}</a>
      {if $looplinks.dir_status eq 3}
        - <span class="smallfont"><span class="redbold">MARKETING PARTNER</span></span>
      {/if}
      <br />
      {$looplinks.dir_description}</p>
      <p class="right"><a href="#top">Top Of Page</a></p>
    {/foreach}
  </div><!--end section_border_bottom -->
{/foreach}  


(Still getting the same error though)
You need to know if your MySQL queries are working right.  Try changing them so you can see if there are any errors.  See line 3 in the above example.  You might also want to test the number of rows that are returned from the queries.

HTH, ~Ray
$rs_linkcatlist = mysql_query("SELECT * FROM tbl_link_category ORDER BY linkcat_position ASC");
 
// TEST FOR QUERY COMPLETION
if (!$rs_linkcatlist) die( mysql_error() );
 
// TEST FOR ANY DATA
$nr = mysql_num_rows($rs_linkcatlist);
if (!$nr) die( "NO ROWS" );

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of aguawebdesign
aguawebdesign
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial