I have been searching Google for a good amount of time and I don't know if it is the way I am asking the question or what but I can not find anything even closely relevant to this. I have a PDO SQL statement :
try
{
$query = $dbh->prepare("SELECT id, anum, first, last, signintime, counselorname,
finishtime, TIMEDIFF(finishtime, signintime) AS Total_wait FROM inoffice;");
$query->execute();
$result = $query->fetchall();
}
catch (PDOException $e) {
error_log($e->getMessage());
die($e->getMessage());
}
and then I have a table structured like so :
<table id='datatables' class='display'>
<thead>
<tr>
<th>Session Id</th>
<th>A Number</th>
<th>First Name</th>
<th>Last Name</th>
<th>Signin Time</th>
<th>Counselor Name</th>
<th>Finish Time</th>
<th>Total Wait Time</th>
</tr>
</thead>
<tbody>
<?php
foreach($result as $row) {
?>
<tr>
<td><?=$row['id'] ?></td>
<td><?=$row['anum'] ?></td>
<td><?=$row['first'] ?></td>
<td><?=$row['last'] ?></td>
<td><?=$row['signintime'] ?></td>
<td><?=$row['counselorname'] ?></td>
<td><?=$row['finishtime'] ?></td>
<td><?= ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
My question is how on earth do I get the final part of my PDO select in that final td tag of my table? the part I am confused on is :
TIMEDIFF(finishtime, signintime) AS Total_wait
I want that to be able to be included in the last td of the table. Any help / trick / work around be awesome.
You must treat TIMEDIFF(finishtime, signintime) AS Total_wait
as its own row from the database.
resulting in this :
<td><?=$row['Total_wait'] ?></td>
now the table looks and works great :
Related
Good Day, I want to display the value in my DB in one row. But what is happening now, it is being displayed horizontally
Image Sample
<table class="table table-bordered">
<thead style="font-size:20px;text-align:center;"class="thead-dark">
<tr>
<th class="tblHeader">MODEL</th>
<th class="tblHeader">PERIOD 1</th>
<th class="tblHeader">PERIOD 2</th>
<th class="tblHeader">PERIOD 3</th>
<th class="tblHeader">PERIOD 4</th>
<th class="tblHeader">PERIOD 5</th>
<th class="tblHeader">PERIOD 6</th>
<th class="tblHeader">PERIOD 7</th>
<th class="tblHeader">PERIOD 8</th>
<th class="tblHeader">PERIOD 9</th>
<th class="tblHeader">PERIOD 10</th>
<th class="tblHeader">PERIOD 11</th>
<th class="tblHeader">PERIOD 12</th>
</tr>
</thead>
<?php
$connect = mysqli_connect("localhost", "root", "", "hh_bpm");
$query = "SELECT *
FROM bpm_periods_instance
WHERE Category_Name=1
";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result))
{ ?>
<tbody style="font-size:20px; text-align:center;">
<tr>
<td><?php echo $row["Text_Value"];?></td>
</tr>
<?php } mysqli_close($connect);?>
</tbody>
</table>
I want to display the string value in line with Period 1 - 12`
I'm still trying to learn.
First of all, move your while loop just before the <tr>, for you want to have just one table body. Then it's better to have the same number of <th>s and <td>s. I see you have 12 <th>s, so make 12 <td>s in each <tr> (leave them empty if you want, but include them)
The tags is a row containing headings for your table. For your to Match your column names you must have equal number of as th in your table.
<tr>
<th>heading1</th>
<th>heading2</th>
<th>heading3</th>
</tr>
You can now iterate this for your rows.
<tr>
<td>data1</td>
<td>data2</td>
<td>data3</td>
</tr>
You are showing just a singe TD for each row.
What you are missing is a row loop:
while($row = mysqli_fetch_array($result))
{ ?>
<tbody style="font-size:20px; text-align:center;">
<tr>
<?php
foreach($row as $k=>$v)
echo "<td>$v</td>";
?>
</tr>
<?php } mysqli_close($connect);?>
</tbody>
</table>
Alternatively, you should output TDs for all columns manually:
<tr>
<td><?php echo $row["Column1"];?></td>
<td><?php echo $row["Column2"];?></td>
<td><?php echo $row["Column3"];?></td>
<td><?php echo $row["Column4"];?></td>
<td><?php echo $row["Column5"];?></td>
<td><?php echo $row["Column6"];?></td>
...
</tr>
You need a crosstab query to achieve the results that you want. Since there is very little information I'll just give and outline
SELECT info,
sum( if(MONTH(dt)=1,1,0) as Period_1,
sum( if(MONTH(dt)=2,1,0) as Period_2,
//--- repeat for all 12 months
Your query will return 13 columns (info and Period_1 to Period_12). You'll need to adjust the html to cater for this output.
I want to list all users data from the database in PHP. there will be a user is logged-in at a single time. I want un-fetch or hide a single row who has active(logged-in). so, I'm asking you how can I do this...
$query=$conn->pdo->exec("SELECT * FROM usertable");
$row=$query->fetch();
<thead>
<tr>
<th>User Name</th>
<th>Name</th>
<th>Email</th>
<th>Role</th>
<th>Post</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php while($row=$query->fetch()){ ?>
<tr>
<td><?php echo $row['username']; ?>
</td>
<td><?php echo ucfirst($row['name']); ?>
</td>
<td><?php echo $row['username']; ?></td>
<td><?php echo ucfirst($row['user_type']); ?></td>
<td><?php echo $row['id']; ?></td>
</tr>
</tbody>
You can add new field in your usertable like logged with default value 0.when user login you should change 0 to 1.Now you change your sql query **select * from usertable where logged='0';**
<?php
$host="localhost";
$user="root";
$pwd="";
$db="assigment";
$conn=mysqli_connect($host,$user,$pwd,$db);
$query="SELECT * FROM 'tdata'";
$result=mysqli_query($conn,$query);
while ($row=mysqli_fetch($result)) {
?>
<table class="table">
<thead>
<tr>
<th>Full Name</th>
<th>Email</th>
<th>Birthday</th>
<th>Gender</th>
<th>Intrests</th>
<th>Address</th>
<th></th>
</thead>
</tr>
<tbody>
<?php echo "<tr><td>".$row[full_name]."</td></tr>";
}
?>
</tbody>
</table>
Error# Fatal error: Uncaught Error: Call to undefined function mysqli_fetch() in C:\xampp\htdocs\gsoft\assigment\tabledb.php:9 Stack trace: #0 {main} thrown in C:\xampp\htdocs\gsoft\assigment\tabledb.php on line 9
if you want fetch without index use mysqli_fetch_assoc().
Or
if you want to fetch with index use mysqli_fetch_array()
And i think you are doing wrong here because your table tag is inside while loop and your <thead> too.
i think it should be outside of while loop. and you have syntax error in your query and you have one extra <th>
your full code.
<?php
$host="localhost";
$user="root";
$pwd="";
$db="assigment";
$conn=mysqli_connect($host,$user,$pwd,$db);
$query="SELECT * FROM tdata ";
$result=mysqli_query($conn,$query); ?>
<table class="table">
<thead>
<tr>
<th>Full Name</th>
<th>Email</th>
<th>Birthday</th>
<th>Gender</th>
<th>Intrests</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php while ($row=mysqli_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row['full_name']; ?></td>
<td><?php echo $row['your table field name']; ?></td>
<td><?php echo $row['your table field name']; ?></td>
<td><?php echo $row['your table field name']; ?></td>
<td><?php echo $row['your table field name']; ?></td>
<td><?php echo $row['your table field name']; ?></td>
</tr>
<?php }
?>
</tbody>
</table>
use mysqli_fetch_array function instead of mysqli_fetch
Use mysqli_fetch_array instead of mysqli_fetch
Also your table is inside the loop
The table I am looking to pull from my database is com/bzkItsK. It currently pulls the first row in the database but I am unsure about how to set up a script that will pull all the rows (currently 4) by their id to the webpage.
Here is the html as I have set it:
<table class="table table-striped">
<thead>
<tr>
<th>user id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Department</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row"><?php echo $rows[$user_id];?></th>
<td><?php echo $rows[$first_name];?></td>
<td><?php echo $rows[$last_name];?></td>
<td><?php echo $rows[$dept];?></td>
</tr>
</tbody>
</table>
mysql_query is as such.
mysql_connect("localhost","?","?");
mysql_select_db("?");
$sql = mysql_query("SELECT * FROM users ORDER BY id ASC");
$id = 'id';
$user_id = 'user_id';
$first_name = 'first_name';
$last_name = 'last_name';
$dept = 'dept';
$rows = mysql_fetch_assoc($sql);
?>
I am trying to pull all 4 rows by id to be auto generated by a single table script.
you must iterate over your results.
$rows = mysql_fetch_assoc($sql);
will only fetch the first entry.
You have do something like this:
<?php while($rows = mysql_fetch_assoc($sql)): ?>
<tr>
<th scope="row"><?php echo $rows[$user_id];?></th>
<td><?php echo $rows[$first_name];?></td>
<td><?php echo $rows[$last_name];?></td>
<td><?php echo $rows[$dept];?></td>
</tr>
<?php endwhile; ?>
EDIT:
And please do not use mysql, use mysqli instead.
I'm trying to use this chart generator from http://htmldrive.net/items/show/792/Rare-Accessible-charts-using-jQuery-and-HTML5.html
Here's the code which loads data from mysql database:
The query works, but I guess my interpretation of the example provided in the site was wrong.
I get an output if I do it this way(predefined data):
<tr>
<th scope="row">Profit</th>
<td>5</td>
<td>5</td>
</tr>
But when I do it this way I get a blank output:
?>
<table>
<caption> Reports</caption>
<thead>
<tr>
<td></td>
<?php while($row=mysql_fetch_assoc($query)){ ?>
<th scope="col"><?php echo $row['Cust_Name']; ?></th>
<?php } ?>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Subtotal</th>
<?php while($row=mysql_fetch_assoc($query)){ ?>
<td><?php echo $row['TOTAL_PUR']; ?></td>
<?php } ?>
</tr>
<tr>
<th scope="row">Profit</th>
<?php while($row=mysql_fetch_assoc($query)){ ?>
<td><?php echo $row['TOTALPROFIT']; ?></td>
<?php } ?>
</tr>
</tbody>
</table>
Here's what I'm getting:
After the first iteration through the rows, when you display the customer names, the fetch data pointer is at the end of the dataset... you're trying to fetch the set again without resetting the pointer.
Try issuing
mysql_data_seek($query, 0);
before the while loops to display total and profit