View previous topic :: View next topic |
Author |
Message |
dk-wamp Smarty n00b
Joined: 01 Jul 2014 Posts: 4
|
Posted: Tue Jul 01, 2014 6:45 pm Post subject: Smarty and while/sql query in php |
|
|
Hello, this is my first post (I know smarty by 1 day).
I have a problem with my php / tpl
I have this query code in my php:
Code: | $result = mysqli_query($con,"SELECT * FROM videofiles WHERE usr_id=1) ;
while ($row = mysqli_fetch_array($result)) {
$file_code= $row['file_name'];
}
$smarty->assign('test', $file_code);
$class_smarty->displayPage('frontend','tpl_myvideos'); |
I put this code in tmpl_myvideos
But with this code it's shows just the first result. How can I show all the query's rows? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Jul 01, 2014 7:30 pm Post subject: |
|
|
You are clobbering your looped variable, use $filecode[] to append to an array:
Code: | $result = mysqli_query($con,"SELECT * FROM videofiles WHERE usr_id=1) ;
while ($row = mysqli_fetch_array($result)) {
$file_code[] = $row['file_name'];
}
$smarty->assign('test', $file_code);
$class_smarty->displayPage('frontend','tpl_myvideos'); |
then in the template, loop the array:
Code: | {foreach $test as $foo}
{$foo}
{/foreach} |
|
|
Back to top |
|
dk-wamp Smarty n00b
Joined: 01 Jul 2014 Posts: 4
|
Posted: Tue Jul 01, 2014 7:47 pm Post subject: |
|
|
oh thank you very much, works very good! |
|
Back to top |
|
dk-wamp Smarty n00b
Joined: 01 Jul 2014 Posts: 4
|
Posted: Tue Jul 01, 2014 8:43 pm Post subject: |
|
|
Sorry mohrt but I have another problem,
If I would use 2 ore more array to put records in tables?
I use for example this code:
Code: | $result = mysqli_query($con,"SELECT * FROM videofiles WHERE usr_id=1) ;
while ($row = mysqli_fetch_array($result)) {
$file_code[] = $row['file_name'];
$file_key[] = $row['file_key'];
}
$smarty->assign('test', $file_code);
$smarty->assign('key', $file_key);
$class_smarty->displayPage('frontend','tpl_myvideos');
|
and in tpl can I use foreach to create a row with data? Or how can I do it?
I would like result:
$filecode[0] - $file_key[0]
$filecode[1] - $file_key[1]
etc etc
Thank you for your support. You're very kind. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Jul 02, 2014 12:30 am Post subject: |
|
|
Code: | {foreach $test as $idx=>$val}
{$test.$idx}
{$key.$idx}
{/foreach} |
|
|
Back to top |
|
dk-wamp Smarty n00b
Joined: 01 Jul 2014 Posts: 4
|
Posted: Wed Jul 02, 2014 12:59 am Post subject: |
|
|
Thank you! |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Mon Jul 14, 2014 8:46 pm Post subject: |
|
|
Why you are not using mysqli_fetch_assoc?...
Why you are not using PDO, already?... |
|
Back to top |
|
Codez Smarty Rookie
Joined: 23 Aug 2014 Posts: 7
|
Posted: Thu Nov 13, 2014 5:13 am Post subject: |
|
|
mohrt wrote: | Code: | {foreach $test as $idx=>$val}
{$test.$idx}
{$key.$idx}
{/foreach} |
|
how to do this with smarty 2? |
|
Back to top |
|
|