Here the value of $table will be passed from another file and it will be a table name in a database.
With that table name ($table) am trying to fetch its column names and all the values in the table and make the table look like an actual one we see in phpMyAdmin.
As a result of the code below. I can only get the column names. But not the data. Please tell me what should i do to perform the task of showing the datas in the table
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="display" rel="datatable">
<thead>
<tr>
<?php
//echo"select * from $table";
$qq=mysql_query("show columns from $table");
if(mysql_num_rows($qq)>0){
//$i=1;
echo '<tr>';
while($rs = mysql_fetch_row($qq))
{
$sel=mysql_query("select * from $table");
$fetch=mysql_fetch_object($sel);
//while($fetch=mysql_fetch_object($sel))
//{
?>
<td><?php echo $fetch->$rs[0];?></td>
<?php
//}
//$i++;
}
echo '</tr>';
}
else
{
?>
<tr>
<td colspan="11">No data to display</td>
</tr>
<?php
}
?>
</tbody>
</table>
Yes only columns will be printing because you are printing $rs[0];?> where $rs holds object for mysql_query qq and it holds your column query only not
"select * from $table"
this.
Why dont you try displaying columns as a single row in html table and then try displaying data from other php set with seperate query structure .
Hope this way it helps.
I coudn't fix your code because it was getting ugly, so i remade it:
php:
function mysql_fetch_all($res) {
$result = array();
while ($row = mysql_fetch_row($res)) {
$result[] = $row;
}
return $result;
}
function getColumnsAndData($table) {
$table = mysql_real_escape_string($table);
$q1 = mysql_query("show columns from $table");
$q2 = mysql_query("select * from $table");
return array(mysql_fetch_all($q1), mysql_fetch_all($q2));
}
list($columns, $data) = getColumnsAndData($table);
?>
html
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="display" rel="datatable">
<thead>
<tr>
<?php foreach ($columns as $column): ?>
<td><?php echo $column[0] . ' ' . $column[1] ?></td>
<?php endforeach; ?>
<tr>
</thead>
<tbody>
<?php if (count($data) > 0): ?>
<?php foreach ($data as $row): ?>
<tr>
<?php foreach ($row as $value): ?>
<td><?php echo $value ?></td>
<?php endforeach; ?>
<tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td>No data to display</td>
</tr>
<?php endif; ?>
</tbody>
</table>
Related
I'm using CodeIgniter and I'm trying to populate a <table> and just getting the same row 4 times.
My model:
$sql2=
"SELECT *
FROM puntos
WHERE checklist_idchecklist='$idchecklist'";
$qry2=$this->db->query($sql2);
$puntos=$qry2->row();
return $puntos
This query returns 2 arrays of 4 attributes each, i tested it doing the SQL query on phpmyadmin.
My controller:
function verpuntos() {
$this->load->model('checklistm');
$puntos=$this->checklistm->obtenerpuntos();
$this->load->view('plantilla/headerguardia');
$this->load->view('checklist/lista', $puntos);
$this->load->view('plantilla/footer');
}
My view:
$punto=array(
'descripcion' => $descripcion,
'lugar' => $lugar,
'tipo' => $tipo,
'urgencia' => $urgencia);
<table class="table">
<thead>
<tr>
<th>Descripcion</th>
<th>Lugar</th>
<th>Tipo</th>
<th>Urgencia</th>
</tr>
</thead>
<tbody>
<?php foreach($punto as $row) {
echo '<tr>';
echo '<td>'.$descripcion.'</td>';
echo '<td>'.$lugar.'</td>';
echo '<td>'.$tipo.'</td>';
echo '<td>'.$urgencia.'</td>';
echo '</tr>';
}
?>
<tbody>
</table>
And this is what I'm getting, the same row 4 times:
In codeigniter row() fetch the first row in object format.$puntos=$qry2->row(); This statement fetch only first row of your fetched data in object fromat. SO no need to use foreach loop. Just try like this..
echo '<tr>';
echo '<td>'.$puntos->descripcion.'</td>';
echo '<td>'.$puntos->lugar.'</td>';
echo '<td>'.$puntos->tipo.'</td>';
echo '<td>'.$puntos->urgencia.'</td>';
echo '</tr>';
For more see here https://www.codeigniter.com/userguide3/database/results.html
<table class="table">
<tr>
<th>Descripcion</th>
<th>Lugar</th>
<th>Tipo</th>
<th>Urgencia</th>
</tr>
</thead>
<tbody>
<?php
echo '<tr>';
foreach($punto as $row)
{
echo '<td>'.$row.'</td>';
}
echo '</tr>';
?>
</tbody>
<table>
try like this
echo '<tr>';
echo '<td>'.$punto['item_1_name'].'</td>';
echo '<td>'.$punto['item_2_name']..'</td>';
echo '<td>'.$punto['item_3_name']..'</td>';
echo '<td>'.$punto['item_4_name']..'</td>';
echo '</tr>';
item_1_name = index name of array returned
HTML Table getting mixed up when having echo in while loop. Bellow attached the output image for that. How can i get normal table with loop result? Please note i am getting table data by searching with manual date.
<?php
if (isset($_POST['submit_date'])) {
if (empty($_POST['m_date'])) {
echo '<div class="alert alert-danger">Error: Select date then search</div>';
}else{
$m_date = $_POST['m_date'];
$q = mysqli_query($conn, "SELECT * FROM bazar_dor WHERE m_date='$m_date'");
echo '<table style="width:100%">
<tr>
<th>Category Name</th>
<th>Price</th>
</tr>
<tr>
';
while ($row=mysqli_fetch_array($q)) {
$cat_name = $row['cat_name'];
$price = $row['price'];
echo '<td>'.$cat_name.'</td><td>'.$price.'</td>';
}
echo '</tr></table>';
}
}
?>
the output i am getting from this code
It looks like you'll need to include table rows (<tr>) inside your loop, as well. Your current code outputs all data on the same row, which undoubtedly breaks the table.
echo '<table style="width:100%">
<tr>
<th>Category Name</th>
<th>Price</th>
</tr>';
while ($row=mysqli_fetch_array($q)) {
$cat_name = $row['cat_name'];
$price = $row['price'];
echo '<tr><td>'.$cat_name.'</td><td>'.$price.'</td></tr>';
}
echo '</table>';
Replace your cede with this
<?php if (isset($_POST['submit_date'])) { if (empty($_POST['m_date'])) { echo '<div
class="alert alert-danger">Error: Select date then search</div>'; }else{ $m_date = $_POST['m_date'];
$q = mysqli_query($conn, "SELECT * FROM bazar_dor WHERE m_date='$m_date'");
echo '<table style="width:100%"> <tr>
<th>Category Name</th> <th>Price</th> </tr> ';
while ($row=mysqli_fetch_array($q)) { $cat_name = $row['cat_name'];
$price = $row['price']; echo '<tr><td>'.$cat_name.'</td><td>'.$price.'</td><\tr>';
} echo '</table>';
} } ?>
I'm in need of some help. I'm working in dreamweaver and I'm trying to insert values from my MySQL database into a table on my HTML page.
Dreamweaver generated these variables for me from the server behaviours
mysql_select_db($database_connection, $connection);
$query_mwAcc = "SELECT * FROM accounts";
$mwAcc = mysql_query($query_mwAcc, $connection) or die(mysql_error());
$row_mwAcc = mysql_fetch_assoc($mwAcc);
$totalRows_mwAcc = mysql_num_rows($mwAcc);
Now what I need help with is what to put into the while loop for my PHP script, this is what I have so far
<table class="table table-bordered">
<?php while (): ?>
<tr>
<td><?php echo $row['id'] ?></td>
</tr>
<?php endwhile; ?>
</table>
You need to write your fetch command within the while loop itself.
$query_mwAcc = "SELECT * FROM accounts";
$mwAcc = mysql_query($query_mwAcc, $connection) or die(mysql_error());
while($row_mwAcc = mysql_fetch_assoc($mwAcc)) {
?>
<tr>
<td><?php echo $row_mwAcc['id'] ?></td>
</tr>
<?php } ?>
</table>
With an update to use PDO you can change the while loop into a foreach on the query result. PDO should be used over mysql_ interface methods, due to deprecation: Why shouldn't I use mysql_* functions in PHP?
<?php
$dbh = new PDO('mysql:host=...;dbname=...', $user, $pass);
$results = $dbh->query('SELECT * FROM accounts');
?>
<table class="table table-bordered">
<?php foreach ($results as $row): ?>
<tr>
<td><?php echo $row['id'] ?></td>
</tr>
<?php endforeach; ?>
</table>
This question already has an answer here:
MySql Transpose Row into Column and Column into Row [duplicate]
(1 answer)
Closed 7 years ago.
I want to display rows as columns.
This is mysql table called Term
|Name ||Year ||HTML ||CSS ||Js ||
|Year1 ||2013-08-30||90 ||70 ||70 ||
|Year2 ||2014-08-30||100 ||65 ||80 ||
|Year3 ||2015-08-30||80 ||95 ||90 ||
What I want is to display as columns like this
|Subject||Year1 ||Year2 ||Year3 ||
|HTML ||90| ||100 ||80 ||
|CSS ||70| ||65 ||95 ||
|JS ||70| ||80 ||90 ||
Code
<tr>
<th>Code</th><th>Subject</th><th>year1</th>
<th>year2</th><th>year3</th>
</tr>
<?php
$query = mysql_query("SELECT * FROM term WHERE Stdid='$id'");
while ($row = mysql_fetch_assoc($query)) {
foreach($row as $key => $value) {
?>
<tr>
<th><?php echo $key ?></th>
<th><?php echo $value?></th>
<th>99</th><th>00</th>
</tr>
<?php } } } ?>
</table>
My result only works for the first year
|Subject||Year1 ||Year2 ||Year3||
|HTML ||90| ||? ||? ||
|CSS ||70| ||? ||? ||
|JS ||70| ||? ||? ||
You first need to transpose all data in a temporary array before you can output it again. I'll give you 2 methods to do this.
Method 1: just fetch every row and switch the row index by the column index:
<table>
<tr>
<th>Subject</th>
<th>year1</th>
<th>year2</th>
<th>year3</th>
</tr>
<?php
$mysqli = new mysqli('localhost', 'user', 'pass', 'database');
$id = 1;
$report = array();
$columnIndex = 0;
$query = $mysqli->query("SELECT HTML, CSS, Js FROM term WHERE Stdid='$id'");
while ($results = $query->fetch_assoc()) {
foreach ($results as $course => $score) {
$report[$course][$columnIndex] = $score;
}
$columnIndex++;
}
foreach ($report as $course => $results) { ?>
<tr>
<th><?php echo $course; ?></th>
<?php foreach ($results as $score) { ?>
<th><?php echo $score; ?></th>
<?php } ?>
</tr>
<?php } ?>
</table>
Method 2: Fetch all rows in an array, so it becomes an array of arrays and use array_map with callback NULL to transpose it (See example 4 at http://php.net/manual/en/function.array-map.php).
You need to add the course names in the initial array to include them in the end result.
<table>
<tr>
<th>Subject</th>
<th>year1</th>
<th>year2</th>
<th>year3</th>
</tr>
<?php
$mysqli = new mysqli('localhost', 'user', 'pass', 'database');
$id = 1;
$data = array(array('HTML', 'CSS', 'Js'));
$query = $mysqli->query("SELECT HTML, CSS, Js FROM term WHERE Stdid='$id'");
while ($row = $query->fetch_assoc())
{
$data[] = $row;
}
$data = call_user_func_array('array_map', array_merge(array(NULL), $data));
?>
<?php
foreach ($data as $row): ?>
<tr>
<?php foreach ($row as $value): ?>
<th><?php echo $value?></th>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
Try this.
<table width = "1093" align="center" bgcolor="pink">
<tr align="center">
<td colspan="6"><h2>View Term</h2></td>
</tr>
<tr align="center" bgcolor="">
<th>S.N</th>
<th>Subject</th>
<th>Year 1</th>
<th>Year 2</th>
<th>Year 3</th>
</tr>
<?php
$db = mysqli_connect("localhost","root","YOURPASSWORD","YOURDATABASE");
if (mysqli_connect_errno())
{
echo"The Connection was not established" . mysqli_connect_error();
exit();
}
$get_term = "SELECT * FROM term WHERE Stdid='$id'";
$run_term = mysqli_query($db,$get_term);
$i =0 ;
while ($row=mysqli_fetch_array($run_term ))
{
// Get data from database.
// Change the input in $row[''] if the name of column is different in your tble "term".
$subject=$row['subject'];
$year1=$row['year1'];
$year2=$row['year2'];
$year3=$row['year3'];
$i++;
?>
<tr align="center">
<td> <?php echo $i;?></td>
<td><?php echo $subject;?></td>
<td><?php echo $year1;?></td>
<td><?php echo $year2;?></td>
<td><?php echo $year3;?></td>
</tr>
<?php }?>
</table>
I have been handed the task of making some changes to a PHP webpage that was coded by someone who has left the company and my Php is exactly exeprt.
The page in question displays a database table in a SQL server that allows you to update values via an update page.
Currently the Update function sits under the 'Action' column at the end of the table and I need to relocate the 'Action' column to the start of the table before the 'Name' column.
When I try to make changes, I break the table array and the 'Update' function no longer works.
Current order of columns are;
Name,
Value,
Details,
Action
The new order of columns attempting to achieve
Action,
Name,
Value,
Details
I have also included the code in question.
Any assistance would be appreciated
Note** It is a Php website running on a Windows box and connecting to a MSSQL Server 2008
$query = sqlsrv_query($conn, 'SELECT * FROM Database_Values ORDER BY Name ASC');
// Did the query fail?
if (!$query) {
die( FormatErrors( sqlsrv_errors() ) );
}
// Dump all field names in result
$field_name = "";
foreach (sqlsrv_field_metadata($query) as $fieldMetadata) {
foreach ($fieldMetadata as $name => $value) {
if ($name == "Name") {
$field_name .= $value . '-';
}
}
}
$field_name = substr_replace($field_name, "", -1);
$names = explode("-", $field_name);
?>
<div style="max-height:610px; overflow:auto;">
<table border="1" cellspacing="0" cellpadding="0" bordercolor="#ccc" class="table" width="100%">
<tr>
<?php
foreach ($names as $name) {
?>
<th><?php echo $name; ?></th>
<?php
}
?>
<th>Action</th>
</tr>
<?php
// Fetch the row
while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) {
//print_r($row);
?>
<tr>
<?php
foreach ($row as $key => $eachrow) {
?>
<td nowrap="nowrap">
<?php echo $eachrow; ?>
</td>
<?php
}
?>
<td nowrap="nowrap">
<?php $groupid = $_SESSION["gid"] ;
if($groupid!='1') {
?>
<a href="javascript:void(0);" title="Permission Restricted" >Update</a>
<?php } else { ?>
Update
<?php } ?>
</td>
</tr>
<?php
}
?>
</table>
</div>
All you need to do is change the order of the th and td cells in the html
$query = sqlsrv_query($conn, 'SELECT * FROM Database_Values ORDER BY Name ASC');
// Did the query fail?
if (!$query) {
die( FormatErrors( sqlsrv_errors() ) );
}
// Dump all field names in result
$field_name = "";
foreach (sqlsrv_field_metadata($query) as $fieldMetadata) {
foreach ($fieldMetadata as $name => $value) {
if ($name == "Name") {
$field_name .= $value . '-';
}
}
}
$field_name = substr_replace($field_name, "", -1);
$names = explode("-", $field_name);
?>
<div style="max-height:610px; overflow:auto;">
<table border="1" cellspacing="0" cellpadding="0" bordercolor="#ccc" class="table" width="100%">
<tr>
<th>Action</th>
<?php
foreach ($names as $name) {
?>
<th><?php echo $name; ?></th>
<?php
}
?>
</tr>
<?php
// Fetch the row
while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) {
//print_r($row);
?>
<tr>
<td nowrap="nowrap">
<?php $groupid = $_SESSION["gid"] ;
if($groupid!='1') {
?>
<a href="javascript:void(0);" title="Permission Restricted" >Update</a>
<?php } else { ?>
Update
<?php } ?>
</td>
<?php
foreach ($row as $key => $eachrow) {
?>
<td nowrap="nowrap">
<?php echo $eachrow; ?>
</td>
<?php
}
?>
</tr>
<?php
}
?>
</table>
</div>