I am using smarty (php template engine)
in my project.
however, I keep pump into so many problems and smarty's documentation or their support is not nill.
any way, I am trying to use mysqli prepared statement
in my php code and it just doesn't return any value but if I use normal mysqli functions
it work just fine.
I don't think the issue is from my prepared statement
as I am using it in other parts of my project and it works fine. so I can only think that the issue is from smarty
.
This works:
$sql = "SELECT DISTINCT category FROM $storeShop";
$query = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query); // count the output amount
while($line = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$cvalue[] = $line;
}
// Assign this array to smarty...
$smarty->assign('category', $cvalue);
// Assign this array to smarty...
$smarty->assign('$category', $cvalue);
This is my prepared statement which doesn't work:
$query = "SELECT DISTINCT category FROM $storeShop";
if ($stmt = mysqli_prepare($db_conx, $query)) {
/* execute statement */
mysqli_stmt_execute($stmt);
/* bind result variables */
mysqli_stmt_bind_result($stmt, $category);
/* fetch values */
while ($line = (mysqli_stmt_fetch($stmt))) {
$cvalue[] = $line;
}
/* close statement */
mysqli_stmt_close($stmt);
}
/* close connection */
mysqli_close($db_conx);
// Assign this array to smarty...
$smarty->assign('category', $cvalue);
// Assign this array to smarty...
$smarty->assign('$category', $cvalue);
and this is what i have in my template page:
{section name=category loop=$category}
<li class="odd"><a href="#">{$category[category].category}</a></li>
{/section}
is this issue common with smarty and what is the solution to fix this issue?
Thanks
EDIT:
we are getting close now. I use the following code, however, it will return the first letter of each category
which is very strange. example: if the categories are book
and car
, it will return b
and c
$query = "SELECT DISTINCT category FROM $storeShop";
if ($stmt = mysqli_prepare($db_conx, $query)) {
/* bind result variables */
mysqli_stmt_bind_result($stmt, $category);
/* execute statement */
mysqli_stmt_execute($stmt);
/* fetch values */
while (mysqli_stmt_fetch($stmt)) {
$cvalue[] = $category;
}
/* close statement */
mysqli_stmt_close($stmt);
}
/* close connection */
mysqli_close($db_conx);
// Assign this array to smarty...
$smarty->assign('category', $cvalue);
// Assign this array to smarty...
$smarty->assign('$category', $cvalue);
I think $line
is a smarty function
that will return the whole value of the mysql table
. smarty is a whole different language...
SECOND EDIT:
I used the var_dump
and it returns the correct categories as it should but in my page, I only get the first letter of the each category.
var_dump($cvalue);
will return array(1) { [0]=> string(3) "GFD" } array(2) { [0]=> string(3) "book" [1]=> string(11) "car" } "
var_dump($category);
will return string(3) "book" string(11) "car"