can i make nested for each in cakephp - cakephp-2.1

I have a problem in cakephp
controller
$due = $this->Issue->find('all' , array('conditions' => array ('Issue.user_id'=>6)));
$this->set('due',$due);
$id = $due['0']['Issue']['book_id'];
$b = $this->Bookmaster->find('first' , array ('conditions' => array ('Bookmaster.id' => $bookid)));
$book = $this->Book->find('all',array('conditions' => array ('Book.id' =>$id)));
$bookid = $book['0']['Book']['bookmaster_id'];
Ctp contain
<h1> ISSUED BOOK DETAILS </h1>
<table width="200" border="2" bordercolor="#009933">
<tr>
<td> BOOK ID </td>
<td> TITLE </td>
<td> AUTHOR </td>
<td> EDITION </td>
<td>ISSUED ON </td>
<td> DUE DATE </td>
<td> POTENTIAL FINE</td>
</tr>
<?php foreach ($due as $data ) { ?> <tr>
<td> <?php echo $data['Issue']['book_id']; ?> </td>
<td> <?php echo $b ['Bookmaster']['title'] ;?></td>
<td> <?php echo $b['Bookmaster']['author'];?></td>
<td> <?php echo $b ['Bookmaster']['edition'];?> </td>
<td> <?php echo $data['Issue']['issue_date_time']; ?> </td>
<td> <?php echo $data['Issue']['due_date']; ?> </td>
<td> <?php echo $out['Fine']['fineamount'];?> </td>
</tr> <?php }?>
</table>
My problem is how can I manipulate with this with for loop.
My loop is not iterating for bookmaster.. so how can I do that..
How can i increment the value of bookid

The problem is you are looping on $due, and the $b data is a key valued array (i.e. $b[0]['bd'], $b[1]['bd'], $b[2]['bd'], etc.) Therefore, the key (or the index) for the array you are searching for will not ever be aligned with the $due loop.
A better solution is to write a join so each record contains the information you require and the loop will be able to display the data you require. More requirements / information is required on your part to enable anyone here to offer a suggestion.

Related

Laravel Nested #foreach By 2 Different Model in View

Model
// Relationship in \App\FatherRegistrars and \App\MotherRegistrars and \App\GuardianMaleRegistrars \App\GuardianFemaleRegistrars
public function student_registrars()
{
return $this->belongsToMany('App\StudentRegistrars')->withTrashed();
}
Controller
public function index(Request $request)
{
$dataFathers = \App\FatherRegistrars::get();
$dataMothers = \App\MotherRegistrars::get();
$dataGM = \App\GuardianMaleRegistrars::get();
$dataGF = \App\GuardianFemaleRegistrars::get();
// manual pagination using code attached in AppServiceProvider.php
$data = $dataFathers->toBase()->merge($dataMothers)->paginate($items);
return view('parents-guardians-registrars.index', compact('data', 'dataFathers', 'dataMothers', 'dataGM', 'dataGF'))->withItems($items);
}
View
#foreach($data as $var)
<tr>
<td style="text-align:center;">
<input type="checkbox" id="select" class="sub_chk" data-id="{{$var->id}}" value="{{$var->id}}" name="selected_values[]"/>
</td>
<td>{{$var->id }}</td>
<td>{{$var->name}}</td>
<td>{{$var->email}}</td>
<td>
<?php $elements = array(); ?>
#foreach($var->student_registrars as $category)
<?php $elements[] = ' '.$category->name.' '; ?>
#endforeach
<?php echo implode(',<br>', $elements); ?>
</td>
<td>
// Second foreach should be here
</td>
<td>
Detail
</td>
#endforeach
// Second foreach
#foreach($dataGM as $var2)
<tr>
<td>
<?php $elements = array(); ?>
#foreach($var2->student_registrars as $category)
<?php $elements[] = ' '.$category->name.' '; ?>
#endforeach
<?php echo implode(',<br>', $elements); ?>
</td>
</tr>
#endforeach
</tr>
And the result for code above is:
I have a little problem related multiple foreach in one view. It's actually just a simple problem but I am stuck here. Any body can solve it?
Should I use partial view to do this?
This may not exactly work (not tested or verified), but you can give something like this a try:
#foreach($data as $var)
<tr>
<td style="text-align:center;">
<input type="checkbox" id="select" class="sub_chk" data-id="{{$var->id}}" value="{{$var->id}}" name="selected_values[]"/>
</td>
<td>{{$var->id }}</td>
<td>{{$var->name}}</td>
<td>{{$var->email}}</td>
<?php $elements = array(); ?>
#foreach($var->student_registrars as $i => $category)
<td>
<?php $elements[] = ' '.$category->name.' '; ?>
<?php echo implode(',<br>', $elements); ?>
</td>
<td>
<?php $elements2 = array(); ?>
#foreach($var2->student_registrars[$i] as $category)
<?php $elements2[] = ' '.$category->name.' '; ?>
#endforeach
<?php echo implode(',<br>', $elements2); ?>
</td>
#endforeach
<td>
Detail
</td>
</tr>
#endforeach
The problem you have is that there is no direct association $data and $dataGM. You should try and use Relationships so that the data needed for $dataGM can be accessed as a relationship from $data, e.g.
$children = $data[$i]->children;
$adopted = $data[$i]->adopted_children;
Then you can loop directly on these items instead of having to create multiple variables.

Is there a way to send array of textbox and checbox from View To Controller (CodeIgniter)

I am building a store front with CodeIgniter where list of items from the database is displayed with a checkbox in the View. The user is to select items and enter their quantity. When the user Click Send, the list of Items and their quantity that user set will be submitted to the Database.
Here is the View Code:
<table>
<thead>
<tr>
<th>SN</th>
<th>Product</th><th>Tick</th>
<th>quantity</th>
</tr>
</thead>
<tbody>
<?php
$i=0;
foreach($it as $r):
$i++;
?>
<tr><td><?php echo $i; ?></td>
<td><?=$r['Item'];?></td>
<td>
<input type="checkbox" name="item[]" value="<?=$r['Item']?>" /></td>
</td>
<td>
<input type="number" name="qty[]" />
</td>
</tr> <?php endforeach?>
<tr><td colspan="3">
<input type="submit" name="btnub" value="Send Order" /></td></tr>
</tbody>
</table>
<!-- Controller Code (CodeIgniter) -->
<!-- CodeIgniter Controller Code to harvest the Order -->
if (isset($_POST['btnub'])) {
foreach($_POST['item'] as $key=>$item) {
for ($i = 1; $i < 2000; $i++) {
$h = "qty" . $i;
$qty = $this->input->post[$h];
}
$tt = array(
'item' => $item,
'Qty' => $qty,
);
$this->tranmodel->insertthing("cuzorder", $tt);
} }

I can not access the array that I attached to my pivot table?

I am using Many to Many relationships between Students and Packages which generate student_package pivot table. And I attached some extra variables, but I can not access them in my view.
Here is my code
Controller
$student->packages()->attach($request->package_id, ['paid' => '1','transaction_id'=>$transaction_id, 'amount' => $request->amount]);
View
#foreach ($student->packages as $package)
<tr>
<td> {{$package->id}} </td>
<td> {{$package->name}} </td>
<td> {{$package->amount}} </td>
<td> {{$package->no_hours}} </td>
<td> {{$package->transaction_id}} </td>
<td> {{$package->paid}} </td>
</tr>
#endforeach
I can't access the extra parameters only. the rest are accessible.
1) Add columns that you need to withPivot() method of belongsToMany relation:
public function packages()
{
return $this->belongsToMany(Package::class)->withPivot('paid', 'transaction_id', 'amount');
}
2) Then access pivot columns trough ->pivot property.
#foreach ($student->packages as $package)
<tr>
<td> {{$package->id}} </td>
<td> {{$package->name}} </td>
<td> {{$package->pivot->amount}} </td>
<td> {{$package->no_hours}} </td>
<td> {{$package->pivot->transaction_id}} </td>
<td> {{$package->pivot->paid}} </td>
</tr>
#endforeach
You need to use the pivot attribute of the model as explained here: https://laravel.com/docs/6.x/eloquent-relationships#many-to-many
For example:
$user = App\User::find(1);
foreach ($user->roles as $role) {
echo $role->pivot->created_at;
}
Read the linked docs, it gives a lot of details for various use cases.

Codeigniter: having issue with fetching data as sum of columns

i am new to Codeigniter, i have two tables named as Users and Amount, there are multiple amounts in Amount Table across one user have same user_id which is foreign key. i want to get data and sum up the amounts value if user_id is same.
My Modal is:
public function client_list(){
$this->db->select_sum('*', sum('amount'));
$this->db->from('users');
$this->db->join('amount', 'amount.user_id = users.ID');
$query=$this->db->get();
$result=$query->result();
return $result;
}
Controller is:
public function index(){
$data['records'] = $this->Admin_model->client_list();
$this->load->view('admin/Client_list', $data);
}
View part is:
<?php
foreach($records as $record):
?>
<tr class="odd gradeX">
<td>
<label class="mt-checkbox mt-checkbox-single mt-checkbox-outline">
<input type="checkbox" class="checkboxes" value="1" />
<span></span>
</label>
</td>
<td> <?php echo $record->firstname; ?> </td>
<td> <?php echo $record->lastname; ?> </td>
<td> <?php echo $record->phone;?> </td>
<td> <?php echo $record->email;?> </td>
<td> <?php echo $record->amount;?> </td>
<td> <?php echo $record->dateofbirth;?> </td>
</tr>
<?php endforeach; ?>
Kind Help would be highly appreciated.

store session cart to database

Sorry for asking this old questions, and I know that I've read before I ask here, it's can use database for adding more cart without limitation. I already try to use ci_sessions table to store session but still no luck, I only can adding 6 items maximum.
please help me, I looking for some example for this almost two days and result is nothing
EDITED
this is my view
<table id="box-table-a" summary="Employee Pay Sheet">
<thead>
<tr>
<th scope="col">Description</th>
<th scope="col">Price</th>
<th class="centered" scope="col">Options</th>
</tr>
</thead>
<tbody>
<?php foreach ($foto_produk->result() as $key => $value) {?>
<tr>
<td><?php echo $value->description;?></td>
<td><?php echo $value->price;?></td>
<td class="centered"><input type="checkbox" name="produk_foto[]" value="<?php echo $value->id;?>" /></td>
</tr>
<?php }?>
</tbody>
</table>
here's my controller code
if($this->input->post('produk_foto')){
$id_foto = $this->input->post('produk_foto');
foreach ($id_foto as $key => $value) {
$this->db->where('id', $value);
$query = $this->db->get('foto_product');
if($query->num_rows() > 0){
foreach($query->result() as $ids => $rows){
echo $rows->id.'<br />';
$data_produk = array(
'user_data'=> array(
'id' => $rows->id,
'price' => $rows->price,
'name' => $rows->description,
'qty' => $rows->aantal
)
);
$this->cart->insert($data_produk);
}
}
}
}
and this my view code
<?php if(!$this->cart->contents()):?>
<div class="alert-box warning">The regular products are empty.</div>
<div class="clearfix"></div>
<?php else:?>
<hr>
<h4>REGULAR PRODUCTS</h4>
<div class="order_detail" id="Display">
<table>
<thead>
<tr>
<th>DESCRIPTION</th>
<th>QUANTITY</th>
<th>PRICE PER ITEM(S)</th>
<th>TOTAL</th>
<th>REMOVE</th>
</tr>
</thead>
<tbody>
<?php foreach($this->cart->contents() as $rows):?>
<tr>
<td style="font-weight:bold;"><?php echo $rows['name'];?></td>
<td>
<?php echo form_open(current_url());?>
<input type="text" size="3" name="quantity" value="<?php echo $rows['qty'];?>" />
<input type="hidden" size="3" name="rowid" value="<?php echo $rows['rowid'];?>" />
<input type="submit" name="update" value="Update" />
<?php echo form_close();?>
</td>
<td><?php echo $rows['price'];?></td>
<td><?php echo $this->cart->format_number($rows['subtotal']);?></td>
<td>delete</td>
</tr>
<?php endforeach;?>
</tbody>
<tfoot>
<tr>
<td colspan="3">Total Products</td>
<td colspan="3">€ <?php echo $this->cart->format_number($this->cart->total());?></td>
</tr>
<tr>
<td colspan="3">Total Shipping</td>
<td colspan="3"></td>
</tr>
<tr>
<td colspan="3"></td>
<td colspan="3" style="padding:0;text-align:center;">
<p>TOTAL :</p>
<span class="tot">€ <?php echo $this->cart->format_number($this->cart->total());?></span>
</td>
</tr>
</tfoot>
</table>
</div>
<?php endif;?>
with this code I want to insert using checkbox with array, and I have more than 6 checkbox
thank you in advance
Ok it's solved by my self..
I don't know that in the name of product there are special characters..
and shame of me..
but thank you anyway

Resources