MySQL not fetching all results - php

Trying to fetch all results in PHP from MySQL database, but it is leaving the first query.
My MySQL table is in the below image: .
My Code:
<?php
$irn = "33857";
$stmt = $user_home->runQuery('SELECT * FROM invoice WHERE Inv = :inv ORDER BY Sr ASC ');
$stmt->bindParam(':inv',$irn);
$stmt->execute();
$rowc = $stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount() > 0)
{
?>
<table id="chiru_inv" class="table table-striped table-hover table-bordered table-responsive">
<tr>
<td colspan="4" align="center">
<h1>Company</br><span style="font-size: 75%;">Number</span></h1>
</td>
</tr>
<tr>
<td colspan="2">
<h3><span style="float: left;"><?php echo $rowc['Customer']; ?> (<?php echo $rowc['Inv']; ?>)</span></h3>
</td>
<td colspan="2">
<h3><span style="float: right;"><?php echo $rowc['Date']; ?></span></h3>
</td>
</tr>
<tr>
<th>Sr.</th>
<th>Item</th>
<th>Qty</th>
<th>Amount</th>
</tr>
<?php
$i = 0;
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row);
$i++;
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $Item; ?></td>
<td>5</td>
<td>200</td>
</tr>
<?php
}
?>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan="2"><strong><?php echo getIndianCurrency(225); ?>Only</strong></td>
<td><strong>Total:</strong></td>
<td><strong>225</strong></td>
</tr>
</table>
<?php
}
?>
The result when I execute the above code is in the below image:
There are three queries with invoice number 33857, but only two are displayed (leaving the first one)!
I need all to be displayed as per invoice number.
Please help me from sorting out the error/code I made or I left.

The problem in your code is with the line:
$rowc = $stmt->fetch(PDO::FETCH_ASSOC);
in the beginning of your code you're fetching the first row.
You have to update your while loop, like this
<?php
$i = 0;
while($rowc)
{
extract($rowc);
$i++;
?>
<!-- Your HTML/PHP code for the table -->
<?php
$rowc=$stmt->fetch(PDO::FETCH_ASSOC);
}
?>
So, the idea is to fetch the row at the end of the while loop, in order to not lose your first row.

Related

how to display comma separated values from multiple columns in php inside html table

i have an sql table with three columns which have comma separated values, i am trying to print it inside an html table, my code looks like below:
<table class="table custom-table m-0">
<thead>
<tr>
<th>Description</th>
<th>Make</th>
<th>UOM</th>
</tr>
</thead>
<tbody>
<?php
$one=explode(',', $row['description']);
$two=explode(',', $row['make']);
$three=explode(',', $row['uom']);
foreach($one as $ones) {
?>
<tr>
<td>
<?php echo $ones?>
</td>
<td></td>
<td></td>
</tr>
<?php }?>
</tbody>
</table>
here am only able to get the values of first column, can anyone please tell me how to get values from all the three columns, thanks in advance
Use a counter - assuming exact same number of entries per row
http://sandbox.onlinephpfunctions.com/code/555be47daf3bc3e99d496585f702bfc9dfae4e4e
<?
$one=explode(',', $row['description']);
$two=explode(',', $row['make']);
$three=explode(',', $row['uom']);
$i=0;
?>
<table class="table custom-table m-0">
<thead>
<tr>
<th>Description</th>
<th>Make</th>
<th>UOM</th>
</tr>
</thead>
<tbody>
<?php
foreach($one as $ones) {
?>
<tr>
<td><?php echo $ones; ?></td>
<td><?php echo $two[$i]?></td>
<td><?php echo $three[$i]?></td>
</tr>
<?php $i++;}?>
</tbody>
</table>

Send HTML formated mail using PHPMailer

I need to send emails with the content of a PHP page, but I am having trouble, as far as I could do is creating static format in HTML but if tomorrow the data has more rows the email will be incomplete,
I will share part of my mail.php
//The query
$wipAgingQuery = $dbconnection->query("
SELECT
CAST(datein AS DATE) AS [Date_In],
COUNT(lab) AS [Count_of_Jobs],
ROUND(SUM(COUNT(lab)) OVER (ORDER BY datein DESC) * 100.0 / SUM(COUNT(lab)) OVER (),2) AS [Cumulative]
FROM [DailyWIP].[dbo].[WIP_Daily_Load]
WHERE location = 'USA' AND dateload BETWEEN CONCAT(CAST(GETDATE() AS DATE),' 11:30:00.000') AND CONCAT(CAST(GETDATE() AS DATE),' 14:00:00.000')
GROUP BY datein ORDER BY datein DESC;");
$jobInWip = $wipAgingQuery->fetchAll(PDO::FETCH_OBJ);
//Assign Query Result in Variable
foreach ($jobInWip as $jobInWip) {
$wipDateIn = $jobInWip -> Date_In;
$wipCount = $jobInWip -> Count_of_Jobs;
$wipCumulative = $jobInWip -> Lab_Cumulative;
$totalJobCount = $totalJobCount + $wipCount;
}
//HTML inside the Mailer Body
<div class='row'>
<div class='col-12'>
<h1>Current Day WIP: Aging</h1>
<table class='table table-bordered'>
<thead class='thead-dark'>
<tr>
<th>Date In</th>
<th>Count of Jobs</th>
<th>Lab Cumulative</th>
</tr>
</thead>
<tbody>
<tr>
<td>$wipDateIn</td>
<td><p class='text-right'>$wipCount</p></td>
<td><p class='text-right'>$wipCumulative</td>
</tr>
<tr>
<td>Grand Total:</td>
<td><p class='text-right'>$totalJobCount</p></td>
</tr>
</tbody>
</table>
</div>
</div>
This is the original wipdata.php page where I can manage that problem since the table is being created by a foreach depending on the results of the query, but I don't know how to do something similar in the mail.php
<div class="row">
<div class="col-12">
<h1>Current Day WIP: Aging</h1>
<br>
<table class="table table-bordered">
<thead class="thead-dark">
<tr>
<th>Date In</th>
<th>Count of Jobs</th>
<th>Lab Cumulative</th>
</tr>
</thead>
<tbody>
<?php foreach ($jobInWip as $jobInWip) { ?>
<tr>
<td><?php echo $jobInWip->Date_In ?></td>
<td><?php echo '<p class="text-right">'. number_format($jobInWip->Count_of_Jobs) .'</p>' ?></td>
<td><?php echo '<p class="text-right">'. number_format($jobInWip->Lab_Cumulative, 2) . ' %' ?></td>
</tr>
<?php $totalJobCount = $totalJobCount + $jobInWip->Count_of_Jobs;
} ?>
<tr>
<td><?php echo 'Grand Total: ' ?></td>
<td><?php echo '<p class="text-right">'. number_format($totalJobCount).'</p>'; ?></td>
</tr>
</tbody>
</table>
</div>
</div>
Oh I just found a way around this, I am not sure if this is the best way to solve it but it worked.
foreach ($jobInWip as $jobInWip) {
$wipDateIn = $jobInWip -> Date_In;
$wipCount = $jobInWip -> Count_of_Jobs;
$wipCumulative = $jobInWip -> Lab_Cumulative;
$totalJobCount = $totalJobCount + $wipCount;
$phpmailer-> Body .="<tr>
<td>$wipDateIn</td>
<td><p class='text-right'>$wipCount</p></td>
<td><p class='text-right'>$wipCumulative</td>
</tr>";}

Table inside nested 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>

Put the "$message" inside the table in codeigniter

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>

While Loop For Table PHP

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
}
}

Categories