I have this problem when calling the function to dump the data I want.
I have created a function to list some items according to their id:
function list_item_by_id_controller(){
global $link, $gf_query;
$id_types = array('eid','hid','pid','poid','bid');
$getKeys = array_keys($_GET);
foreach ( $getKeys as $k ){
if( in_array($k, $id_types) ){
$id = $_GET[$k];
}
}
if(!$id){
$gf_query['message_code'] = 'i101';
return false;
}
$query = list_item_by_id($id);
if( !$query ){
$gf_query['message_code'] = 'i102';
return false;
}
return $query;
}
When I call the function on the page where I want to print the data, I get the following error:
fatal error: uncaught error: cannot use object of type mysqli_result as array in c:\mamp\htdocs\gourfood_02-08__modificado\public_html\templates\product-single.php:8 stack trace: #0 c:\mamp\htdocs\gourfood_02-08__modificado\public_html\single.php(23): include() #1 {main} thrown in c:\mamp\htdocs\gourfood_02-08__modificado\public_html\templates\product-single.php on line 8
On line 8 of product-single.php
I have the following line of code <h2 class="title"> <?php echo $i['item'] ?></h2>
I have tried changing in the model mysqli_num_rows
by mysqli_fetch_array
if that was the problem but nothing.
This is the model
function list_item_by_id($id){
global $link;
$id = (int)$id;
$sql = "SELECT * FROM items
INNER JOIN categorias ON categorias_id = categorias.id
WHERE items.id = $id";
$query = mysqli_query($link,$sql);
$num_rows = mysqli_num_rows($query);
if( $num_rows > 0 ){
return $query;
}
return false;
}
Could someone tell me what's wrong? Thanks
EDIT: the code around echo $i['item]
is the following:
On the page single.php
<?php
require('load.php');
require('templates/header.php');
require('templates/navbar.php');
$i = list_item_by_id_controller();
?>
<div class="single-main">
<div class="container">
<div class="row">
<ol class="breadcrumb">
<li><a href="index.php">Home</a></li>
<li><a href="index.php#carta">Carta</a></li>
<li class="active">Producto</li>
</ol>
<?php if($i): ?>
<?php include('templates/product-single.php') ?>
<?php else: ?>
<div class="row">
<div class="col-xs-12">
<p>Oooppps, parece que ha habido un error. Si lo deseas, puedes volver a la portada pinchando <a href="index.php"> aquí</a></p>
</div>
</div>
<?php endif; ?>
</div>
</div>
</div>
Then in product-single.php
is where I include the echos
to dump the data:
<article class="product-single">
<div class="col-xs-12 col-md-6">
<img src="images/03-small.jpg" class="img-responsive portfolio-img" alt="Project Title">
</div>
<div class="col-xs-12 col-md-6">
<div class="product-detail">
<h2 class="title"> <?php echo $i['item'] ?></h2>
<h4 class="price"><?php echo $i['precio'] ?> €</h4>
<p class="descripcion"><?php echo $i['descripcion'] ?></p>
<form method="POST" id="orderForm">
<div class="form-group">
<label for="cantidad">Cantidad</label>
<input type="number" class="form-control" id="cantidad" value="1">
</div>
<div id="success"></div>
<button type="submit" class="btn btn-custom btn-lg">Enviar</button>
</form>
</div>
</div>
</article>