I have a problem,
I want to create a variable wich is save the content of a table,
here is the code:
<?php
include 'koneksi.php';
$query = "SELECT transaksi.id as id, transaksi.deskripsi as deskripsi,
sum(case when shift='1' then transaksi.jumlah end) as shift1,
sum(case when shift='2' then transaksi.jumlah end) as shift2,
sum(transaksi.jumlah) as total
from transaksi where tanggal='2014-11-15' group by id, deskripsi";
$data1 = mysql_query($query) or die('Invalid query: ' .mysql_error());
$no=1;
while($row = mysql_fetch_object($data1)){
$output="
<table>
<tr>
<td>".$no++."</td>
<td>".$row->id."</td>
<td>".$row->deskripsi."</td>
<td>".$row->shift1."</td>
<td>".$row->shift2."</td>
<td>".$row->total."</td>
</tr>
</table>";}
echo $output;
?>
The code above show the result:
3 7100-04000 FINISH GOOD 32100-KVY-7000 20 11 31
The result show only last record of the query,
It should be like this:
1 7100-00000 FINISH GOOD 32100-KZRM-B200 10 9 19
2 7100-03000 FINISH GOOD 32100-KVB-N700 7 8 15
3 7100-04000 FINISH GOOD 32100-KVY-7000 20 11 31
Please help, because I want to use $output as $body in php mailer.
Thank you.
try this:
echo "<table>";
while($row = mysql_fetch_object($data1)){
echo "<tr>
<td>".$no++."</td>
<td>".$row->id."</td>
<td>".$row->deskripsi."</td>
<td>".$row->shift1."</td>
<td>".$row->shift2."</td>
<td>".$row->total."</td>
</tr>";}
echo "</table>";
or if you want to use a variable
$output = "<table>";
while($row = mysql_fetch_object($data1)){
$output .= "<tr>
<td>".$no++."</td>
<td>".$row->id."</td>
<td>".$row->deskripsi."</td>
<td>".$row->shift1."</td>
<td>".$row->shift2."</td>
<td>".$row->total."</td>
</tr>";}
$output .= "</table>";
echo $output;
Try this -
$output = '<table>';
while($row = mysql_fetch_object($data1)){
$output .= "
<tr>
<td>".$no++."</td>
<td>".$row->id."</td>
<td>".$row->deskripsi."</td>
<td>".$row->shift1."</td>
<td>".$row->shift2."</td>
<td>".$row->total."</td>
</tr>";
}
$output .= "</table>";
echo $output;
echo "<table>";
while($row = mysql_fetch_object($data1)){
$output=?>
<tr>
<td><?php echo $no++;?></td>
<td><?php echo $row->id;?></td>
<td><?php echo $row->deskripsi;?></td>
<td><?php echo $row->shift1;?></td>
<td><?php echo $row->shift2;?></td>
<td><?php echo $row->total;?></td>
</tr>
<?php
}
echo $output;
?>
echo "</table>";
Related
I have a table 'tblexam' which contains State,city,candidate.I want to fetch the data from this table using where clause.I am able to fetch the data but i want to add the sum of Candidate at the last row(As Shown In Picture) how can i do that .
$sql="SELECT *
FROM tblexam
WHERE state='UP'";
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result) {
$cnt=$cnt+1; ?>
<tr class="odd gradeX">
<td class="center"><?php echo htmlentities($cnt);?></td>
<td class="left"align="left"><?php echo htmlentities($result->state);?></td>
<td class="center" align="left"><?php echo htmlentities($result->city);?></td>
<td class="center"align="left"><?php echo htmlentities($result->candidate);?></td>
<?php } ?>
<td class="center"align="left"><?php echo htmlentities($result->Total);?></td>
</tbody>
</table>
Add total while iterating through rows and display it outside the loop, Sample code is given below
$sql = "SELECT * FROM tblexam WHERE city='UP'";
$result = $conn->query($sql);
$numRows = $result->num_rows;
if ($numRows> 0) {
$total = 0;
echo '<table border="1" cellpadding="5" cellspacing="0">';
echo '<tr>';
echo '<th>state</th>';
echo '<th>city</th>';
echo '<th>candidate</th>';
echo '</tr>';
while($row = $result->fetch_assoc()) {
$total+=$row['candidate'];
echo '<tr>';
echo '<td>'.$row['city'].'</td>';
echo '<td>'.$row['state'].'</td>';
echo '<td>'.$row['candidate'].'</td>';
echo '</tr>';
}
echo '<tr>';
echo '<td>Total</td>';
echo '<td> </td>';
echo '<td>'.$total.'</td>';
echo '</tr>';
echo '<table>';
}
Before you start looping the data you can create a variable which you set to 0, inside the loop you can add the result's total to this variable, after the loop, the variable will contain the grand total:
$total = 0;
foreach($results as $result) {
$total += (int)$result->Total;
...
}
// $total = 1425
I retrieved a list of data from an SQL database and now I would like to display it in a neat table rather than in a list. I managed to find a way to do this (probably not very elegant, though), but the column headers seem to be offset and I have not idea how to fix this.
I'm completely new to PHP, so any hints on how to solve this will be much appreciated!
echo '<table>';
echo '<tr>';
echo '<th>';
echo '<td>Word</td>';
echo '<td>Frequency</td>';
echo '</th>';
echo '</tr>';
$response = $db->query("SELECT * FROM frequencies WHERE freq BETWEEN 900 AND 910 ORDER BY freq");
while ($row = $response->fetch())
{
echo '<tr>';
echo '<td>'.$row['word'].'</td>';
echo '<td>'.$row['freq'].'</td>';
echo '</tr>';
}
echo '</table>';
$response->closeCursor();
A <th> element is a table header element and should be used instead of <td> (table data) element in your header row - it should never be a wrapper around <td> elements.
echo '<table>';
echo '<tr>';
echo '<th>Word</th>';
echo '<th>Frequency</th>';
echo '</tr>';
I prefer combining php and html
<table >
<thead>
<tr>
<th >Word</th>
<th >Frequency</th>
</tr>
</thead>
<?php
$response = $db->query("SELECT * FROM frequencies WHERE freq
BETWEEN 900 AND 910 ORDER BY freq");
?>
<tbody>
<?php
while ( $row = $response->fetch()) {
?>
<tr>
<td><?php echo $row['word']; ?></td>
<td><?php echo $row['freq']; ?></td>
</tr>
<?php }
$response->closeCursor();
?>
</tbody>
</table>
<table>
<tr>
<th>name</th>
<th>startDate</th>
<th>rating</th>
<th>underlay</th>
<th>edges</th>
<th>grip</th>
<th>depth</th>
<th>length</th>
<th>height</th>
<th>realname</th>
</tr>
<?php
if(isset($_GET['DS'])){
$query='SELECT * FROM KundDetaljer where rspName = :DS';
$stmt = $pdo->prepare($query);
$stmt->bindParam(':DS', $_GET['DS']);
$stmt->execute();
foreach($stmt as $key => $row){
echo '<tr>';
echo "<td>".$row['rspName']."</td>";
echo "<td>".$row['startDate']."</td>";
echo "<td>".$row['rating']."</td>";
echo "<td>".$row['underlay']."</td>";
echo "<td>".$row['edges']."</td>";
echo "<td>".$row['grip']."</td>";
echo "<td>".$row['depth']."</td>";
echo "<td>".$row['length']."</td>";
echo "<td>".$row['height']."</td>";
echo "<td>".$row['realname']."</td>";
echo "</tr>";
}
}
echo "</table>";
?>
Hi, i'm a student in Sweden who is having a problem with making numbers to stars out of rating. The code above is the code that shows the rating from customers. When the comments from customers is made i want it to be showed as stars.
inside foreach (before any echo):
$stars = "";
for($i=0;$i<$row["rating"];$i++){
$stars .= "★";
}
Then instead of
echo "<td>".$row['rating']."</td>";
use
echo "<td>".$stars."</td>";
may I know how to create a HTML table for displaying my queries search results?
here is my code for search
<?php
//This connects the file to your database.
// Change the "database-username & database-password" to the one you've setup already.
mysql_connect ("localhost", "root","") or die (mysql_error());
mysql_select_db ("smpi");
$search = $_POST['search'];
$sql = mysql_query("select * from maklumat_pc where FName like '%$search%'");
while ($row = mysql_fetch_array($sql)){
echo '<br/> Agensi: '.$row['Agensi'];
echo '<br/> Jabatan: '.$row['Jabatan'];
echo '<br/> Work Group: '.$row['Work_Group'];
echo '<br/> Computer Name: '.$row['Computer_Name'];
echo '<br/> Kategori Infra: '.$row['Kategori_Infra'];
echo '<br/> Nama Pengguna: '.$row['Nama_Pengguna'];
echo '<br/> Jawatan: '.$row['Jawatan'];
echo '<br/> Gred Jawatan: '.$row['Gred_Jawatan'];
}
?>
use this for show data as Table Format
<?php
mysql_connect ("localhost", "root","") or die (mysql_error()); //This connects the file to your database. Change the "database-username & database-password" to the one you've setup already.
mysql_select_db ("smpi");
$search = mysql_real_escape_string($_POST['search']);
$sql = mysql_query("SELECT * FROM maklumat_pc WHERE FName LIKE '%$search%'");
?>
<table>
<thead>
<tr>
<th>Agensi</th>
<th>Jabatan</th>
<th>Work Group</th>
<th>Computer Name</th>
<th>Kategori Infra</th>
<th>Nama Pengguna</th>
<th>Jawatan</th>
<th>Gred Jawatan</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($sql))
{
?>
<tr>
<td><?php echo $row['Agensi'];?></td>
<td><?php echo $row['Jabatan'];?></td>
<td><?php echo $row['Work_Group'];?></td>
<td><?php echo $row['Computer_Name'];?></td>
<td><?php echo $row['Kategori_Infra'];?></td>
<td><?php echo $row['Nama_Pengguna'];?></td>
<td><?php echo $row['Jawatan'];?></td>
<td><?php echo $row['Gred_Jawatan'];?></td>
</tr>
<?php
}
?>
</tbody>
</table>
Warning
This extension is deprecated as of PHP 5.5.0, and will be removed in the future
Something like this,
echo '<table>';
echo '<thead><tr><th>Agensi</th><th>Jabatan</th></thead>';
while ($row = mysql_fetch_array($sql)){
echo '<tr>';
echo '<td>'.$row['Agensi'].'</td>';
echo '<td>'.$row['Jabatan'].'</td>';
echo '</tr>';
}
echo '</table>';
Try this code:
echo '<table border="1">';
while ($row = mysql_fetch_array($sql)){
echo '<tr>';
echo '<td> Agensi: '.$row['Agensi'].'</td>';
echo '<td> Jabatan: '.$row['Jabatan'].'</td>';
echo '<td> Work Group: '.$row['Work_Group'].'</td>';
echo '<td> Computer Name: '.$row['Computer_Name'].'</td>';
echo '<td> Kategori Infra: '.$row['Kategori_Infra'].'</td>';
echo '<td> Nama Pengguna: '.$row['Nama_Pengguna'].'</td>';
echo '<td> Jawatan: '.$row['Jawatan'].'</td>';
echo '<td> Gred Jawatan: '.$row['Gred_Jawatan'].'</td>';
echo '</tr>';
}
echo '</table>';
I have a MySQL query with over 50 return results. Now I need to display the results in a table with 3 rows and 3 columns.
Something like this:
<table>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
</table>
I tried it with PHP like this:
$q = "SELECT name, address, content FROM mytable";
$r = mysqli_query($dbc, $q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$name = $row['name'];
$address = $row['address'];
$content = $row['content'];
//Create new output
$output = "<p>$name</p>";
$output .= "<p>$address</p>";
$output .= "<p>$content</p>";
//Add output to array
$mycontent[] = $output;
}
Then I am printing the content array in my table like this:
<tr>
<td><?php echo $mycontent[0]; ?></td>
<td><?php echo $mycontent[1]; ?></td>
<td><?php echo $mycontent[2]; ?></td>
</tr>
<tr>
<td><?php echo $mycontent[3]; ?></td>
<td><?php echo $mycontent[4]; ?></td>
<td><?php echo $mycontent[5]; ?></td>
</tr>
<tr>
<td><?php echo $mycontent[6]; ?></td>
<td><?php echo $mycontent[7]; ?></td>
<td><?php echo $mycontent[8]; ?></td>
</tr>
Using this code, I can only display 9 contents. My problem is that I want to display more content. I'm going to use pagination to display contents; something like 0-9, 10-18, 19-27 etc.
NOTE: I can do the pagination part.
I hope someone will give me the right direction for this.
Thank you.
try something like:
echo "<table>";
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$name = $row['name'];
$address = $row['address'];
$content = $row['content'];
echo "<tr><td>".$name."</td><td>".$address."</td><td>".$content."</td></tr>";
}
echo "</table>";
this example will print in table all the result of the query.
if you want to limit only to some results, so limit the sql query.
for example:
$q = "SELECT name, address, content FROM mytable limit 50";
to get each content,name, address in TD, and 3 of mycontent(content, name, address) in a TR try this:
$c= mysql_query("select COUNT(name) from mytable");
$n=mysql_fetch_array($c);
$maxname= $n[0];
$i=0;
$tr=0;
echo "<table>";
while ($tr < $maxname)
{
echo "<tr>";
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC) and $i<$tr) {
$name = $row['name'];
$address = $row['address'];
$content = $row['content'];
echo "<td>".$name." | ".$address." | ".$content."</td>";
$i++;
}
echo "</tr>";
$tr= $tr+3;
}
echo "</table>";
Try something like this. It places your values in an easy to use associative array. Then you just loop through.
<?php
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$values[] = array(
'name' => $row['name'],
'address' => $row['address'],
'content' => $row['content']
);
}
?>
<table>
<?php
foreach($values as $v){
echo '
<tr>
<td>'.$v['name'].'</td>
<td>'.$v['address'].'</td>
<td>'.$v['content'].'</td>
</tr>
';
}
?>
</table>
You can store in a variable if u r planning to use it in a function or you can just print it. Either way...
$q = "SELECT name, address, content FROM mytable";
$r = mysqli_query($dbc, $q);
$str = "<table>";
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$str .= "<tr>";
$str .= "<td>" . $row['name'] . "</td>";
$str .= "<td>" . $row['address'] . "</td>";
$str .= "<td>" . $row['content'] . "</td>";
$str .= "</tr>";
}
$str .= "</table>"
echo $str;
There you go!
use a for-loop to iterate your $mycontent-array:
EDIT: I was made aware that $mycontent is built differently than I first assumed:
$h = "<table>"; // we are going to build the table in $h
$maxcount = count($mycontent); // get the number of values within the array
for ($i = 0;$i < $maxcount;$i++) { // counting $i up from 0 to $maxcount -1 ...
$h.= "<tr><td>{$mycontent[$i]}</td></tr>"; // build row
}
$h.= "</table>"; // close the table
echo $h; // echo it
---> live demo: http://3v4l.org/g1E1T
YOU CAN REFERENCE THIS CODE
<tbody>
<!-- <tr>
<td>Technology</td>
<td>Professor Abiola</td>
<td>wetech#school.com</td>
<td> Plot 2, Oriola road,</td>
<td>
<button type="button" class="btn btn-primary"> Edit</button>
<button type="button" class="btn btn-success">Update</button>
<button type="button" class="btn btn-danger">Delete</button>
</td>
</tr> -->
<?php
while($rows = mysqli_fetch_array($response)){
$faculty = $rows['faculty'];
$hod = $rows['hod'];
$email = $rows['email'];
// $location = $rows['location'];
echo "<tr>
<td>$faculty</td>
<td>$hod</td>
<td>$email</td>
<td>".$rows["location"]."</td>
<td>
<button type='button' class='btn btn-primary'>Edit</button>
<button type='button' class='btn btn-success'>Update</button>
<button type='button' class='btn btn-danger'>Delete</button>
</td>
</tr>";
}
?>
</tbody>
</table>