Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Looping?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
alanzhao
Smarty Rookie


Joined: 24 Apr 2003
Posts: 6
Location: Manhattan, NY

PostPosted: Thu Apr 24, 2003 8:58 pm    Post subject: Looping? Reply with quote

Hi,

suppose I have the script follows:

index.php
Code:

// select 10 records containing 'name', 'email', 'url', 'location', 'message'
$qid = $DB->query("SELECT * FROM guestbook LIMIT 0, 10");


I want to loop through $qid and display the records, how do I code the script and template file? I try to use '{foreach}{/foreach}', but it can't loop through a data object.

The template format can be simple as:
Code:

{$name}<br>
{$email}<br>
{$url}<br>
{$location><br>
{$message}<br>


Thanks Rolling Eyes
Back to top
View user's profile Send private message
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Fri Apr 25, 2003 12:35 am    Post subject: Reply with quote

Code:
$result = $db->query("SELECT whatever...");
if ($result)
{
 while ($row = $result->fetch_assoc())
 {
  $rows[] = $row;
 }
}
$smarty->assign("rows", $rows);


{foreach from=$rows item="row"}
 {$row.name}...{$row.email}
{/foreach}
Back to top
View user's profile Send private message
alanzhao
Smarty Rookie


Joined: 24 Apr 2003
Posts: 6
Location: Manhattan, NY

PostPosted: Fri Apr 25, 2003 1:01 am    Post subject: Reply with quote

Thanks! Rolling Eyes
Back to top
View user's profile Send private message
kemical
Smarty Rookie


Joined: 04 May 2003
Posts: 5

PostPosted: Sun May 04, 2003 11:50 pm    Post subject: Reply with quote

i tried this code and im still not returning any results

here is my index.php

Code:
$result = mysql_query("SELECT * FROM articles",$db);
                  if ($result) {
                  while ($row = mysql_fetch_assoc($result));
                  {
                    $rows["title"] = $row;
                  }
                  }
                  $smarty->assign("title", $rows);


and here is my template

Code:
{foreach from=$rows item="title"}
{$row.title}<br />
{/foreach}


any suggestions?
_________________
:// kem
Back to top
View user's profile Send private message
ymo
Smarty Rookie


Joined: 03 May 2003
Posts: 14
Location: Tokyo

PostPosted: Mon May 05, 2003 12:03 am    Post subject: Reply with quote

My suggestion is Debug your code! Wink Like this.
Code:

$result = mysql_query("SELECT * FROM articles",$db);
if ($result) {
 while ($row = mysql_fetch_assoc($result));
  {
   $rows["title"] = $row;
 }
}

// for debug
print_r($rows);

//$smarty->assign("title", $rows);
Back to top
View user's profile Send private message
kemical
Smarty Rookie


Joined: 04 May 2003
Posts: 5

PostPosted: Mon May 05, 2003 12:12 am    Post subject: Reply with quote

after debugging it is only displaying

Code:
Array ( [title] => )


with this code

Code:
$result = mysql_query("SELECT * FROM articles",$db);
                  if ($result) {
                  while ($row = mysql_fetch_assoc($result));
                   {
                     $rows["title"] = $row;
                  }
                  }

                  // for debug
                  print_r($rows);
                  //$smarty->assign("title", $row);


any clues?
_________________
:// kem
Back to top
View user's profile Send private message
ymo
Smarty Rookie


Joined: 03 May 2003
Posts: 14
Location: Tokyo

PostPosted: Mon May 05, 2003 12:25 am    Post subject: Reply with quote

What is diference with Wom.bat's code?
You need study about array. Wink
See, http://www.php.net/manual/en/language.types.array.php
Back to top
View user's profile Send private message
kemical
Smarty Rookie


Joined: 04 May 2003
Posts: 5

PostPosted: Mon May 05, 2003 12:42 am    Post subject: Reply with quote

well i read up on arrays but it still telling me this code should work but it isnt Sad

im on win2k server with php4.23 is this a windows issue?
_________________
:// kem
Back to top
View user's profile Send private message
ymo
Smarty Rookie


Joined: 03 May 2003
Posts: 14
Location: Tokyo

PostPosted: Mon May 05, 2003 2:26 am    Post subject: Reply with quote

No, It's not environment problem.
Problem is PHP code. Wink
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon May 05, 2003 3:11 am    Post subject: Reply with quote

kemical: take a closer look at Wom.bat's code -- he is appending new elements to an array ( $rows[] = $row ) while your code is trying to repeatedly (force) put the entire contents of a row (an array in its own right) in the single array element named title ($row['title'] ). Each iteration of the loop will replace the results.

If you are trying to capture an array of titles, then the way to do it is:

$rows["title"][] = $row["title"];

There are many examples of looping and looping dbs already posted into the forums if you take a look Wink

good luck.
Back to top
View user's profile Send private message
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Mon May 05, 2003 6:26 am    Post subject: Reply with quote

boots, I think he just gets this array thingie wrong
kemical, do
$rows[] = ...
instead of
$rows["title"] = ...
in the while loop...
and do
$smarty->assign("title", $rows);
instead of
$smarty->assign("title", $row);

don't get me wrong please, but before you try smarty, you should really get a little basics in programming and PHP...
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon May 05, 2003 7:25 am    Post subject: Reply with quote

Quote:
he just gets this array thingie wrong


@Wom.bat : ya think ?? Wink

I just thought I'd point out WHY and WHERE his code was wrong so that maybe he could understand why the replies he was getting were simply saying that he got it wrong. I concur with you (unless you were talking about me Twisted Evil)--you have to have some programming and PHP background if you want to build an application that uses Smarty.

It doesn't take a lot, so do your practicing before you fire up Smarty.
Back to top
View user's profile Send private message
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Mon May 05, 2003 12:23 pm    Post subject: Reply with quote

hehe Smile
err... Embarassed ... I wasn't talking about you, really...

Smile
Back to top
View user's profile Send private message
kemical
Smarty Rookie


Joined: 04 May 2003
Posts: 5

PostPosted: Mon May 05, 2003 1:34 pm    Post subject: Reply with quote

well ive done database queries and stuff like that, just havent used arrays all that much Smile

but that stuff worked thanks
_________________
:// kem
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP