|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
marfee Smarty n00b
Joined: 11 May 2003 Posts: 2 Location: Taiwan
|
Posted: Sun May 11, 2003 4:38 am Post subject: about using of foreach function |
|
|
Hi ,All
I am a novice of smarty, i`ve a problem about using foreach to fetch data from database, can anyone help ~~thanks a lot!!
This is a example :
Database Name:eio
Table Name:customer
cust_id user phone
1 user1 22552211
2 user2 22334455
3 user3 11223344
config.php(define how i connect to database):
//start
$DSN="mssql://root:hi@localhost/eio";
include_once "DB.php";
$mydb=new DB;
$myobj=$mydb->connect($DSN,false);
if($mydb->isError($myobj))
{ echo $myobj;
return;
}
define(SMARTY_DIR,"/web/smarty/");
define(TEMPLATES_DIR,"/web/templates/");
include(SMARTY_DIR."Smarty.class.php");
$smarty = new Smarty;
$smarty->template_dir =SMARTY_DIR."templates/";
$smarty->compile_dir = SMARTY_DIR."templates_c/";
$smarty->config_dir = SMARTY_DIR."configs/";
$smarty->cache_dir = SMARTY_DIR."cache/";
//end
showfile.php:
//start
include_once("config.php");
$sql="select * from customer";
$myres=$myobj->query($sql);
if($myobj->isError($myres))
{ die ($myres->getMessage());
return;
}
while($row=$myres->fetchRow())
{$arr[$i]=array('id'=>$row[0],'name'=>$row[1],'phone=>'$row[2]');
$i++;
}
$smarty->assign('tmp',$arr);
$smarty->display("data.tpl");
//end
data.tpl
//start
{foreach item=content from=$arr}
{$content}
{/foreach}
//end
Question1:why is the result always "Array Array Array" ?
*How can i show the data using Foreach function?
Question2:if i want to show data in table , how can i design my data.tpl file
in correct code?
because my english is pool , so when i study the document of Smarty, a lot of things isn`t understand for me.
so Can anyone help me with a sample code?
thanks a lot!!
Last edited by marfee on Wed May 05, 2010 10:59 pm; edited 1 time in total |
|
Back to top |
|
Wom.bat Smarty Pro
Joined: 24 Apr 2003 Posts: 107 Location: Munich, Germany
|
Posted: Sun May 11, 2003 9:05 am Post subject: |
|
|
$content is the sql result array for each iteration
to access data, you have to do
{$content.index}
i.e. something like
{$content.name}
but this wouldn't work anyway, as your array is named "tmp" (you give it a name when you assign it)
I would suggest this:
Code: |
// ...
while ($row = $myres->fetchRow())
{
$arr[] = $row;
}
$smarty->assign('arr',$arr);
$smarty->display("data.tpl");
|
and
Code: |
{foreach from=$arr item="row"}
{$row.name}
{/foreach}
|
displaying your data in a table is easy
consider some approach like the following:
Code: |
<table ...>
{foreach from=$arr item="row"}
<tr>
<td>{$row.id}</td><td>{$row.name}</td>
</tr>
{/foreach}
</table>
|
|
|
Back to top |
|
marfee Smarty n00b
Joined: 11 May 2003 Posts: 2 Location: Taiwan
|
Posted: Wed May 14, 2003 4:35 pm Post subject: |
|
|
HI,
I`ve tried those code with approach changes, but it shows the word "Array" only one time if i used {$row}.
if i set "$arr[]=$row" , whether it means $arr[0]=array,$arr[1]=array
or not.....because $row is an object ,not data of array......
and why i use $row.id?i`ve tried it, and it only show one character ,like A or r......(just like seperate with five characters:A,r,r,a,y)
I am so stupid , so can you explain it for me with above example again.
Thanks a lot!!! |
|
Back to top |
|
|
|
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
|