Solved

Smarty PHP Nested foreach loop

Posted on 2009-05-13
3
1,618 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:aguawebdesign
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:aguawebdesign
ID: 24381165
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)
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24385172
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

0
 

Accepted Solution

by:
aguawebdesign earned 0 total points
ID: 24389810
The MySQL queries were fine.  After combing through the code again I found an error in the php:

while($linklist = mysql_fetch_array($rs_linkcatloop))  SHOULD BE  while($linklist = mysql_fetch_array($rs_linkcatlist))

Also found another error in my .tpl code:

{foreach from=linkcats item=loopcats}  SHOULD BE   {foreach from=$linkcats item=loopcats}
and
{foreach from=links_array[$loopcats.linkcat_id] item=looplinks}  SHOULD BE  {foreach from=$links_array[$loopcats.linkcat_id] item=looplinks}


It's all working perfectly now.
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySQL limit and not so limited 13 61
Modify Filter to cause a field to be not Required 3 50
assigning javascript variable to php variable 8 69
sitemap.xml 3 32
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question