473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

display results of mysql query in template

13
Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query("SELECT * FROM product inner join members ON product_member_id = members_sid WHERE members_username='username'") ;    
  2.  
  3. while($row = mysql_fetch_array($result))
  4.   {
  5.       $template->assign_vars(array(
  6.         'PRODUCT' => $row['product_title'],
  7.         'USERNAME' => $row['members_username'],
  8.         'COUNTRY' => $row['members_country'],
  9.  
  10.     ));
when I use this code, I try to display all the products linked to the 'username', it only displays ONE product.
I am trying to get it to display all of the products.I used the while loop ...it still displays just one.
any suggestions? Thank you
Apr 26 '10 #1
5 2611
Atli
5,058 Expert 4TB
Hey.

Knowing nothing about the template class you are using, I would guess that you keep overwriting the same variables each loop, therefore only displaying the last row returned by your query.

If you need to pass multiple values for the same variable, you need to use an array.
Apr 26 '10 #2
fogsys
13
any example?
to assign a variable to the template you use :
$template->assign_vars(array(
'PRODUCT' => $row['product_title']
'variable' => $variable
etc etc))
Apr 26 '10 #3
Atli
5,058 Expert 4TB
What you are assigning the template right now is a list of variables that looks something like this:
Expand|Select|Wrap|Line Numbers
  1. $var = array(
  2.     'PRODUCT' => 'p1',
  3.     'USERNAME' => 'u1',
  4.     'COUNTRY' => 'c1'
  5. );
I'm assuming here that each of the element names there ("PRODUCT", "USERNAME" and "COUNTRY") represents a variable name available to your template.

In that case, to assign a list of values for each of them you would want their values to be an array. Somewhat more like this:
Expand|Select|Wrap|Line Numbers
  1. $var = array(
  2.     'PRODUCT' => array('p1', 'p2', 'p3'),
  3.     'USERNAME' => array('u1', 'u2', 'u3'),
  4.     'COUNTRY' => array('c1', 'c2', 'c3')
  5. );
However, a better approach might be to assign a single two-dimensional array, containing a list of product. That makes it easier to loop through in your template. (Assuming, of course, that your template system is capable of that.)
Expand|Select|Wrap|Line Numbers
  1. $var = array(
  2.     'products' => array(
  3.         array(
  4.             'title' => 'product 1',
  5.             'username' => 'user 1',
  6.             'country' => 'country 1'
  7.         ),
  8.         array(
  9.             'title' => 'product 2',
  10.             'username' => 'user 2',
  11.             'country' => 'country 2'
  12.         ),
  13.         array(
  14.             'title' => 'product N',
  15.             'username' => 'user N',
  16.             'country' => 'country N'
  17.         )
  18.     )
  19. );
Then you would only have a single variable, "products", available in your template, which would essentially be a table of products, each element representing a data row.

In a Smarty template, you could use that as:
Expand|Select|Wrap|Line Numbers
  1. {foreach from=$products item=product}
  2. <p>
  3.     Title: {$product.title}<br>
  4.     User : {$product.username}<br>
  5.     Country: {$product.country}
  6. </p>
  7. {/foreach}
Apr 27 '10 #4
fogsys
13
thank you sir. I am trying it right now. will confirm as soon as I implement it
Apr 28 '10 #5
fogsys
13
oh and just for info,im using the phplib template
if you google it its the first link.But Im trying to figure it out myself.thank you again
Apr 28 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Robert | last post by:
I have a php/mysql query working like so: $Query = "SELECT * FROM $TableName WHERE name LIKE '%".$searchterm."%' " All I want to do now is sort them alphabetically. By using the above...
4
by: danubian | last post by:
Hi, I'm a newbie in php/mysql programming,really am. I'm working on a web-site that allows registration and posterior logging in. Already registered user logs in with valid username and...
0
by: Jim Moseby | last post by:
I stumbled across this while trying to update a table with a timestamp type column. At the time, I didn't know that the timstamp column would update itself when a row was changed. A kind gentleman...
1
by: Guy Erez | last post by:
Hi, I'm running queries with MySql 4.0.17 that return thousands of records. Because I need to present them in GUI, I returieve the results in chunks using LIMIT, for example - get first 100,...
2
by: Gnurp Gnarp | last post by:
When the number of columns get large, it becomes increasingly difficult to view the results of a select in a small window (e.g. 80 line xterm) because of line wrapping. Can I have the results of...
1
by: James | last post by:
Hey everyone, I have a really obscure but impassable problem with a reasonably simple piece of php/PEAR DB/MySQL code When calling several stored procs the first call succeeds, but subsequent...
1
by: SC | last post by:
I'm developing a site that may eventually have a very large database of users (hopefully, but who knows). It will be a community website where users can search each other (think Friendster,...
2
by: ameshkin | last post by:
I know this is probably not too hard to do, but how do I display multiple rows of a mysql query/recordset. Im having trouble doing this. I don't just want to display them, but I want to make sure...
0
by: taras.di | last post by:
Hi everyone, I've come across an extremely strange problem. The exact same query in both mysql command line client, and mysql query browser gives entirely different results. I was hoping someone...
5
by: PHPBABY3 | last post by:
Hi, 1. I have two SQL tables. I will call them employees and departments: EMP: LAST_NAME, FIRST_NAME, DEPTNM DEPT: NUM, NAME Input: text string FIND Output: the LAST_NAME, FIRST_NAME...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.