PHP echo mysql table column names for html table header - php

RESOLVED
Works perfectly! Here is my final code:
<table>
<thead>
<tr>
<?php
$row = mysql_fetch_assoc($result);
foreach ($row as $col => $value) {
echo "<th>";
echo $col;
echo "</th>";
}
?>
<th>Edit</th>
</tr>
</thead>
<tbody>
<?php
// Write rows
mysql_data_seek($result, 0);
while ($row = mysql_fetch_assoc($result)) {
?>
<tr>
<?php
foreach($row as $key => $value){
echo "<td>";
echo $value;
echo "</td>";
}
?>
<td><button id="edit_project_button(<?php echo $row['ID']; ?>)" class="edit-project-button edit button" onclick="editproject(<?php echo $row['ID']; ?>)">Edit</button></td>
</tr>
<?php } ?>
</tbody>
</table>
I wish to echo out a HTML table using mysql_fetch functions appropriately. I plan on making a thead to contain the mysql table column names and a tbody to contain the mysql table resultset. The SQL query selects a couple of columns from the table, with default limit set.
The issue: It doesn't seem to print the first row of table data, everything else displays (record #1 missing)
It displays the with column names echo'd within each , it then skips the first record and successfully echo's the 2nd row onward. For example:
| id | firstname | lastname | date_start | date_end | clientid | members | edit |
| 2 | Cal | Clark | 2012-12-12 | 2012-12-12 | 22 | Rob | (edit button) |
| 3 | Rob | Robin | 2012-12-12 | 2012-12-12 | 33 | Cal | (edit button) |
I'm 100% sure that the first record will display from my query in phpmyadmin.
Here is my code:
<table>
<thead>
<tr>
<?php
$row = mysql_fetch_assoc($result);
foreach ($row as $col => $value) {
echo "<th>";
echo $col;
echo "</th>";
}
?>
<th>Edit</th>
</tr>
</thead>
<?php
// Write rows
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><?php echo $row[0]; ?></td>
<td><?php echo $row[1]; ?></td>
<td><?php echo $row[2]; ?></td>
<td><?php echo $row[3]; ?></td>
<td><?php echo $row[4]; ?></td>
<td><?php echo $row[5]; ?></td>
<td><?php echo $row[6]; ?></td>
<td><button id="edit_project_button(<?php echo $row[0]; ?>)" class="edit-project-button edit button" onclick="editproject(<?php echo $row[0]; ?>)">Edit</button></td>
</tr>
<?php } ?>
</table>
I feel so oblivious right now =/

make a rewind of your data first!!
mysql_data_seek($result, 0);
while ($row = mysql_fetch_array($result)) {
...

Related

Could not get sum of each row in a column in php mysql table

enter image description hereenter image description hereI have a database table having multiple cols and data in many rows. I want to get sum of all data of all the data in each row and place in a col 'Total'. However, Grand total is showing in each row and not the total of each row.
I am presenting the code as below
<?php
include 'config.php';
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$add=mysqli_query($con,"SELECT Col1, Col2, Col3, Col4,
sum(Col1 + Col2 + Col3 + Col4) AS Total, SUM(Col1), SUM(Col2),
SUM(Col3), SUM(Col4)
from DistrictData WHERE District = 'District1' ");
while($row=mysqli_fetch_array($add))
{
$Total = $row['Total'];
$SUM1=$row['SUM(Col1)'];
$SUM2=$row['SUM(Col2)'];
$SUM3=$row['SUM(Col3)'];
$SUM4=$row['SUM(Col4)'];
$Total2= $SUM1 + $SUM2+ $SUM3+ $SUM4 ;
}
}
$sql="SELECT * FROM DistrictData WHERE District = 'District1'"; $counter = 0;
$result = $con->query($sql);
?>
<body>
<table >
<tr>
<td>Sr No.</td>
<td>Deptt</td>
<td>Data Description</td>
<td>Unit</td>
<td>Year</td>
<td>Blocks in District</td>
</tr>
<tr>
<td>Total</td>
<td>Col1</td>
<td>Col2</td>
<td>Col3</td>
<td>Col4</td>
</tr>
<?php
while($rows = $result->fetch_assoc())
{
?>
<tr>
<td><?php echo ++$counter; ?></td>
<td><?php echo $rows['Data']; ?></td>
<td><?php echo $rows['Description']; ?></td>
<td><?php echo $rows['Unit']; ?></td>
<td><?php echo $rows['Year']; ?></td>
<td> <?php echo $Total ?></td>
<td><?php echo $rows['Col1']; ?></td>
<td><?php echo $rows['Col2']; ?></td>
<td><?php echo $rows['Col3']; ?></td>
<td><?php echo $rows['Col4']; ?></td>
</tr>
<?php
}
?>
<tr>
<td>Total:</td>
<td> <?php echo $Total2 ?></td>
<td> <?php echo $SUM1 ?></td>
<td> <?php echo $SUM2 ?></td>
<td> <?php echo $SUM3 ?></td>
<td> <?php echo $SUM4 ?></td>
</tr>
</table>
<?php
$con->close();
?>
Output of the code
Please suggest any changes in the code so that I could get the sum for each row in Total Column.
Thanks in advance
Use this query for row sum of column elements
SELECT Col1+Col2+Col3+Col4 as I_want_this_figure from DistrictData
Note that a normalised environment would typically look more like this...
sr_no district_block val
1 1 5
1 2 6
1 3 5
1 4 8
2 1 226
2 2 112
2 3 4
2 4 5
3 1 2
3 2 7
3 3 3
3 4 8
... where (sr_no,district_block) are components of a PRIMARY KEY
Use Allies after every Sum() as something.
You will get last result when you take away total from while loop.
do your first Query under 2nd Query and pass the ID as parameter.
while($row = mysqli_fetch_array($query)){
$total_query = mysqli_query($con,"SELECT Col1, Col2, Col3, Col4,
SUM(Col1 + Col2 + Col3 + Col4) as Total from DistrictData WHERE id='row[yourID]'");
$total_result = mysqli_fetch_array($total_query);
NOW you can populate your data here
}
Hope this will help you.

How to passed value from looping result to new object?

How to passed value from looping result to new object using button ?
For example :
I have input text to get order number. The result for Order number 0001 is like this
| Order No | Delivery No | Invoice No | Delivery Date | Action |
=================================================================
| 0001 | D0001 | I0001 | 21-03-2018 |(process button)|
| 0001 | D0002 | I0002 | 21-03-2018 |(process button)|
I have no trouble to display table above, the problem is how to make those process button work ? Process button is to display detail of one invoice. From example above, if i click process button for Invoice I0002 result will be like this
| No. Item | Item Name | Price | Qty | Amount |
================================================
| 0000001 | apple | 5 | 2 | 10 |
and so on..
So far my work for first table
$select_order = "SELECT ORDER_NO, DELI_NO, INV_NO, DELI_DT, AMOUNT FROM ORDER WHERE ORDER_NO = '$text_input_order'";
$exec_order = odbc_exec($connSQL, $select_order);
while(odbc_fetch_row($exec_order))
{
$get_order_no = odbc_result($exec_order, "ORDER_NO");
$get_deli_no = odbc_result($exec_order, "DELI_NO");
$get_inv_no = odbc_result($exec_order, "INV_NO");
$get_deli_dt = odbc_result($exec_order, "DELI_DT");
$get_amount = odbc_result($exec_order, "AMOUNT");
?>
<tr>
<td><? echo $get_order_no;?></td>
<td><? echo $get_deli_no ;?></td>
<td><? echo $get_inv_no ;?></td>
<td><? echo $get_deli_dt ;?></td>
<td><? echo $get_amount ;?></td>
<td><input type="submit" value="proces" name="proces"></td>
</tr>
<?
}
Now I need your help to make second table using process button.
Please made the changes as per your requirements after button click.
<script>
$(function(){
$('button[name=proces]').click(function(){
var id= $(this).attr("id");
//some action here
//ex:
window.location.href="<put your site url>?id="+id;
//or an ajax function
});
});
</script>
<?php
$select_order = "SELECT ORDER_NO, DELI_NO, INV_NO, DELI_DT, AMOUNT FROM
ORDER WHERE ORDER_NO = '$text_input_order'";
$exec_order = odbc_exec($connSQL, $select_order);
while(odbc_fetch_row($exec_order))
{
$get_order_no = odbc_result($exec_order, "ORDER_NO");
$get_deli_no = odbc_result($exec_order, "DELI_NO");
$get_inv_no = odbc_result($exec_order, "INV_NO");
$get_deli_dt = odbc_result($exec_order, "DELI_DT");
$get_amount = odbc_result($exec_order, "AMOUNT");
?>
<tr>
<td><? echo $get_order_no;?></td>
<td><? echo $get_deli_no ;?></td>
<td><? echo $get_inv_no ;?></td>
<td><? echo $get_deli_dt ;?></td>
<td><? echo $get_amount ;?></td>
<td><input type="button" id=".$get_inv_no." value="proces" name="proces"></td>
</tr>
<?
}

How to write Orderby Clause with where with where clause to sort data

hi i'm trying sort mysql data in table but my problem is that my query is sorting all the data my table looks like
Word | Meaning | Synonym | Antonym
definitely | without doubt | certainly |possibly
great | of an extent, amount | considerable |little
zeal | great energy | passion | indiference
zealot | a person who is fanatical|fanatic | moderate
zealous | having or showing zeal. | fervent | apathetic
so when i search lets say word starting wit z then i get
Word | Meaning | Synonym | Antonym
zeal | great energy | passion | indiference
zealot | a person who is fanatical|fanatic | moderate
zealous | having or showing zeal. | fervent | apathetic
now i want to perform sorting on these searched data but my sort query is sorting the all data
my store procedure looks like
CREATE DEFINER=`root`#`localhost` PROCEDURE `SortContent`()
BEGIN
SELECT * from dictionarysearch ORDER BY word ASC;
END
and my php code is like
<?php
if(isset($_GET['search_btn'])){
$search=$_GET['search'];
$result=GetWords(mysqli_escape_string($conn,$search));
}
/*if(isset($_GET['q'])){
$id=$_GET['q'];
$result=GetWordsById($id);
}*/
if(isset($_GET['sort'])){
$sort=$_GET['sort'];
}
if(isset($_GET['sort'])){
if($sort=="asc"){
$result=SortContent();//Here i'm calling a function which is calling the store procedure
}
if($sort=="desc"){
$result=SortContent2();
}
}
else{
$result=GetAdminWords();
}
if(mysqli_num_rows($result)>0)
?>
<thead>
<tr>
<th>Word</th>
<th>Meaning</th>
<th>Synonym</th>
<th>Antonym</th>
</tr>
</thead>
<?php
while($row=mysqli_fetch_array($result)){
?>
<tbody>
<tr>
<td><?php echo $row['word'];?></td>
<td><?php echo $row['meaning'];?></td>
<td><?php echo $row['synonym'];?></td>
<td><?php echo $row['antonym'];?></td>
<td><i class="fa fa-edit"></i> <a onClick="javascript: return confirm('Please confirm deletion');" href="view.php?id=<?php echo $row['id'];?>"><i class="fa fa-trash"></i></a> </td>
</tr>
</tbody>
<?php
}?>
</table>
so i want to know how i can write a query so that it sorts only selected data and i have set id autoincrement
There is no need to use a stored Procedure for this. I suggest that the following code will work for you...
<?php
if(isset($_GET['search_btn']) && strlen($_GET['search'])){
$search = mysqli_escape_string($conn,$_GET['search']);
$sql = "SELECT * FROM dummydata WHERE info LIKE '".$search."%'";
if(isset($_GET['sort']) && in_array($_GET['sort'], array('ASC', 'DESC'))){
$sort=$_GET['sort'];
$sql .= " ORDER BY info ".$sort;
}
//echo "<h3>".$sql."</h3>";
if (($result=mysqli_query($conn,$sql))!==false) {
?>
<thead>
<tr>
<th>Word</th>
<th>Meaning</th>
<th>Synonym</th>
<th>Antonym</th>
</tr>
</thead>
<tbody>
<?php
while ($row=mysqli_fetch_assoc($result)){
//echo "<pre>".var_export($row,true)."</pre>";
?>
<tr>
<td><?php echo $row['word'];?></td>
<td><?php echo $row['meaning'];?></td>
<td><?php echo $row['synonym'];?></td>
<td><?php echo $row['antonym'];?></td>
<td>Your action url here.... </td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
} else {
echo "<h3>Problem with SQL</h3>";
}
}
?>
I hope that this helps.

Eliminating duplicate specific rows?

Please Help!! I just want to display the Barangay which is talojongon with no other duplicate values
Let's say my current output is:
| Barangay | Disease |
__________________________________
| Talojongon | Cancer |
| Talojongon | Cancer |
| Talojongon | Cancer |
and i want to turn this to:
| Barangay | Disease |
__________________________________
| Talojongon | Cancer |
| | Cancer |
| | Cancer |
The receive in the database is for disease and status in the database is the barangay...thank you...Any suggestions is appreciated ..:)
This is my current part of my code
<br>
<table id="keywords" class="table table-striped table-bordered">
<thead>
<tr>
<th><center>Barangay</th></center>
<th><center>Diseases</th></center>
</tr>
</thead>
<tbody>
<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM patients WHERE receive="Cancer"';
foreach ($pdo->query($sql) as $row){
echo '<tr>';
echo '<td><center>'. $row['status'] . '</center></td>';
echo '<td><center>'. $row['receive'] . '</center></td>';
}
Database::disconnect();
?>
</tr>
</tbody>
How about this?
<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM patients WHERE receive="Cancer" order by status';
$last = '';
foreach ($pdo->query($sql) as $row){
echo '<tr>';
if($last == $row['status'])
{
echo '<td></td>';
}
else
{
$last = $row['status'];
echo '<td><center>'. $row['status'] . '</center></td>';
}
echo '<td><center>'. $row['receive'] . '</center></td>';
}
Database::disconnect();
?>
<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM patients WHERE receive="Cancer"';
$counter=0;
foreach ($pdo->query($sql) as $row){
$counter++;
echo '<tr>';
if($counter==1){
echo '<td><center>Cancer</center></td>';
}else{
echo '<td><center></center></td>';
}
echo '<td><center>'. $row['receive'] . '</center></td>';
}
Database::disconnect();
?>
Simply echo the status just in the first row...
Save the last value of $row['status'] in a variable. If it's the same leave it blank in the output.
$last_status = null;
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
$status = $row['status'] == $last_status ? '' : $row['status'];
echo '<td style="text-align: center;">' . $status . '</td>';
echo '<td style="text-align: center;">' . $row['receive'] . '</td>';
echo '</tr>';
$last_status = $row['status'];
}
Also, you shouldn't use the obsolete <center> tag, use CSS. And </tr> should be inside the loop, not at the end.
You probably should also use ORDER BY status in the query, so that all the rows with the same status are together.

how to subtract two columns output in a third column using PHP

Using PHP I want to subtract each values of used column from credit column. I have created a tableA in phpmyadmin.
remaining:credit-used I can not get the results i just get same values of credit.
// The desire output in table A
id |date |credit |used|remaining|
1 |20-5-2013 |400 |300 |100 |
2 |19-5-2013 |300 |100 |200 |
3 |18-5-2013 |600 |50 |550 |
// db connection
query select all from tableA
$sql = "SELECT * FROM tableA";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
?>
fetch the information and output into a table:
<table >
<tr>
bla bla..
</tr>
// echo:
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["date"]; ?></td>
<td><?php echo $row["credit"]; ?></td>
<td><?php echo $row["used"]; ?></td>
I'm trying to do this query to get remaining:
// <td> <?php> echo $total= $row["credit"] - $row["used"];?>
</td>
</tr>
</table>
can anyone give me a hints or highlight the error. Than you very much in advance.
What you have should work, but why not let MySQL do the calculation?
mysql_query("SELECT *, `credit`-`used` AS `remaining` FROM `tableA`");
You have a closing bracket on your php
<td> <?php echo $row["credit"] - $row["used"]; ?> </td>

Categories