Good day all,
I need help in project table
I have a table called projects and i want to count the projects based on country_id column using php in a foreach... ist possible?
however i am using cakephp the old version 1.2 ...
I tried this code:
<table>
<tr>
<th>Country </th>
<th>Country Name</th>
<th>Number of Place</th>
</tr>
<?php
$count_country =0;
$country_count_each=0;
foreach ($projects as $project){
$country_count_each = $project['Project']['country_id'];
if($project['Project']['country_id']==$country_count_each){
$count_country+=$country_count_each;
?>
<tr>
<td style="width: 30%"><?php echo $project['Project']['country_id']; ?></td>
<td style="width: 30%"><?php echo 'Country Name'.$tt; ?></td>
<td style="width: 30%"><?php echo $count_country; ?></td>
</tr>
<?php
}
}
?>
</table>
You need to loop over the entire array and get all the counts first. You can put them into an associative array:
$country_counts = [];
foreach ($projects as $project) {
$country = $project['Project']['country_id'];
if (isset($country_counts[$country])) {
$country_counts[$country]++;
} else {
$country_counts[$country] = 1;
}
}
?>
<table>
<tr>
<th>Country </th>
<th>Country Name</th>
<th>Number of Place</th>
</tr>
<?php
foreach ($projects as $project) {
?>
<tr>
<td style="width: 30%"><?php echo $project['Project']['country_id']; ?></td>
<td style="width: 30%"><?php echo 'Country Name'.$tt; ?></td>
<td style="width: 30%"><?php echo $country_counts[$project['Project']['country_id']]; ?></td>
</tr>
<?php
}
?>
</table>
Related
I need help with how to remove the duplication within list of countries based on country_id.
I have a table which includes many projects for a country and counting the projects number which is working fine but need to print without duplication, only one country_id with the count of its project.
The code has been added - if anyone can help it is appreciated
<table>
<tr>
<th>Country ID</th>
<th>Country Name</th>
<th>Number of Place</th>
</tr>
<?php
$country_counts = [];
foreach ($projects as $project) {
$country_id = $project['Project']['country_id'];
if (isset($country_counts[$country_id])) {
$country_counts[$country_id]++;
?>
<tr>
<td style="width: 30%"><?php echo $project['Project']['country_id']; ?></td>
<td style="width: 30%"><?php echo 'Country Name'; ?></td>
<td style="width: 30%"><?php echo $country_counts[$project['Project']['country_id']]; ?></td>
</tr>
<?php
} else {
$country_counts[$country_id] = 1;
}
}
?>
</table>
the result which i got is
shows the result
Do something like this
$checkout_array = array_unique($checkout_array, SORT_REGULAR);
I did this for my own you can do it for your array
I am unable to test this code but the idea of what I suggested as the second options goes as follows:
<table>
<tr>
<th>Country ID</th>
<th>Country Name</th>
<th>Number of Place</th>
</tr>
<?php
$country_counts=[];
$ids=array();// Store unique country_id values here
foreach( $projects as $project ) {
$country_id = $project['Project']['country_id'];
# Check if the country_id is NOT in the array and display if OK.
if( isset( $country_counts[ $country_id ] ) && !in_array( $country_id, $ids ) ) {
$country_counts[$country_id]++;
?>
<tr>
<td style="width: 30%"><?php echo $project['Project']['country_id']; ?></td>
<td style="width: 30%"><?php echo 'Country Name'; ?></td>
<td style="width: 30%"><?php echo $country_counts[$project['Project']['country_id']]; ?></td>
</tr>
<?php
// add the country_ID to the array so that
// on the next iteration of the loop the $ids
// array will be checked again and no output if
// it is already in the array.
$ids[]=$country_id;
} else {
$country_counts[$country_id] = 1;
}
}
?>
</table>
I am having a nested table with a while loop, I want to add one more nested table in the same row:
Now I want to add one more nested table as each cd contains more than one data like below:
My code is as follows
<?php
if(isset($_POST['viewcd'])){
$queryw = "select * from lib_cd where id=".$_POST['idd'];
$resultw = $mysqli->query($queryw);
?>
<div>
<table border="1">
<thead>
<tr ><th >Select</th>
<th>Well_Number</th>
<th>Well_Name</th>
<th>CD No:</th>
<th >Logs</th>
</tr>
</thead>
<?php
while($rowcd = $resultw->fetch_assoc()){
?>
<tr>
<td><?php echo $rowcd['id'] ?> </td>
<td><?php echo $rowcd['well_no'] ?></td>
<td><?php echo $rowcd['well_name'] ?></td>
<td>
<table border="1" width="100%">
<?php
$querycd = "select * from cd where pidd=".$rowcd['id'];
$resultcd = $mysqli->query($querycd);
while($rowcd = $resultcd->fetch_assoc()){
?>
<tr>
<td ><?php echo $rowcd['cd_no'] ?></td>
/* I want to add one more nested table here*/
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
}
?>
</table>
</div>
I tried some thing like this,after my second while loop
while($rowcd = $resultcd->fetch_assoc()){
?>
<tr>
<td ><?php echo $rowcd['cd_no'] ?></td>
<td>
<table>
<?php
$queryl = "select * from lib_cd_logs where pid=".$rowcd['cd_no'];
$resultl = $mysqli->query($queryl);
while($rowl = $resultl->fetch_assoc()){
?>
<tr>
<td><?php echo $rowl['logs'] ?></td>
</tr>
<?php
}
?>
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
}
?>
</table>
</div>
but the result was messed up. I am confused, where I want to end my while loop, I think.
Finally i got as i wish, and i am sharing the code as below
<?php
if(isset($_POST['viewcd'])){
$queryw = "select * from lib_cd where id=".$_POST['idd'];
$resultw = $mysqli->query($queryw);
?>
<div class="container">
<table border="1" align="center" border-collapse="collapse">
<thead>
<tr >
<th >Select</th>
<th>Well_Number</th>
<th>Well_Name</th>
<th width="100">CD No:</th>
<th width="150">Logs</th>
<th width="100">Bottom Depth</th>
<th width="100">Top Depth</th>
<th width="100">Date of Log</th>
</tr>
</thead>
<?php
while($rowcd = $resultw->fetch_assoc()){
?>
<tr>
<td><?php echo $rowcd['id'] ?> </td>
<td align="center"><?php echo $rowcd['well_no'] ?></td>
<td align="center"><?php echo $rowcd['well_name'] ?></td>
<td colspan="5">
<table rules="all">
<tr>
<?php
$querycd = "select * from cd where pidd=".$rowcd['id'];
$resultcd = $mysqli->query($querycd);
while($rowcd = $resultcd->fetch_assoc()){
?>
<td width="100" align="center"><?php echo $rowcd['cd_no'] ?></td>
<td colspan="4">
<table rules="all">
<tr>
<?php
$queryl = "select * from lib_cd_logs where pid=".$rowcd['cd_no'];
$resultl = $mysqli->query($queryl);
while($rowl = $resultl->fetch_assoc()){
?>
<td width="155"><?php echo $rowl['logs'] ?></td>
<td width="105" align="center"><?php echo $rowl['bottom'] ?></td>
<td width="100" align="center"><?php echo $rowl['top'] ?></td>
<td width="100" align="right"><?php echo $rowl['date'] ?></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
?>
</table>
</td>
<?php
}
}
?>
</tr>
</table>
I hope this is what you meant as per your data table shown above
<div>
<table border="1">
<thead>
<tr ><th >Select</th>
<th>Well_Number</th>
<th>Well_Name</th>
<th>CD No:</th>
<th >Logs</th>
</tr>
</thead>
<tr>
<td>id</td>
<td>well</td>
<td>name</td>
<td>
<table border="1" width="100%">
<tr>
<td>1</td>
</tr>
<tr>
<td>2</td>
</tr>
</table>
</td>
<td>
<table border="1" width="100%">
<tr>
<td>Log1</td>
</tr>
<tr>
<td>Log2</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
I have a problem i want to put the $message inside the table that is a equivalent as a "Search not found".
Here is my picture outside the table $message = Search not found
View:
<div class="z table-responsive" >
<table class=" table table-hover" >
<thead >
<tr >
<th>ID Number</th>
<th>First name</th>
<th>Middle name</th>
<th>Last name</th>
<th>Sex</th>
</tr>
</thead>
<?php if ( isset($message) ){
echo $message;
} else{ foreach($results as $row){
?>
<tbody>
<tr>
<td><?php echo $row-> Idnumber ?></td>
<td class="text-capitalize "><?php echo $row -> Firstname ?></td>
<td class="text-capitalize"><?php echo $row->Middlename ?></td>
<td class="text-capitalize"><?php echo $row-> Lastname ?></td>
<td class="text-capitalize"><?php echo $row-> Sex?></td>
<td>
Option
</td>
</tr>
</tbody>
<?php }} ?>
</tbody>
</table>
</div>
You are using wrong end tags.
This will print Search not found inside the table with colspan 5 and center aligned
Note: Most of time we check where foreach value is empty ($results). But in your case you are checking some thing else ($message).
Changes
<tr> <!-- Changed -->
<td colspan="5" align="center"><?php echo $message; ?> </td>
</tr>
if ( isset($message) ){ is changed to if (!empty($message)){
Final code
<div class="z table-responsive" >
<table class=" table table-hover" >
<thead >
<tr >
<th>ID Number</th>
<th>First name</th>
<th>Middle name</th>
<th>Last name</th>
<th>Sex</th>
</tr>
</thead>
<tbody>
<?php
if (!empty($message)) # improved
{
?>
<tr> # Changed
<td colspan="5" align="center"><?php echo $message; ?> </td>
</tr>
<?php
}
else
{
foreach($results as $row)
{
?>
<tr>
<td><?php echo $row-> Idnumber ?></td>
<td class="text-capitalize "><?php echo $row -> Firstname ?></td>
<td class="text-capitalize"><?php echo $row->Middlename ?></td>
<td class="text-capitalize"><?php echo $row-> Lastname ?></td>
<td class="text-capitalize"><?php echo $row-> Sex?></td>
<td>
<a href="<?php echo site_url('viewstudentinalpha/viewspecific/'.$row->Id) ?>" class="btn btn-info " style="font-size: 18px;" type="submit" name="submit" role="button">
Option
</a>
</td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</div>
Edit 01
In Controller
function search_keyword()
{
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
$keyword = $this->input->post('keyword');
$data['results'] = $this->model_adminlogin->search($keyword);
$this->load->view('result_view',$data);
}
In View
Alter your if like this.
Copy all codes i have added in above for your view. and only change this few lines.
<?php
if (!empty($results)) # Change
{
?>
<tr> # Change
<td colspan="5" align="center"> Search not found </td> # Change
</tr>
<?php
}
else
{
This isn't an issue with CodeIgniter so much as your code. What you are trying to do with the code is to put the message between the <thead> and <tbody>, and not in any table tags.
This is the same as trying to put it between rows in a table, and therefore gets interpreted as not being part of the table, and drawn outside of the table as a result.
If you put the message into the <tbody>, it will go in the right place, so you can do:
<?php if ( isset($message) ){
echo "<tbody><tr><td colspan='5'>" . $message . "</td></tr></tbody>";
}....
try this
<tbody>
<?php
if ( isset($result )
// or if (!isset($message)) //depend on your code
{
foreach($results as $row){
?>
<tr>
<td><?php echo $row-> Idnumber ?></td>
<td class="text-capitalize "><?php echo $row -> Firstname ?></td>
<td class="text-capitalize"><?php echo $row->Middlename ?></td>
<td class="text-capitalize"><?php echo $row-> Lastname ?></td>
<td class="text-capitalize"><?php echo $row-> Sex?></td>
<td>
Option
</td>
</tr>
<?php }}
else
//or elseif(isset($message)) //depend on your code
{
echo $message; ?>
}
</tbody>
</table>
</div>
I'm trying to do this in while loop PHP, number of assignments are unlimited so rowspan should also adjust itself with the number of rows, is there any proper way to do it with minimum numbers of line?
<table border="1" cellpadding="5" cellspacing="0">
<thead>
<tr>
<th>Assignment No</th>
<th>Student Name</th>
<th>Assignment Marks</th>
<th>Overall Result</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">1</td>
<td align="center">S1</td>
<td align="center">5</td>
<td rowspan="3" align="center">B Grade</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">S1</td>
<td align="center">8</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">S1</td>
<td align="center">7</td>
</tr>
</tbody>
</table>
Try This:
<? foreach($dataarray as $data)
{?>
<tbody>
<tr>
<td align="center"><? echo $data[0]; ?></td>
<td align="center"><? echo $data[1]; ?></td>
<td align="center"><? echo $data[2]; ?></td>
</tr>
<? if($data[3]!=Null) { ?>
<td rowspan="3" align="center"><? echo $data[3]; ?></td>
<? } ?>
<? } ?>
and put if condition for last column
Assuming that data comes in a linear fashion (ie a plain one dimensional array), and that the grade is not in the array, I'd do something like this:
<?
$sizeofArray = count($data);
$rowspan = floor($sizeofArray/3);
for($arrCnt = 0; $arrCnt < $sizeofArray; $arrCnt +=3)
{?>
<tbody>
<tr>
<td align="center"><?=$data[$arrCnt]; ?></td>
<td align="center"><?=(($arrCnt+1 < $sizeofArray)?$data[$arrCnt+1]:" "); ?></td>
<td align="center"><?=(($arrCnt+2 < $sizeofArray)?$data[$arrCnt+2]:" "); ?></td>
</tr>
<? if($data[3]!=Null) { ?>
<td rowspan="<?=$rowspan?>" align="center"><?=$grade; ?></td>
<? } ?>
<? } ?>
EDIT: added "padding" code, to avoid index out of bounds error
Based on your case, rowspan is different from one student to another. What you would want to do, is group data by student. The number of assignments per student is what will give you rowspan value.
While ... {
$data['student'][] = ['name' => $row -> name]; // all student inf
$data['student][]['assignments'][] = ['id' => $row -> assignement_id];//student assigments
}
Hope this help or put in the right direction to resolve your problem.
use foreach to loop
foreach ($data as $student){
$rowspan = count($student['assignments'];
foreach ($student['assigments'] as $assignment ){
// html table
}
}
I have the following php/html code :
<div id="demo">
<table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
<thead>
<tr>
<th>Medicine Name</th>
<th>Batch Number</th>
<th>Total Quantity</th>
<th>Expiry Date(s)</th>
<th>Selling Price</th>
<th> Total Price</th>
<th>Issue</th>
</tr>
</thead>
<tbody>
<?php foreach ($prescription as $prescribed): ?>
<tr class="odd gradeX">
<td><?php echo $prescribed['commodity_name']; ?></td>
<td ><?php echo $prescribed['batch_no']; ?></td>
<td><?php echo $prescribed['total_quantity']; ?></td>
<td><?php echo $prescribed['expiry_date']; ?></td>
<td ><?php echo $prescribed['selling_price']; ?></td>
<td><?php
$total_quantity = $prescribed['total_quantity'];
$selling_price = $prescribed['selling_price'];
$total_quantity_float = floatval($total_quantity);
$selling_price_float = floatval($selling_price);
$total_price = $total_quantity_float*$selling_price_float;
echo $total_price;
?></td>
<td>
<a class="issue" href="#types" id="issue">Issue</a>
<input type="hidden" name="batch_no" id="batch_no" value="<?php echo $prescribed['batch_no']; ?>"/>
</td>
<!-- <td> <a id="issue1" class="issue1" href="#types">Issue</a> </td>-->
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
I would like to get the total sum of the total_price variable which is displayed(total_price variable) as a row on the table. This should show the total price for all the commodities, how can I do this best?
try this
<?php
$total_price_sum = 0;
foreach ($prescription as $prescribed){
..
..
$total_price_sum = $total_price_sum + $total_price;
}
echo $total_price_sum;
?>
You can do this by adding to a variable each time you loop. This can be done as follows:
<?php
$grand_total = 0;
foreach ($prescription as $prescribed){
?>
..... all the HTML bits .....
<?
$grand_total = $grand_total + $total_price;
}
echo $grand_total;
?>
Hope that helps.
Regards,
Ralfe