Problems with view rendering - php

I have a page with list of users in admin panel, it has a view with bootstrap table (users list). Today I noticed that it renders only half or less number of users. After refreshing the page the point in which it stops is different user (might be on 250's user or 1000's). The data from controller is full and solid, I checked it with printing users array. Here the picture of ending the view:
And the code inside table:
<? foreach ($items as $item): ?>
<tr>
<td><strong><?= $item->id; ?></strong></td>
<td>
<?= $item->username ?>
</td>
<td>
<?= $item->name ?>
</td>
<td>
<?= $item->email ?>
</td>
<td>
<?= $item->phone ?>
</td>
<td>
<?= $item->remark ?>
</td>
<td>
<span style="color: <?= $item->validated ? 'green' : 'gray' ?>"><?= $item->validated ? 'Активирован' : 'Не активирован'?></span>
</td>
<td>
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
Действия <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li><a href="<?= Route::url('admin', array(
'controller' => $model->object_name(),
'action' => 'edit',
'id' => $item->id
)) ?>">Редактировать</a></li>
<li><a href="<?= Route::url('admin', array(
'controller' => $model->object_name(),
'action' => 'delete',
'id' => $item->id
)) ?>">Удалить</a></li>
</ul>
</div>
</td>
</tr>
<?php endforeach ?>
Error log is empty
UPD When I try to open this page on the iphone, it starts to load it and the throws error "Operation could not be completed" error 303

Something goes wrong when you call $item->validated for your 250'th user.
<?= $item->validated ? 'green' : 'gray' ?>"><?= $item->validated ? 'Активирован' : 'Не активирован'?>
Check your php logs first and find your error.

Related

Getting a table column data as array with POST request in CakePHP

I have a form that has a table inside it and that table has a quantity column, I want to get the quantity of all products and put them in a quantity array in the controller, is there a way I can do that? For now, it doesn't work when I click Update Cart button. Here is my current code:
index page
<section class="section service border-top">
<div>
<?= $this->Form->create(null) ?>
<fieldset>
<?php if(isset($_SESSION['cart']) && !empty($_SESSION['cart'])){?>
<table id="cart-table" class="table table-bordered table-striped">
<thead>
<tr>
<th><?= 'Products' ?></th>
<th><?= 'Price'?></th>
<th><?= 'Quantity'?></th>
<th><?= 'Sub Total'?></th>
</tr>
</thead>
<tbody>
<?php foreach ($cart_items as $cart_item): ?>
<tr>
<td><?= $cart_item->name;?></td>
<td><span id="price"><?= "$ AU".$cart_item->price;?></span></td>
<td contenteditable='true'><?php echo $this->Form->number('quantity[]',['min'=>1, 'value'=>$cart_item->quantity]); ?></td>
<td><span id="total"><?php echo "$ AU".$cart_item->price * $cart_item->quantity?></span></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php }else{ ?>
You don't have any items in your cart. Add more?
<?php } ?>
</fieldset>
<?= $this->Form->button(__('Make a Payment'), ['class' => 'btn btn-success']) ?>
<?= $this->Form->button(__('Update Cart'), ['controller' => 'Carts', 'action' => 'updateCart'], ['class' => 'btn btn-success']) ?>
<?= $this->Form->end() ?>
<?= $this->Form->postLink(__('Clear Cart'), ['controller' => 'Carts', 'action' => 'clearCart'], ['class' => 'btn btn-danger', 'confirm' => __('Are you sure you want to clear your cart?')]) ?>
</section>
controller
public function updateCart()
{
if(isset($_SESSION['cart']) && !empty($_SESSION['cart'])){
if ($this->request->is('post')) {
$qty = $this->request->getData('quantity[]');
debug($this->request->getData('quantity'));
}
}
else{
$this->Flash->error(__("You don't have anything in your cart. Add more?"));
}
$this->redirect(['controller'=>'Carts','action' => 'index']);
}
Thank you

MethodNotAllowedHttpException When i'm using Post Method [gloudemans-package]

recently I'm trying to create a shopping cart for my assignment project. I'm quite new to this framework, sorry if I asked something stupid :( It's a pleasure for me if you guys can check it for me. Tks
I'm also trying to switch to GET method but still didn't work
The error were:
MethodNotAllowedHttpException
This is my controller
public function cart() {
if (Request::isMethod('post')) {
$product_id = Request::get('product_id');
$product = Product::find($product_id);
Cart::add(array('id' => $product_id, 'name' => $product->name, 'qty' => 1, 'price' => $product->price));
}
if (Request::get('product_id') && (Request::get('increment')) == 1) {
$rowId = Cart::search(array('id' => Request::get('product_id')));
$item = Cart::get($rowId[0]);
Cart::update($rowId[0], $item->qty + 1);
}
if (Request::get('product_id') && (Request::get('decrease')) == 1) {
$rowId = Cart::search(array('id' => Request::get('product_id')));
$item = Cart::get($rowId[0]);
Cart::update($rowId[0], $item->qty - 1);
}
$cart = Cart::content();
return view('cart', array('cart' => $cart, 'title' => 'Welcome', 'description' => '', 'page' => 'home'));}
This is my Route
Route::post('cart', 'HomepageController#cart')->name('cart');
This is my view
#extends('layout')
#section('cart')
<section id="cart_items">
<div class="container">
<div class="breadcrumbs">
<ol class="breadcrumb">
<li>Home</li>
<li class="active">Shopping Cart</li>
</ol>
</div>
<div class="table-responsive cart_info">
#if(count($cart))
<table class="table table-condensed">
<thead>
<tr class="cart_menu">
<td class="image">Item</td>
<td class="description"></td>
<td class="price">Price</td>
<td class="quantity">Quantity</td>
<td class="total">Total</td>
<td></td>
</tr>
</thead>
<tbody>
#foreach($cart as $item)
<tr>
<td class="cart_product">
<img src="images/cart/one.png" alt="">
</td>
<td class="cart_description">
<h4>{{$item->name}}</h4>
<p>Web ID: {{$item->id}}</p>
</td>
<td class="cart_price">
<p>${{$item->price}}</p>
</td>
<td class="cart_quantity">
<div class="cart_quantity_button">
<a class="cart_quantity_up" href=""> + </a>
<input class="cart_quantity_input" type="text" name="quantity" value="{{$item->qty}}" autocomplete="off" size="2">
<a class="cart_quantity_up" href='{{url("cart?product_id=$item->id&increment=1")}}'> + </a>
<a class="cart_quantity_down" href='{{url("cart?product_id=$item->id&decrease=1")}}'> - </a>
</div>
</td>
<td class="cart_total">
<p class="cart_total_price">${{$item->subtotal}}</p>
</td>
<td class="cart_delete">
<a class="cart_quantity_delete" href=""><i class="fa fa-times"></i></a>
</td>
</tr>
#endforeach
#else
<p>You have no items in the shopping cart</p>
#endif
</tbody>
</table>
</div>
</div>
</section> <!--/#cart_items-->
#endsection
You are using a tags, this won't do a POST request, instead, the browser is gonna follow the link to make a GET request. That's why you are getting MethodNotAllowedHttpException since it's only expecting a POST request at that endpoint. The same issue will happen with the delete button you have.
This will be complicated to give you a complete solution in an answer. For what you are trying to do, there are several paths you can take to make this work.
you can have a form(s) in your HTML to make a POST request and probably use buttons to post the increment and decrement the quantity, and/or delete, then reload the page with updated quantity.
you can handle the increment/decrement/delete on the frontend using JS and updating the quantity field and finally submit the form with another button (e.g. Checkout Button).
You can also, make this simpler using a framework like Vue/React to
easily handle the cart behavior/function with reactivity.
Notes:
you should not need to check Request::isMethod('post') if you only mapped it to POST in your routes. You should have 1 method per endpoint anyways.
consider using FormValidation instead of explicitly checking if a value was sent or not https://laravel.com/docs/8.x/validation
You might want to redirect to a show method after updating instead of returning a view.
Example:
<td class="cart_quantity">
<div class="cart_quantity_button">
<form action="{{ url('cart') }}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="product_id" value="{{ $item->id }}">
<button type="submit" class="cart_quantity_up" name="increment"> + </button>
<input class="cart_quantity_input" type="text" name="quantity" value="{{$item->qty}}" autocomplete="off" size="2" readonly>
<button type="submit" class="cart_quantity_down" href='{{url("cart?product_id=$item->id&=1")}}' name="decrease"> - </button>
</form>
</div>
</td>

Search for Products in the User View - Cakephp 3

Friends, I am displaying a user's profile along with the products registered by him. My relationships are correct. I can list all products as normal, along with user information. But my product search field in the user's view doesn't work.
This is my code detail_user.ctp
//user information only above. (Address, Telephone, etc.)
.
.
.
<h2 class="bd-title mt-2" id="content">Products Gallery</h2>
<div class="card mt-2 mb-2">
<div class="card-header">
<?= $this->Form->create('', ['type' => 'get']); ?>
<div class="input-group">
<?= $this->Form->control('keyword', array('class' => 'form-control','name'=> 'search','label' => false,'required' => true,));?>
<div class="input-group-append">
<?= $this->Form->button(__('Search')); ?>
</div>
</div>
</div>
<div class="table-responsive">
<table class="table table-hover">
<tbody>
<?php foreach ($user->products as $product): ?>
<td>
<p class="title mb-0"><?= h($product->name) ?> </p>
<var class="price text-muted">R$ <?= number_format($product->price);?></var>
</td>
<td> <br></td>
<td width="130">
<?= $this->Html->link(__('Open'), ['controller' => 'Produtos','action' => 'details_product','prefix' => false, $product->slug], array('class' => 'btn btn-primary')) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody></table>
</div> <!-- table-responsive .end// -->
</article>
And my method is like this:
public function detailUser($slug)
{
$keyword = $this->request->getQuery('keyword');
$query = $this->Users
->find()
->matching('Products', function(\Cake\ORM\Query $q) {
return $q->where(['name LIKE' => ('%'. $this->request->getQuery('keyword') . '%')]);
});
$usuario = $this->Users
->findBySlug($slug)
->contain(['Products', 'Groups'])
->firstOrFail();
$query = $this->Users->find('all');
$this->set('user', $user);
$this->set('query', $this->paginate());
}
I have difficulties to solve this method. At the moment, when I type the product name, the url changes, but the product is not searched.
I consulted the generated SQL and everything is correct, but it does not filter the word entered in the search field and also does not give an error.
I appreciate any comments!

Displaying multiple columns of data [from a PHP list] in a table in Laravel View

I currently am using a Laravel View with the following HTML that displays data onto a table using the <td> tag.
#if($unrostered)
<table class="table table-hover table-striped">
<thead>
<tr>
<th>#</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php $index = 1; ?>
#foreach($unrostered as $name => $profile)
<tr>
<td>
<?= $index; $index++; ?>
</td>
<td>
<a
href="{{ $profile }}">{{ $name }}
</a>
</td>
</tr>
#endforeach
</tbody>
</table>
#endif
I am currently just using one associative list called $unrostered which contains the key and value called name and profile. This shows two columns of data, like this:
As you can see, the name variable is the name of the person, and the profile is a URL that links to their profile.
My HTML skills aren't good. I want to pass in multiple lists [of the same length] into my HTML table, so each list occupies their own column.
I tried juggling the row and data tags, however, all I was able to achieve were that all the data points occupied only one row instead of columns.
Assume the name of your other lists are $list1, $list2, ... and they have the same format as your $unrostered. Then your foreach code should be changed to:
<?php $index = 1; ?>
#foreach($unrostered as $name => $profile)
<tr>
<td>
<?= $index; $index++; ?>
</td>
<td>
<a
href="{{ $profile }}">{{ $name }}
</a>
</td>
<td>
<a
href="{{ $list1[$name] }}">{{ $name }}
</a>
</td>
<td>
<a
href="{{ $list2[$name] }}">{{ $name }}
</a>
</td>
</tr>
#endforeach

custom foreach with respective title

I have the loop here see the image for reference..
I have the column called Supplier Name
For example there are two row at the same supplier name
so i need the one heading
my expected ouptut is
ABS Agencies
Supplier name - here should be the heading
ABS Agencies - heading
ABS Agencies
ABS Agencies
mycode here
#foreach($prsdkgrids as $prsdkgrid)
<?php if($prsdkgrid->SupplierName == $prsdkgrid->SupplierName){ $SupplierName[] = $prsdkgrid->SupplierName; } ?>
#endforeach
#foreach($prsdkgrids as $prsdkgrid)
<?php if(count($SupplierName) == $SupplierName[$f]) { }else{ echo $f."<tr><td><h3 style='font-size: 15px;'>$prsdkgrid->SupplierName</h3></td></tr>"; } ?>
<tr class="psr_table1_tr" id="PSupdetID<?php echo $prsdkgrid->PSupdetID; ?>" <?php if($prsdkgrid->ApprovalStatus == 1){ echo 'style="background: #d9edf7;"';} ?> >
<td> {{ Form::label('', $prsdkgrid->ArticleNo) }} </td>
<td> {{ Form::label('', $prsdkgrid->Size) }} </td>
<td> {{ Form::label('', $prsdkgrid->SpecialDesc) }} </td>
<td> {{ Form::label('', $prsdkgrid->QtyExp) }} </td>
<td> {{ Form::label('', $prsdkgrid->Coloring) }} </td>
<td> {{ Form::label('', $prsdkgrid->Packaging) }} </td>
<td> {{ Form::label('', $prsdkgrid->Packing) }} </td>
<td> {{ Form::label('', $prsdkgrid->TargetPrice) }} </td>
<td> {{ Form::label('', $prsdkgrid->SupPrice) }} </td>
<td> {{ Form::label('', $prsdkgrid->SupplierName) }}</td>
<td> {{ Form::label('', $prsdkgrid->SupComments) }} </td>
<td> <?php if($prsdkgrid->ApprovalStatus == 0){ ?><div align="center" class="floatleft approvedk" id="ApprvedbyResp<?php echo $prsdkgrid->PSupdetID; ?>">
<li><a class="prs_add_link" href="#"><input class="save_btn Approve" type="button" onclick="ApprvedbyResp(<?php echo $prsdkgrid->PSupdetID; ?>)" value="Approve"></a></li></div><?php } ?> </td>
</tr> <?php $f++; ?>
#endforeach
You can store the supplier_name in a variable and in the loop you can just compare the variable and supplier name. A simple approach is like this since you're using foreach()
$tmp_supplier_name = '';
foreach($prsdkgrids as $prsdkgrid){
echo ($tmp_supplier_name != $prsdkgrid->supplier_name) ? '<tr>SUppliername</tr>' : ''; //compare
$tmp_supplier_name = $prsdkgrid->supplier_name; //store the suppliername that was compared
}
Or another approach is try to manipulate your array and it should be something like this , much readable.
Array(
['supplierA'] =>
Array(
[0] => array(.....)
[1] => array(.....)
),
['supplierB'] =>
Array(
[0] => array(.....)
[1] => array(.....)
)
)
Recreate the array example:
$new_array = [];
$tmp_supplier_name = '';
foreach($prsdkgrids as $prsdkgrid){
if($tmp_supplier_name != $prsdkgrid->supplier_name){
$tmp_supplier_name = $prsdkgrid->supplier_name;
}else{
$new_array[$prsdkgrid->supplier_name]['opt1'] = $prsdkgrid->opt1;
$new_array[$prsdkgrid->supplier_name]['opt2'] = $prsdkgrid->opt2;
//and so on
}
}

Categories