View previous topic :: View next topic |
Author |
Message |
alanzhao Smarty Rookie
Joined: 24 Apr 2003 Posts: 6 Location: Manhattan, NY
|
Posted: Thu Apr 24, 2003 8:58 pm Post subject: Looping? |
|
|
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 |
|
Back to top |
|
Wom.bat Smarty Pro
Joined: 24 Apr 2003 Posts: 107 Location: Munich, Germany
|
Posted: Fri Apr 25, 2003 12:35 am Post subject: |
|
|
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 |
|
alanzhao Smarty Rookie
Joined: 24 Apr 2003 Posts: 6 Location: Manhattan, NY
|
Posted: Fri Apr 25, 2003 1:01 am Post subject: |
|
|
Thanks! |
|
Back to top |
|
kemical Smarty Rookie
Joined: 04 May 2003 Posts: 5
|
Posted: Sun May 04, 2003 11:50 pm Post subject: |
|
|
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 |
|
ymo Smarty Rookie
Joined: 03 May 2003 Posts: 14 Location: Tokyo
|
Posted: Mon May 05, 2003 12:03 am Post subject: |
|
|
My suggestion is Debug your code! 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 |
|
kemical Smarty Rookie
Joined: 04 May 2003 Posts: 5
|
Posted: Mon May 05, 2003 12:12 am Post subject: |
|
|
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 |
|
ymo Smarty Rookie
Joined: 03 May 2003 Posts: 14 Location: Tokyo
|
|
Back to top |
|
kemical Smarty Rookie
Joined: 04 May 2003 Posts: 5
|
Posted: Mon May 05, 2003 12:42 am Post subject: |
|
|
well i read up on arrays but it still telling me this code should work but it isnt
im on win2k server with php4.23 is this a windows issue? _________________ :// kem |
|
Back to top |
|
ymo Smarty Rookie
Joined: 03 May 2003 Posts: 14 Location: Tokyo
|
Posted: Mon May 05, 2003 2:26 am Post subject: |
|
|
No, It's not environment problem.
Problem is PHP code. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon May 05, 2003 3:11 am Post subject: |
|
|
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
good luck. |
|
Back to top |
|
Wom.bat Smarty Pro
Joined: 24 Apr 2003 Posts: 107 Location: Munich, Germany
|
Posted: Mon May 05, 2003 6:26 am Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon May 05, 2003 7:25 am Post subject: |
|
|
Quote: | he just gets this array thingie wrong |
@Wom.bat : ya think ??
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 )--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 |
|
Wom.bat Smarty Pro
Joined: 24 Apr 2003 Posts: 107 Location: Munich, Germany
|
Posted: Mon May 05, 2003 12:23 pm Post subject: |
|
|
hehe
err... ... I wasn't talking about you, really...
|
|
Back to top |
|
kemical Smarty Rookie
Joined: 04 May 2003 Posts: 5
|
Posted: Mon May 05, 2003 1:34 pm Post subject: |
|
|
well ive done database queries and stuff like that, just havent used arrays all that much
but that stuff worked thanks _________________ :// kem |
|
Back to top |
|
|