You are not enjoying the benefits offered by registering. To register click here...
  
Forums | Prefs | Your Messages | Members | Recent | Search
Quick Search


Advanced Search
| Calendar | Doc | Dev' Blog! |
Chat (Alpha Version)

Guest, do not forget to login ( Register )
 Top > Forums > This Software > Support, FAQ, How-to's
 [How To] While Loop

Skin Selection:
 (1 pages) 1  
harmor


Story Teller
Group: Developers
Posts: 127
Reputation: 3


Posted on Jul 12, 2006, 6:14 am by harmor
[How To] While Loop
First lets look at the PHP code
php Code:
  1.  
  2.         $qry = "SELECT * FROM {$CONFIG->dbprfx}users ORDER BY user_posts DESC LIMIT 5";
  3.         $res = $DB->query($qry);
  4.         while($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
  5.         {
  6.             $usernames .= "Username: ".$row['userid']." (Posts: ".$row['user_posts'].") ";
  7.            
  8.             $TEMPLATE->assign('username', $usernames);
  9.         }
  10. $this->pagename = "mypage.tpl";
  11.  
  12.  
  13.  

First we do a common task and retrieve data from the database.  Within the while loop we are assigning a string to a variable to be passed through the "assign" function.
The data $username holds gets sent to $TEMPLATE->assign('username', $usernames);.
The first argument is for the loop in the .tpl file. The second argument holds the data we want to send to the .tpl file

In mypage.tpl we have "foreach loop".
html Code:
  1. {foreach from=$username item=user_name}
  2.     {$user_name}
  3.     {/foreach}
  4.  
  5.  
  6.  
  7.  


The parameter from=$username is relative (is that the correct term?) to the first parameter of the "assign" function.
The parameter item=user_name is unique and doesn't have to be anything special.
The code within the "foreach" tags is the same as the value of "item".

This was a quick explaination. If you have any problems I'll help.

Last edit by harmor on Dec 31, 1969, 7:00 pm
Xen Web Hosting Offering ad-free hosting with features such as, cpanel, fantastico, PHP and MySQL support, and more -------------------------------------------------------------------------------------------------------
Chris
Let's hang on to Harmor, it's not everyday that you find people who walk the walk, not just talk the talk.
 
Top

 (1 pages) 1 - Flat Mode | Threaded Mode  
Quick Jump:

         
Page generated in 0.04 seconds (Queries: 0.01) - Cpu: 0.85
Total DB [adodb:mysql] queries: 15
Total Strings Translated: 13
Powered by NextBBS SE v0.4.5 - Copyright © 2007 CFR & The NextBBS Team
Chatbox powered by smiletag