|
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 |
Robbie Smarty n00b
Joined: 20 Apr 2003 Posts: 4 Location: The Netherlands
|
Posted: Wed Apr 30, 2003 1:54 pm Post subject: assign array-in-array to a section |
|
|
Hi there!
I'm doing some testing with some mysql results and smarty.
But you will have to create an array of the result time after time, like this:
Code: | $query = mysql_query("SELECT * FROM news")or die(mysql_error());
$results = array();
while ( $row = mysql_fetch_array ( $query ) ) {
$results[] = array(
'id' => $row['id'],
'subject' => $row['subject']
);
}
$smarty->assign('results', $results);
$smarty->display('index.tpl'); |
and everytime I want to make another loop trough an different table I will have to change the 'id' and 'subject' etc etc.
well, I'm very lazy so I did some coding and the result is a piece of code that autom. run trough my mysql result and trow them in an array.
this is my code:
http://www.bloodyhound.net/area51/test.phps
using this template:
http://www.bloodyhound.net/area51/templates/test.tpl
and the result:
http://www.bloodyhound.net/area51/test.php
But you wil see that the array $result has 2 arrays in him.
After looping' and while-ing' I assign $result to my template, but it only assings the first array of $result.
I hope you understand it and have a solution for me.
ty, Robbie |
|
Back to top |
|
Wom.bat Smarty Pro
Joined: 24 Apr 2003 Posts: 107 Location: Munich, Germany
|
Posted: Wed Apr 30, 2003 3:51 pm Post subject: |
|
|
mysql_fetch_array returns every column TWO times - one time with numeric index, one time with associative index.
you should always use either mysql_fetch_assoc or mysql_fetch_row to avoid problems with that |
|
Back to top |
|
Robbie Smarty n00b
Joined: 20 Apr 2003 Posts: 4 Location: The Netherlands
|
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Apr 30, 2003 5:57 pm Post subject: |
|
|
but it *does* make a change.
your test-page show, you get a 2 dimensional array with 2 rows and 2 colums. this should be what you want.
with mysql_fetch_array() you will get an array with 2 rows, but 4 columns. |
|
Back to top |
|
Robbie Smarty n00b
Joined: 20 Apr 2003 Posts: 4 Location: The Netherlands
|
Posted: Wed Apr 30, 2003 7:14 pm Post subject: |
|
|
ah, indeed you r right, but how to fix my script then?
its still asigning only the first... |
|
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
|