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>';
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've got an MSSQL server running with several large tables. I'm trying to place them into an HTML table so that I can display all of the data in a nice CSS modified webpage. I'm using PHP to ferry the information from SQL to my HTML script, but the only way I've found so far to generate such a table is by hardcoding all of the SQL column names into my PHP-SQL query.
As you can see below, this is the setup required for just one such table. Is there any more concise way to get the information from SQL into a formatted HTML table? I've looked around for a number of hours perhaps for some sort of PHP scripted loop that can iterate through all of the SQL columns, but I haven't found anything. I greatly appreciate any input!
<table id="capacitors">
<thead>
<tr>
<td>Part Number</td>
<td>Capacitance</td>
<td>Capacitance Tolerance</td>
<td>Case Package</td>
<td>Case Package SI</td>
<td>Dielectric Char.</td>
<td>Dielectric Mat.</td>
<td>Halogen Free Stat.</td>
<td>Insulation Resistance</td>
<td>Lead Free Stat.</td>
<td>Lifecycle Stat.</td>
<td>Mounting Style</td>
<td>Operating Temp.</td>
<td>Packaging</td>
<td>Pin Count</td>
<td>Reach SVHC Comp.</td>
<td>Rohs Stat.</td>
<td>Size Height</td>
<td>Size Length</td>
<td>Size Thickness</td>
<td>Size Width</td>
<td>Temp. Coefficient</td>
<td>Voltage Rating DC</td>
</tr>
</thead>
<tbody>
<?php
foreach ($db->query($sql) as $rows){
?>
<tr>
<td><?php echo $rows['part_number']?></td>
<td><?php echo $rows['capacitance']?></td>
<td><?php echo $rows['capacitance_tolerance']?></td>
<td><?php echo $rows['case_package']?></td>
<td><?php echo $rows['case_package_si']?></td>
<td><?php echo $rows['dielectric_characteristic']?></td>
<td><?php echo $rows['dielectric_material']?></td>
<td><?php echo $rows['halogen_free_status']?></td>
<td><?php echo $rows['insulation_resistance']?></td>
<td><?php echo $rows['lead_free_status']?></td>
<td><?php echo $rows['lifecycle_status']?></td>
<td><?php echo $rows['mounting_style']?></td>
<td><?php echo $rows['operating_temperature']?></td>
<td><?php echo $rows['packaging']?></td>
<td><?php echo $rows['pin_count']?></td>
<td><?php echo $rows['reach_svhc_compliance']?></td>
<td><?php echo $rows['rohs_status']?></td>
<td><?php echo $rows['size_height']?></td>
<td><?php echo $rows['size_length']?></td>
<td><?php echo $rows['size_thickness']?></td>
<td><?php echo $rows['size_width']?></td>
<td><?php echo $rows['temperature_coefficient']?></td>
<td><?php echo $rows['voltage_rating_dc']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
This is basic example using PHP Driver for SQL Server:
<?php
# Settings
$server = 'server\instance,port';
$database = 'database';
$user = 'user';
$password = 'password';
$tablename = 'tablename';
# Connection
$cinfo = array(
"Database" => $database,
"ReturnDatesAsStrings" => true,
"UID" => $user,
"PWD" => $password
);
$conn = sqlsrv_connect($server, $cinfo);
if($conn === false)
{
echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
exit;
}
# SQL statement
$sql = "SELECT * FROM [".$tablename."]";
$stmt = sqlsrv_prepare($conn, $sql);
if($stmt === false) {
echo "Error (sqlsrv_prepare): ".print_r(sqlsrv_errors(), true);
exit;
}
# Columns names
echo '<table id="'.$tablename.'">';
echo "<thead>";
echo "<tr>";
$metadata = sqlsrv_field_metadata($stmt);
foreach($metadata as $field) {
echo "<td>".$field['Name']."</td>";
}
echo "</tr>";
echo "</thead>";
# Table rows
echo "<tbody>";
if (!sqlsrv_execute($stmt)) {
echo "Error (sqlsrv_execute): ".print_r(sqlsrv_errors(), true);
exit;
}
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
echo "<tr>";
foreach($row as $value) {
echo "<td>".$value."</td>";
};
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
# End
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
?>
Maybe use SHOW COLUMNS or DESCRIBE
$columns = "SHOW COLUMNS FROM <table>";
$output = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)){
echo $row['Field']."<br>";
}
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>";
I've created a PHP program for adding and viewing reminders. The add page works, but I'm having some trouble displaying them properly. How should I code this to only get the actual data? Also, how could I put this into an HTML table? (i.e. column for name, description, and date; rows are the retrieved data)
Thanks
When I open the file in my browser I get this as an output:
Array ( [reminderID] => 14 [reminderName] => Test [reminderDescript] => Test_Descript [reminderDate] => 2012 05 7 )
Code:
<?php include 'header.php'; ?>
<?php include 'database.php'; ?>
<div id="content">
<h1>Reminder List</h1>
<table align ="center">
<thead>
<tr>
<td>Name</td>
<td>Description</td>
<td>Date</td>
</tr>
</thead>
<?php
$query = 'SELECT * FROM reminder_event';
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
print_r($row);
}
?>
</table>
<p><a href='reminder_add.php'>Add Reminder</a></p>
</div>
<?php include 'footer.php'; ?>
print_r() is a diagnostic tool for debugging, not to be used for real output. Instead, output HTML to a table using the array keys fetched from your row.
// Open a table
echo "<table>";
while($row = mysql_fetch_assoc($result)) {
// Output each row
echo "<tr>
<td>{$row['reminderName']}</td>
<td>{$row['reminderDescript']}</td>
<td>{$row['reminderDate']}</td>
</tr>";
}
// Close the table
echo "</table>";
Better still, escape each of the values for HTML output with [htmlspecialchars()] (http://us3.php.net/manual/en/function.htmlspecialchars.php) before output to prevent cross-site scripting attacks and broken HTML if characters like < > & are encountered in the values.
echo "<table>";
while($row = mysql_fetch_assoc($result)) {
// Encode all values for HTML output
$name = htmlspecialchars($row['reminderName']);
$desc = htmlspecialchars($row['reminderDescript']);
$date = htmlspecialchars($row['reminderDate']);
// Then output the encoded values
echo "<tr><td>$name</td><td>$desc</td><td>$date</td></tr>";
}
echo "</table>";
Change:
while($row = mysql_fetch_assoc($result)) {
print_r($row);
}
To:
while($row = mysql_fetch_assoc($result)) {
echo $row['reminderID'];
echo $row['reminderName'];
echo $row['reminderDescript'];
echo $row['reminderDate'];
}
You can echo those values out in whatever HTML you'd like. So, for example, if you want it in a table you would do something like this:
echo "<table><tr>";
while($row = mysql_fetch_assoc($result)) {
echo "<td>" . $row['reminderID'] . "</td>";
echo "<td>" . $row['reminderName'] . "</td>";
echo "<td>" . $row['reminderDescript'] . "</td>";
echo "<td>" . $row['reminderDate'] . "</td>";
}
echo "</tr></table>";
You can clean that up a bit to take some (or all) of the HTML out of the PHP.
<?php include 'header.php'; ?>
<?php include 'database.php'; ?>
<div id="content">
<h1>Reminder List</h1>
<table>
<thead><tr><td>id</td><td>name</td><td>description</td><td>date</td></tr></thead>
<tbody>
<?php
$query = 'SELECT * FROM reminder_event';
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row['reminderID']; ?></td>
<td><?php echo $row['reminderName']; ?></td>
<td><?php echo $row['reminderDescript']; ?></td>
<td><?php echo $row['reminderDate']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<p><a href='reminder_add.php'>Add Reminder</a></p>
</div>
<?php include 'footer.php'; ?>