Prestashop: Notice: Undefined index: produits - smarty

I'm new in prestashop: I'm trying to display the result of my query in a tpl file, and I get this error:
Notice: Undefined index: produits
This is my controller code :
class AdminStatproduitController extends ModuleAdminController
{
public function init() {
parent::init();
}
public function initContent()
{
parent::initContent();
$products = Db::getInstance()->ExecuteS('SELECT pp.id_product, ppl.name, pps.quantity
FROM `'._DB_PREFIX_.'product` pp
LEFT JOIN '._DB_PREFIX_.'product_sale pps ON pps.id_product = pp.id_product
LEFT JOIN '._DB_PREFIX_.'product_lang ppl ON ppl.id_product = pp.id_product
WHERE pp.cache_is_pack =0
ORDER BY pps.quantity ASC
LIMIT 3');
$pack = Db::getInstance()->ExecuteS('SELECT pp.id_product, ppl.name, pps.quantity
FROM `'._DB_PREFIX_.'product` pp
LEFT JOIN '._DB_PREFIX_.'product_sale pps ON pps.id_product = pp.id_product
LEFT JOIN '._DB_PREFIX_.'product_lang ppl ON ppl.id_product = pp.id_product
WHERE pp.cache_is_pack =1
ORDER BY pps.quantity DESC
LIMIT 3');
$smarty = $this->context->smarty;
$content = $smarty->fetch(_PS_MODULE_DIR_ . 'statproduit/views/templates/admin/statproduit.tpl');
$this->context->smarty->assign(array('produits'=>$products,
'pack'=> $pack,
'content' => $this->content . $content));
}
and this is my tpl code
<section>
<div id="formAddPaymentPanel" class="bootstrap panel">
<form id="formAddPayment" method="post">
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th><span class="title_box ">Id Produit</span></th>
<th><span class="title_box ">Nom Produit </span></th>
<th><span class="title_box ">Quantité Produit</span></th>
<th><span class="title_box ">Pack Choisie</span></th>
<th></th>
</tr>
</thead>
<tbody>
{foreach from=$produits item=produit}
<tr>
<td>{produit} </td>
<td>product.name</td>
<td>product.quantity</td>
<td>delete</td>
</tr>
</tr>
{/foreach}
</tbody>
</table>
</div>
</form>
</div>
</section>
Any suggestion??
thanks

just change this in the controller:
$this->context->smarty->assign('produits',$products);
$this->context->smarty->assign('pack',$pack);
$content = $this->context->smarty->fetch(_PS_MODULE_DIR_ . 'statproduit/views/templates/admin/statproduit.tpl');
$this->context->smarty->assign('content',$this->content . $content);

The complete code:
public function initContent()
{
parent::initContent();
$products = Db::getInstance()->ExecuteS('SELECT pp.id_product, ppl.name, pps.quantity
FROM `'._DB_PREFIX_.'product` pp
LEFT JOIN '._DB_PREFIX_.'product_sale pps ON pps.id_product = pp.id_product
LEFT JOIN '._DB_PREFIX_.'product_lang ppl ON ppl.id_product = pp.id_product
WHERE pp.cache_is_pack =0
ORDER BY pps.quantity ASC
LIMIT 3');
$pack = Db::getInstance()->ExecuteS('SELECT pp.id_product, ppl.name, pps.quantity
FROM `'._DB_PREFIX_.'product` pp
LEFT JOIN '._DB_PREFIX_.'product_sale pps ON pps.id_product = pp.id_product
LEFT JOIN '._DB_PREFIX_.'product_lang ppl ON ppl.id_product = pp.id_product
WHERE pp.cache_is_pack =1
ORDER BY pps.quantity DESC
LIMIT 3');
$this->context->smarty->assign(array('produits'=>$products,
'pack'=> $pack));
$content = $this->context->smarty->fetch(_PS_MODULE_DIR_ . 'statproduit/views/templates/admin/statproduit.tpl');
}
And for tpl:
<form id="formAddPayment" method="post">
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th><span class="title_box ">Id Produit</span></th>
<th><span class="title_box ">Nom Produit </span></th>
<th><span class="title_box ">Quantité Produit</span></th>
<th><span class="title_box ">Pack Choisie</span></th>
<th></th>
</tr>
</thead>
<tbody>
{foreach from=$produits item=produit}
<tr>
<td>{$produit.id_product} </td>
<td>{$produit.name}</td>
<td>{$produit.quantity}</td>
<td>delete</td>
</tr>
{/foreach}
</tbody>
</table>
</div>
</form>

Related

Laravel : "Object of class stdClass could not be converted to string" while pass variable into closure

I get an error like this
ErrorException
This my link index
<td>{{$cmface}}</td>
This my Routes
Route::get('/detail_face_to_face/{id}', 'FaceToFaceController#detail')->name('detail_face_to_face');
This my Controller
public function detail($id)
{
$xclass = DB::table('face_to_face')->select('Class')->where('id', $id)->first();
$tface = DB::table('tbclass')
->select('tbclass.F_Name','tbclass.Class')
->where('tbclass.Class', $xclass)
->whereNotExists(function ($query) use ($id) {
$query->select('id_user')
->from('absent')
->where([['absent.id_face_to_face', $id],['absent.type_face_to_face', '1'],])
->whereRaw('absent.id_user = tbclass.ID_No');
})
->orderBy('tbclass.F_Name', 'ASC')
->paginate(10);
return view('face_to_face.detail',['tface' => $tface]);
}
This my face_to_face.detail page
<table class="table">
<thead style="white-space:nowrap;">
<tr>
<th>No</th>
<th>Name</th>
<th>Class</th>
</tr>
</thead>
<tbody style="white-space:nowrap;">
#if($tface->count()===0)
<tr>
<td class="table-success text-center" colspan="10"><< Data is Empty >></td>
</tr>
#else
#foreach($tface as $no => $tm)
<tr>
<td>{{ ++$no + ($tface->currentPage()-1) * $tface->perPage() }}</td>
<td>{{$tm->F_Name}}</td>
<td>{{$tm->Class}}</td>
</tr>
#endforeach
#endif
</tbody>
</table>
{{ $tface->links() }}
if I click the link on the index page, i get that error
Can anyone help ???
Could you dd($xclass)?
Error is in, ->where('tbclass.Class', $xclass). You probably do ->where('tbclass.Class', $xclass->Class)
Regard

multiple users for shopping cart

currently I'm doing a bookstore system. almost similar with another shopping cart like amazon or whatsoever. my problem is how to apply multiple users to add product on cart . i do have login session and i have set MM_username for each user login. but when users add product into cart, it only appears name of one user.
here's the code below
if(isset($_POST["place_order"]))
{
$insert_order = "
INSERT INTO tbl_order(customer_id, creation_date, order_status)
VALUES(103, now(), 'pending')
";
$order_id = "";
if(mysqli_query($connect, $insert_order))
{
$order_id = mysqli_insert_id($connect);
}
$_SESSION["order_id"] = $order_id;
$order_details = "";
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
$order_details .= "
INSERT INTO tbl_order_details(order_id, product_name, product_price, product_quantity)
VALUES('".$order_id."', '".$values["product_name"]."', '".$values["product_price"]."', '".$values["product_quantity"]."');
";
}
if(mysqli_multi_query($connect, $order_details))
{
unset($_SESSION["shopping_cart"]);
echo '<script>alert("You have place an order...your order status will be inform through phone call within maximum 3 days..Thank you")</script>';
echo '<script>window.location.href="cart.php"</script>';
}
}
if(isset($_SESSION["order_id"]))
{
$customer_details = '';
$order_details = '';
$total = 0;
$query = '
SELECT * FROM tbl_order
INNER JOIN tbl_order_details
ON tbl_order_details.order_id = tbl_order.order_id
INNER JOIN tbl_customer
ON tbl_customer.CustomerID = tbl_order.customer_id
WHERE tbl_order.order_id = "'.$_SESSION["order_id"].'"
';
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result))
{
$customer_details = '
<label>'.$row["CustomerName"].'</label>
<p>'.$row["Address"].'</p>
<p>'.$row["City"].', '.$row["PostalCode"].'</p>
<p>'.$row["Country"].'</p>
';
$order_details .= "
<tr>
<td>".$row["product_name"]."</td>
<td>".$row["product_quantity"]."</td>
<td>".$row["product_price"]."</td>
<td>".number_format($row["product_quantity"] * $row["product_price"], 2)."</td>
</tr>
";
$total = $total + ($row["product_quantity"] * $row["product_price"]);
}
echo '
<h3 align="center">Order Summary for Order No.'.$_SESSION["order_id"].'</h3>
<div class="table-responsive">
<table class="table">
<tr>
<td><label>Customer Details</label></td>
</tr>
<tr>
<td>'.$customer_details.'</td>
</tr>
<tr>
<td><label>Order Details</label></td>
</tr>
<tr>
<td>
<table class="table table-bordered">
<tr>
<th width="50%">Product Name</th>
<th width="15%">Quantity</th>
<th width="15%">Price</th>
<th width="20%">Total</th>
</tr>
'.$order_details.'
<tr>
<td colspan="3" align="right"><label>Total</label></td>
<td>'.number_format($total, 2).'</td>
</tr>
</table>
</td>
yes i know i shouldn't have put 103 as customer_id and i know that is the reason it will only read 103 user, but i have tried many ways include filter MM_username, ' ', $value and whatsoever. but it didn't work.

Laravel5.1, Relation, EloquentORM, Many to Many, Sort

<table border="true">
<tr>
<th style="width:100px">Table</th>
<th colspan="3" style="width:300px">Columns</th>
</tr>
<tr>
<td style="width:100px">Articles</td>
<td style="width:100px">id</td>
<td colspan="2" style="width:200px; color: #aaaaaa">other dependent variables</td>
</tr>
<tr>
<td style="width:100px">Article_Tag</td>
<td style="width:100px">id</td>
<td style="width:100px">article_id</td>
<td style="width:100px">tag_id</td>
</tr>
<tr>
<td style="width:100px">Tags</td>
<td style="width:100px">id</td>
<td style="width:100px">name</td>
<td style="width:100px"></td>
</tr>
</table>
Then, I want to sort Articles table by [name] of Tags table.
I tried eager-loading with closure below, but it did'nt work.
Model Article and Tag are connected with each other just by belongsToMany,
and I succeed in output their data, but they weren't sorted.Offcourse, I did'nt give getTag any argument.(When I gave argument, they weren't narrowed by [name] either.)
use App\Article;
use App\Tag;
...
public function __construct(Article $article)
{
$this->article = $article;
}
...
public function getTag($tag = NULL)
{
$flag = isset($tag);
$articles = $this->article->orderBy('created_at', 'desc')->with([
'tags' => function($query) use ($flag, $tag){
($flag)
? $query->where('name', $tag)
: $query->orderBy('name');
}
])->paginate(15);
Try this variant:
$articles = $this->article->with([
'tags' => function($query) use ($flag, $tag){
return $flag
? $query->where("name", $tag);
: $query->orderBy("name")
}
])->paginate(15);

sort by alphabetical field

I need alphabetical order of how the name from a list. What I need to do while I occupied the pager CakePHP but I could not be. I thought in order by the field (in this case the name) so DESC and ASC does not work me in any case.
In Controller :
$conditions = array('active !=' => -1, 'NOT' => array('role_id' => 2));
$users = $this->paginate('User', $conditions);
$this->set(compact('users','filter'));
In View :
<div class="row">
<table class="table table-striped table-hover table-condensed col-lg-12">
<thead>
<tr>
<th>#</th>
<th><?=$this->Paginator->sort('username', h(__('User')));?></th>
<th><?=$this->Paginator->sort('name');?></th>
<th><?=$this->Paginator->sort('email');?></th>
<th><?=$this->Paginator->sort('created', h(__('Registration date')));?></th>
<th><?=$this->Paginator->sort('active', h(__('Status')));?></th>
</tr>
</thead>
<tbody>
<?php
$offset = $this->Paginator->counter(array('format' => '{:start}'));
foreach($users as $user) {
echo '<tr onclick="window.location.href=\''.$this->Html->url(array('controller'=>'users','action'=>'view',
$user['User']['id'])).'\'">'.
'<td>'.($offset++).'</td>'.
'<td>'.h($user['User']['username']).'</td>'.
'<td>'.h($user['User']['name'].' '.$user['User']['father_surname'].' '.$user['User']['mother_surname']).'</td>'.
'<td>'.str_replace('#','[at]',h($user['User']['email'])).'</td>'.
'<td>'.$this->Time->format('d-m-Y H:i',strtotime($user['User']['created'])).'</td>'.
'<td>';
if ($user['User']['active'] == 1)
echo '<span class="label label-success">'.h(__('Active')).'</span>';
else
echo '<span class="label label-warning">'.h(__('Blocked')).'</span>';
echo '</td>'.
'</tr>';
}
?>
</tbody>
</table>
</div>
Add the following after your conditions
$this->User->order(
array('User.username ASC') //use whatever field you want to sort
);

how to get result of query in view in codeigniter

I have use two query to get result on the view.
To get the result of resultq1 i use foreach but how can i get result of resultq2 in view.
For each row of resultq1 i get record "reccount" in resultq2.
//controller
//Query 1
$q = $this->db->select(array(
'spf.id as id' ,
'spf.name',
"if(u.username != '',u.username,spf.added_by) as added_by ",
'spf.added_on'
))->from('sp_forum spf')->join('sp_users u', 'spf.added_by = u.id', 'left')->where($where)->order_by('spf.id desc')->limit(10, $page * 10)->get();
$resultq1= $q->result_array();
$data['resultq1'] = $resultq1;
$resultq2 = array();
$i=0;
foreach($resultq1 as $rec)
{
//query 2
$id = $rec['id'];
$q1 = $this->db->select("count('id') as reccount ")->from('sp_forum_topic spft')->where('spft.forumid',$id)->get();
$resultq2[$i] = $q1->row_object();
$i++;
}
$this->load->view('forumview', $data, true);
//view file
<table width="100%">
<tr>
<td class="th"> Details</td>
<td width="5%" class="th"> Answer</td>
<td width="15%" class="th">Started by</td>
<td width="15%" class="th">Created on</td>
</tr>
<?php foreach($resultq1 as $row):?>
<tr>
<td><?php echo $row['name'] ;?></td>
<td >---- </td> // here i want to use resultq2
<td><?php echo $row['added_by'] ;?></td>
<td ><?php echo $row['added_on'];?></td>
</tr>
<?php endforeach;?>
</table>
Array print in view for resultq2.
Resultq1 have 4 rows so that i get 4 value in resultq2.
Array ( [0] => stdClass Object ( [reccount] => 0 ) [1] => stdClass Object ( [reccount] => 0 ) [2] => stdClass Object ( [reccount] => 0 ) [3] => stdClass Object ( [reccount] => 2 ) ) 0002
You need to pass $resultq2 to the view as well.
In the controller, just before calling the view
$data['resultq2'] = $resultq2
You can now use $resultq2 in your view.
PS - SQL queries should be in models, not controllers.
Just Write '$data['resultq2'] = $resultq2' before loading the view,
$data['resultq2'] = $resultq2
$this->load->view('forumview', $data, true);
And you can retrieve it in the view by $result2 variable.
Just do var_dump($result2); in your view file, You will get the complete data of $result2 varaible.
And xbonez is right, do write your DB queries in the model, controller is only for login, and coordinating between (models, views, libraries, helper etc...)
I optimized your second query a bit, but logic is pretty much the same.
$q = $this->db->select(array(
'spf.id as id' ,
'spf.name',
"if(u.username != '',u.username,spf.added_by) as added_by ",
'spf.added_on'
))->from('sp_forum spf')->join('sp_users u', 'spf.added_by = u.id', 'left')->where($where)->order_by('spf.id desc')->limit(10, $page * 10)->get();
$resultq1= $q->result_array();
$data['resultq1'] = $resultq1;
$ids = array();
foreach($resultq1 as $result)
$ids[] = $result['id'];
$resultq2 = $this->db->select("count('id') as reccount ")->from('sp_forum_topic spft')->where_in('spft.forumid',$ids)->get();
foreach( $resultq2->result_array() as $res )
$newArr[$res['forumid']] = $res;
$data['resultq2'] = $newArr;
After resultq2 is indexed by original id, its easy to use id from resultq1 loop to show correct data
<table width="100%">
<tr>
<td class="th"> Details</td>
<td width="5%" class="th"> Answer</td>
<td width="15%" class="th">Started by</td>
<td width="15%" class="th">Created on</td>
</tr>
<?php foreach($resultq1 as $row):?>
<tr>
<td><?php echo $row['name'] ;?></td>
<td >
<?php
print_r( $resultq2[$row['id']]);
?>
</td> // here i want to use resultq2
<td><?php echo $row['added_by'] ;?></td>
<td ><?php echo $row['added_on'];?></td>
</tr>
<?php endforeach;?>
</table>
This should fix it.
Modify the controller - see comments also
<?php
//Query 1
$q = $this->db->select(array(
'spf.id as id',
'spf.name',
"if(u.username != '',u.username,spf.added_by) as added_by ",
'spf.added_on'
))->from('sp_forum spf')->join('sp_users u', 'spf.added_by = u.id', 'left')->where($where)->order_by('spf.id desc')->limit(10, $page * 10)->get();
$resultq1 = $q->result_array();
$data['resultq1'] = $resultq1;
$resultq2 = array();
foreach ($resultq1 as $rec) {
//query 2
//echo $id = $rec['id']; //Test and See if all IDs echo out correctly
$data["id"] = $id; //Add this for the ID
$q1 = $this->db->select("count('id') as reccount, spft.forumid")->from('sp_forum_topic spft')->where('spft.forumid', $id)->get();
$resultq2[$id] = $q1->result(); //Change this line
$data["resultq2"][$id] = $resultq2[$id]; //Add this line
}
//echo "<pre>";
//$export = var_export($data, true);// Test the returned value...
//echo "</pre>";
$this->load->view('forumview', $data, true);
?>
In Your view
<table width="100%">
<tr>
<td class="th"> Details</td>
<td width="5%" class="th"> Answer</td>
<td width="15%" class="th">Started by</td>
<td width="15%" class="th">Created on</td>
</tr>
<?php //foreach ($resultq1 as $row): ?>
<?php foreach ($data as $row): ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td>
<?php
//$resultq2 is now array
//print_r($resultq2); //Do this so you can see the depth of the array
foreach ($resultq2 as $counts) {
foreach ($counts as $count) { //The second foreach might be necessary - please test
echo $count->reccount; //This should print out the count result
//echo "<br />";
}
}
?>
</td>
<td><?php echo $row['added_by']; ?></td>
<td ><?php echo $row['added_on']; ?></td>
<td >
</td>
</tr>
<?php
endforeach;
exit;
?>
</table>
I haven't had time to test it but it should work. Let me know if you find any issues:

Resources