By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,060 Members | 1,893 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,060 IT Pros & Developers. It's quick & easy.

display results of mysql query in template

P: 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
Share this Question
Share on Google+
5 Replies


Atli
Expert 5K+
P: 5,058
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

P: 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
Expert 5K+
P: 5,058
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

P: 13
thank you sir. I am trying it right now. will confirm as soon as I implement it
Apr 28 '10 #5

P: 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

Post your reply

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