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

Smarty and while/sql query in php

 
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
dk-wamp
Smarty n00b


Joined: 01 Jul 2014
Posts: 4

PostPosted: Tue Jul 01, 2014 6:45 pm    Post subject: Smarty and while/sql query in php Reply with quote

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

Code:
 {$test}
 


But with this code it's shows just the first result. How can I show all the query's rows?
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Jul 01, 2014 7:30 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
dk-wamp
Smarty n00b


Joined: 01 Jul 2014
Posts: 4

PostPosted: Tue Jul 01, 2014 7:47 pm    Post subject: Reply with quote

oh thank you very much, works very good!
Back to top
View user's profile Send private message
dk-wamp
Smarty n00b


Joined: 01 Jul 2014
Posts: 4

PostPosted: Tue Jul 01, 2014 8:43 pm    Post subject: Reply with quote

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Jul 02, 2014 12:30 am    Post subject: Reply with quote

Code:
{foreach $test as $idx=>$val}
    {$test.$idx}
    {$key.$idx}
{/foreach}
Back to top
View user's profile Send private message Visit poster's website
dk-wamp
Smarty n00b


Joined: 01 Jul 2014
Posts: 4

PostPosted: Wed Jul 02, 2014 12:59 am    Post subject: Reply with quote



Thank you! Smile
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Jul 14, 2014 8:46 pm    Post subject: Reply with quote

Why you are not using mysqli_fetch_assoc?...
Why you are not using PDO, already?...
Back to top
View user's profile Send private message
Codez
Smarty Rookie


Joined: 23 Aug 2014
Posts: 7

PostPosted: Thu Nov 13, 2014 5:13 am    Post subject: Reply with quote

mohrt wrote:
Code:
{foreach $test as $idx=>$val}
    {$test.$idx}
    {$key.$idx}
{/foreach}

how to do this with smarty 2?
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