I am trying to fetch data from mysql database and php codeigniter using the data i already have in an array. but it seems am doing something wrong.
$query = $this->db->query("select * from product_order where transaction_code='BfkA'");
foreach ($query->result_array() as $row)
{
$p_id = $row['product_id'];
$this->db->join('product_order', 'product_order.product_id = products.product_id');
$this->db->where('products.product_id', $p_id);
$res=$this->db->get('products');
}
return $res->result_array();
I want the output to be like this in my table
<table border="1">
<tr> <td>Product Name</td> <td>Quantity</td> </tr>
<tr> <td>shoe</td> <td>1</td> </tr>
<tr> <td>bag</td> <td>4</td> </tr>
<tr> <td>watch</td> <td>3</td> </tr>
</table>
but i get
<table border="1">
<tr> <td>Product Name</td> <td>Quantity</td> </tr>
<tr> <td>shoe</td> <td>1</td> </tr>
<tr> <td>shoe</td> <td>1</td> </tr>
</table>
You could use where_in to replace the where method and use array_column to get the product_id array, then you could remove the foreach block :
$query = $this->db->query("select product_id from product_order where transaction_code='BfkA'");
$prod_ids = array_column($query->result_array(), 'product_id');
$this->db->join('product_order', 'product_order.product_id = products.product_id');
$this->db->where_in('products.product_id', $prod_ids);
$res = $this->db->get('products');
return $res->result_array();
Related
i found out a way by someone asking a question, of a way of producing subtotals, i now just wanted to sum values between two tables , in the code from the person i tested and it works perfectly, i just wanted to aggregate more one table to any of the querys , it's summing on the blade.
//resumed controller
public function index() {
$emphayas = DB::select('select * from emphayas');
$treasuries = DB::select('select * from treasuries');
return view('database',['emphayas'=>$emphayas,
'treasuries'=>$treasuries,
]);}
-----------------------------------------------------------------
//resumed blade
<table class=" ">
<tr>
<th class="py-3 bg-cyan-400">Total</th>
<td class="py-3 bg-cyan-400">available_for_use_month</td>
</tr>
#foreach ($emphayas as $emphaya)
<tr>
<td></td>
<td>{{ $emphaya->available_for_use_month}}</td>
</tr>
#endforeach
<tr>
<td>Total</td>
<td>{{ \App\Models\Emphaya::sum('available_for_use_month') }}
</tr>
</table>
<table class=" ">
<tr>
<th class="py-3 bg-cyan-400">Total</th>
<td class="py-3 bg-cyan-400">available_for_use_month</td>
</tr>
#foreach ($treasuries as $treasury)
<tr>
<td></td>
<td>{{ $treasury->available_for_use_month}}</td>
</tr>
#endforeach
<tr>
<td>Total</td>
<td>{{ \App\Models\Treasury::sum('available_for_use_month') }}
</tr>
</table>
in this case what i want to do is connect the total of treasury to emphyas
<tr>
<td>Total</td>
<td>{{ \App\Models\Emphaya::sum('available_for_use_month') + \App\Models\Treasury::sum('available_for_use_month') }}
</tr>
<tbody>
<tr th:each="bean , beanStat : ${list}">
<td class="text-center" th:text="${beanStat.size-beanStat.count+1}+(${paging.cri.page}-1)*${cri.perPageNum}" >1</td>
<!-- paging.totalcount -->
<!-- <tr th:each="bean , beanStat : ${list}">
<td class="text-center" th:text="${beanStat.size-beanStat.count+1}+(${paging.cri.page}-1)*${cri.perPageNum}" >1</td>
First of all, I'm sorry for not speaking English well.
I want to number the articles, but I want to number them in reverse order.
I think you can do it like below in jsp
In thymeleaf, I wonder how to do this in reverse order (is there an index?)
Total number of records - ( (Current page number - 1) * I think it is the number of records displayed per page.
${(totalCount - status.index) - ( (currentPage - 1) * displayNum ) }
I implemented it like this, but I wonder if there is such a part as status.index in the thymeleaf.
enter image description here
There is an equivalent to your JSP approach in Thymeleaf - a set of iteration tracking attributes you can use when processing a list.
Assuming you have a Java list like this:
List<String> list = new ArrayList<>();
list.add("One");
list.add("Two");
list.add("Three");
Then you can use the following Thymeleaf:
<table>
<thead>
<tr>
<th>Number</th>
<th>Item</th>
</tr>
</thead>
<tbody>
<tr th:each="item, iterStat : ${list}">
<td th:text="${iterStat.size - iterStat.count +1}"></td>
<td th:text="${item}"></td>
</tr>
</tbody>
</table>
This generates:
<table>
<thead>
<tr>
<th>Number</th>
<th>Item</th>
</tr>
</thead>
<tbody>
<tr>
<td>3</td>
<td>One</td>
</tr>
<tr>
<td>2</td>
<td>Two</td>
</tr>
<tr>
<td>1</td>
<td>Three</td>
</tr>
</tbody>
</table>
Update
If you want to add paging into the calculation, then your Java code needs to know:
current page number
maximum number of rows per page (there may be fewer in the final page)
total number of records (all pages)
Let's assume I pass those 3 values from Java to my Thymeleaf model with some new data:
List<String> list = new ArrayList<>();
list.add("Alfa");
list.add("Bravo");
list.add("Charlie");
list.add("Delta");
list.add("Echo");
int currentPage = 1;
int rowsPerPage = 5;
int totalRecords = 9;
Now, the Thymeleaf changes to this:
<table>
<thead>
<tr>
<th>Number</th>
<th>Item</th>
</tr>
</thead>
<tbody>
<tr th:each="item, iterStat : ${list}">
<td th:text="${totalRecords - (currentPage * rowsPerPage)
+ rowsPerPage - iterStat.count +1}"></td>
<td th:text="${item}"></td>
</tr>
</tbody>
</table>
For page 1 this generates:
<tbody>
<tr>
<td>9</td>
<td>Alfa</td>
</tr>
<tr>
<td>8</td>
<td>Bravo</td>
</tr>
<tr>
<td>7</td>
<td>Charlie</td>
</tr>
<tr>
<td>6</td>
<td>Delta</td>
</tr>
<tr>
<td>5</td>
<td>Echo</td>
</tr>
</tbody>
For page 2 (the final 4 items), we have:
List<String> list = new ArrayList<>();
list.add("Foxtrot");
list.add("Golf");
list.add("Hotel");
list.add("India");
int currentPage = 2;
int rowsPerPage = 5;
int totalRecords = 9;
Note: Only the current page number has changed. The rows per page and the total records: those values remain the same across all pages.
And the same Thymeleaf template generates this:
<tbody>
<tr>
<td>4</td>
<td>Foxtrot</td>
</tr>
<tr>
<td>3</td>
<td>Golf</td>
</tr>
<tr>
<td>2</td>
<td>Hotel</td>
</tr>
<tr>
<td>1</td>
<td>India</td>
</tr>
</tbody>
I want to sum up the data in my table. Please tell, How can I do it. I have tried the below method but it is not working.
This is my controller code
public function CreateRentCertificateReport(Request $request)
{
$data['reports'] = Report::distric()->status(1)->get();
return view('adc.reports.start-create-report', $data);
}
This is my view code
<tbody>
#foreach($reports as $data)
<tr>
<td>{{$data->upazila->upazila_name}}</td>
<td class="numeric_bangla">{{$data->column_one}}</td>
</tr>
#endforeach
<tr>
<td>Total</td>
#foreach($reports as $data)
<td>{{$data['column_one']->countBy()}}</td>
#endforeach
</tr>
</tbody>
You can use pluck() and sum() :
<tbody>
#foreach($reports as $data)
<tr>
<td>{{ $data->upazila->upazila_name }}</td>
<td class="numeric_bangla">{{ $data->column_one }}</td>
</tr>
#endforeach
<tr>
<td>Total</td>
<td>{{ $reports->pluck('column_one')->sum() }}</td>
</tr>
</tbody>
Maybe by doing something like this:
#php
$total = 0;
foreach($reports as $report) {
$total += $report['column_one']
}
#endphp
<tbody>
#foreach($reports as $report)
<tr>
<td>{{$report->upazila->upazila_name}}</td>
<td class="numeric_bangla">{{$report->column_one}}</td>
</tr>
#endforeach
<tr>
<td>Total</td>
<td>{{$total}}</td>
</tr>
</tbody>
Instead of calling ->get() on an Eloquent query, you can call ->sum(“column_name”) to get a number which is the sum of that column’s values
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Tmp</th>
</tr>
</thead>
<tbody>
#foreach($data as $row)
<tr>
<td>{{$row->temp}}</td>
</tr>
#endforeach
</tbody>
</table>
Above Example, if the temp value greater than 40, what is the easiest and best way give a specific color for Temp
#if($row->temp>'40')
<td bgcolor="#dd2c00">{{$row->temp}}</td>
#endif
This worked for me.
I have a Function to showing a single data . but if this data empty its having error. I using first because just want to
Trying to get property of non-object
its my Controller
public function PDF_profile(Request $request,$id){
$users = User::findOrFail($id);
$pns = Data_pns::where('user_id',$id)->first();
$pendidikan = Data_riwayat_pendidikan::where('user_id',$id)->latest()->first();;
$r_kgb = DB::table('data_riwayat_kgb')->latest()->first();
$pdf = PDF::loadView('admin.pdf_profile',
['users' => $users,
'pendidikan'=>$pendidikan,
'r_kgb'=>$r_kgb,
'pns' => $pns,
]);
// dd($pns);
return $pdf->stream('Profile.pdf')->header('Content-Type','application/pdf');
}
my View
<table align="left" border="" >
<thead>
<tr>
<th> <b>Riwayat Pendidikan </b></th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td> <b>Tingkat Pendidikan </b></td>
<td>:
{{ $pendidikan->jenjang}}
</td>
</tr>
<tr>
<td> <b> Tempat Pendidikan</b></td>
<td>:{{$pendidikan->nama_tempat}}</td>
</tr>
<tr>
<td><b>Konsentrasi/Jurusan</b></td>
<td>:{{ $pendidikan->jurusan}}</td>
</tr>
<tr>
<td><b>Tahun Lulus</b></td>
<td>:{{$pendidikan->lulus_tahun}}</td>
</tr>
</tbody>
</table>
its just part on my view .
and this line is mentioned error .
<td>:
<?php echo e($pendidikan->jenjang); ?>
</td>
but if this data is inputed or not empty its not having error . how to solved it ?
You can first check if the object is not null as first() returns null hence the error, so do this instead:
<td>:
#if($pendidikan)
{{$pendidikan->nama_tempat}}
#endif
</td>
You can also replace this: ->latest()->first() with just calling last().
You can use the optional() helper method
{{ optional($pendidikan)->nama_tempat}}
or short form #
{{ #$pendidikan->nama_tempat}}