0

Im n00b, so basically i want to call my query which is located in my index.php file from my .tpl file using smarty:

Index.php

<?php

//Database connection
$db = mysqli_connect('xx','xx','','xx')
or die('Error connecting to MySQL server.');

//access Smarty template engine
require_once('Smarty-3.1.30/libs/Smarty.class.php');

$smarty = new Smarty();
$smarty->template_dir = 'views';
$smarty->compile_dir = 'tmp';


//query product page
$query = "SELECT * FROM cs_shop";
mysqli_query($db, $query) or die('Error querying database.');

$result = mysqli_query($db, $query);
$row = mysqli_fetch_array($result);


//query
while ($row = mysqli_fetch_array($result)) {
$row['product_category'] . ' ' . $row['product_price'] . ': ' . 
$row['product_quantity'] . ' ' . $row['product_about'] .' ' 
.$row['product_color'] .'<br />';
    }

//db collect data
$smarty->assign('row', $row); 
//template
$smarty->display('index.tpl');

mysqli_close($db);
?>

The while loop i use in the index.php is what i want to call in my .tpl file, im new to smarty and cant get it to work , test database connection and it worked, my , Smarty gets called no errors.

Its a basic static page im just doing experiment, using Smarty, So i just want to display the query as list no td's or anything like that.

So can someone give me a example how my .tpl file would look located in my 'views' directory if i want to display the query?

Thanks in advance

3
  • 1
    To loop over an array and access the items inside, you can use a foreach: {foreach from=$row item="item"} {$item} {/foreach}. This will output everything in $row, which is an array. Also your code doesn't work now, there are errors, e.g. inside the while you contenate strings but don't assign them to a variable. Commented May 12, 2017 at 8:34
  • nice , so can i ask you to maybe just give me an example of assigning e.g. 'product_category' to a variable. And this would happen in the index.php file at the top of all my code?( so i first declare the variables and then the query? And also how would the while loop look like calling the variable? thanks Commented May 12, 2017 at 8:40
  • Your while loop doesn't makes any sense. You're creating a string but not assigning it to any variable. You have to read all the data, assign it to an array variable and pass that variable to smarty Commented May 12, 2017 at 8:51

2 Answers 2

1

A short example how to display a few words from an array as options within a <p> tag:

index.php

$rows = ['hello', 'there', 'this', 'is', 'me'];
$smarty->assign('rows', $rows);
$smarty->display('index.tpl');

index.tpl

// head, css etc, doesn't matter here

<p>
  {foreach from=$rows item="item"}
    {$item}<br>
  {/foreach}
</p>

This will produce some code which evaluates to:

<p>
  hello<br>
  there<br>
  this<br>
  is<br>
  me<br>
</p>

And as interpreted HTML:

hello
there
this
is
me

As the content of the variables you can pass (almost) whatever you want.

Sign up to request clarification or add additional context in comments.

6 Comments

thanks allot!! This is exactly what i was looking for.
No problem, nice i could help you.
sorry, one thing i forgot to ask, so in my database i have the tables ('product_category','product_price'......) how would i assign them to variable to call them in the while loop. Hope it makes sense? if you look in my code above you can see im calling a query by selecting all from cs_shop and then $row calls the query..im a bit confused.
You are speaking of columns, not tables. Remember the following: You loop through your results with mysqli_fetch_array() this means If you have lets say the columns id and value in your table, $row inside the while loop would look like [ ['id'] => 23, ['value'] => 'anime' ]. so when you are inside the foreach in smarty you will get excatly this as $item. You can then access the values like $item['id'].
i get what you say and yess sorry i mean columns, and thanks again for lecturing me haha but im not getting my syntax correct, would you mind showing me how this would look like? im pushing the limits now i know but this is getting to me... like im not sure should i declare a variable for each column and then call the variable in my loop.. and then call the varable assigned in the loop for the .tpl . Doing it without assigning database colums is okay becuase you make your own custom variables, but im assigning my variable to a column right , and i think my sytax of doing this is wrong.
|
0

Got this to work, here is what i did.

In my index.php (showing only what i know is not-correct and correct now)

<?php
$new = ['product_category','product_price','product_quantity','product_about','product_color'];

//added an array - rows
$rows = array();

//while loop calling array adding products columns to it
while ($row = mysqli_fetch_array($result)) {
 $rows[] = array(
    'product_category' => $row['product_category'],
    'product_price' => $row['product_price'],
    'product_quantity' => $row['product_quantity'],
    'product_about' => $row['product_about'],
    'product_color' => $row['product_color']
);
}

//db collect data - calls rows
$smarty->assign('row', $rows); 
//template
$smarty->display('index.tpl');

index.tpl

 <p>
  {foreach from=$row item="item"} 

     {$item['product_category'] }<br />

  {/foreach}
 </p>

like i said im totally noob in this, but got it to display. Thanks @Tobias .F

3 Comments

Add brackets {} around $item['product_category'].
i did now and shows no results (not even an error). The way i assigned the columns and called them in the loop correct?
My bad...it seems i made an error, try accessing the values as {$value.product_catagory}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.