Display all mysql data in rows one by one - php

I am fetching all the records from mysql but it split in to rows as shown in the image
I want that it display row wise means the column which are splitting in the column means "future of india" and GAURAV should come in line now row wise. When the body part full it auto split into new row. here is my code
$sql = "SELECT * FROM category order by id ASC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo '<table width="30%" border="0">
<tr>
<td><div id="rcorners2">'.$row["category"].' </div></td>
</tr>
</table>

Use <span> instead of<div> in the <td> and it will work

You need to move your loop.
$sql = "SELECT * FROM category order by id ASC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
echo '<table width="30%" border="0">
while($row = mysqli_fetch_assoc($result)) {
<tr>
<td>
<div id="rcorners2">'.$row["category"].' </div>
</td>
</tr>";
}
echo "</table>";

You should take table wrap all rows.
<?php
$sql = "SELECT * FROM category order by id ASC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
echo '<table width="30%" border="0">'
while($row = mysqli_fetch_assoc($result)) {
echo '
<tr>
<td><div id="rcorners2">'.$row["category"].' </div></td>
</tr>';
}
echo '</table>';
}
?>

If I understand correctly, I'll write pseudocode
<table>
<tr>
$query = "..."
while ($row = fetch()) {
<td>
print row data
</td>
}
</tr>
</table>

Related

PHP/mysql:Rank users

I have this table which displays data from mysql and ordering them by most of clicks
but i don't have #1 or #2 and so on....
here is the code:
<table align="center" style="width:50%">
<tr>
<th>User</th>
<th>clicks</th>
</tr>
<tr>
<td>
<?php include 'conn.php';
$sql = "select user, count(*) duplicates from users group by user order by duplicates desc";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["user"]. "<br>";
}
} else {
}
?>
</td>
<td>
<?php
include 'conn.php';
$sql = "select user, count(*) AS duplicates from users group by user order by duplicates desc";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["duplicates"]. "<br>";
}
} else {
}
$conn->close();
?></td>
</tr>
</table>
output is this in a table:
users clicks
user1 3
user2 2
user3 1
but i want the output to be:
rank users clicks
1 user1 3
2 user2 2
3 user3 1
thanks for answering
Add Rank section to table
<tr>
<th>Rank</th>
<th>User</th>
<th>clicks</th>
</tr>
Add rank for each result
<td>
<?php include 'conn.php';
$sql = "select user, count(*) duplicates from users group by user order by duplicates desc";
$result = $conn->query($sql);
$rank = 1;
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $rank. "<br>";
$rank++;
}
}
?>
</td>
Full Code
<table align="center" style="width:50%">
<tr>
<th>Rank</th>
<th>User</th>
<th>clicks</th>
</tr>
<tr>
<td>
<?php include 'conn.php';
$sql = "select user, count(*) duplicates from users group by user order by duplicates desc";
$result = $conn->query($sql);
$rank = 1;
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $rank. "<br>";
$rank++;
}
}
?>
</td>
<td>
<?php include 'conn.php';
$sql = "select user, count(*) duplicates from users group by user order by duplicates desc";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["user"]. "<br>";
}
}
?>
</td>
<td>
<?php
include 'conn.php';
$sql = "select user, count(*) AS duplicates from users group by user order by duplicates desc";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["duplicates"]. "<br>";
}
}
$conn->close();
?></td>
</tr>
</table>
using javascript:
<script type="text/javascript">
var nb = document.getElementsByClassName("number");
for (var i = 0; i < nb.length; i++) {
nb[i].innerHTML = (i+1)+".";
}
</script>
<table align="center" style="width:50%">
<tr>
<th>User</th>
<th>clicks</th>
</tr>
<tr>
<td class="number">
<?php include 'conn.php';
$sql = "select user, count(*) duplicates from users group by user order by
duplicates desc";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["user"]. "<br>";
}
} else {
}
?>
</td>
<td>
<?php
include 'conn.php';
$sql = "select user, count(*) AS duplicates from users group by user order by
duplicates desc";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["duplicates"]. "<br>";
}
} else {
}
$conn->close();
?></td>
</tr>
</table>

search data and will choose to display in static table. like an add to cart

I want to display data after in static table just like an "Add to Cart".
<table>
<th>Item</th> <th>Description</th>
<tr>
<td>Monitor<td>
<td>Monitor Blue<td>
</tr>
<tr>
<td>Mouse<td>
<td>Mouse Wireless<td>
</tr>
<tr>
<td>Keyboard<td>
<td>Keyboard<td>
</tr>
My search form : This is no problem is searching item. Only in the add button every row. So when you search for item you have choice if you want to add in static table.
Search:
Mysql when search
<?
$sql = "select * from item_tb where name='".$search."'";
$result=$conn->query($sql);
echo "
<table cellspacing='20' border='0' ><tr> <th>Item</th><th>Description</th>
</tr>";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "
<tr> <td>
".$row["name"]."</td> <td>
".$row["description"]."</td>";
echo" <form name='add' method='POST'>";
echo" <td><a href='item.php?id=".$row['Iid']."'><input type='submit' value='Add'></td></a>";
echo" </form>";
}
}
?>
Here is my main problem. i don't how to make table where i'll get the data i have search and will display as many as item i add. I'm really down in this.
<?
if($id != "")
{
$sql = "SELECT * FROM item_tb WHERE Iid='".$id."'";
$result=$conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo" ".$row["name"]." ";
}
}
}
?>
<?php
if($id != "") {
$sql = "SELECT * FROM item_tb WHERE Iid='" . $id . "'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><?= $row['name'] ?></td>
</tr>
<?php
}
}
}
?>

Showing data correctly in html table with php

Hello this table pulls data from a database.
<h2>Weekly appointment list</h2>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Week Day</th>
<th>Customers</th>
<th>Selected service</th>
<th>Time</th>
</tr>
</thead>
<tbody>
<tr>
<td>Monday</td>
<?php
$date = date('Y-m-d', strtotime("this Monday"));
$sql = "SELECT * FROM appointment WHERE weekday = '$date'";
$query = mysqli_query($db, $sql);
$numRows = mysqli_num_rows($query);
if ($numRows > 0) {
while ($row = mysqli_fetch_array($query)) { ?>
<td><?php echo $row['user_name'] ?></td>
<td><?php echo $row['service'] ?></td>
<td><?php echo $row['time'] ?></td>
<?php }
}
?>
</tr>
</tbody>
But the problem is when i have two users from echo $row['user_name'] //user x, user y the table rows break and show something like this: image link. See the the table row is broken. I want to show this way:expected table structure. All customers are shown on the particular day row in customers column. How to fix my code or the way of representation. Thanks in advance.
change your sql query to group concat username,service and time.
$sql ="SELECT group_concat(user_name) as user_name,group_concat(service) as service ,group_concat(time) as time FROM appointment WHERE weekday = '$date'";
This query will return one row with all the user and service information in one row.
you want to combine something like this
With something like this:
$res = mysql_query(/**/);
$rows = array();
while($row = mysql_fetch_assoc($res)){
array_push($rows, $row);
}
Let me know if you struggle.

PHP Display 2 Last Row data to Last Row HTML table based on Date Added

<table>
<td>ID</td>
<td>Name</td>
<td>Date Added</td>
<tr>
<?php
$q = mysql_query("SELECT * FROM tab ORDER BY date_added DESC LIMIT 2");
while($d = mysql_fetch_array($q))
{
echo "<td>$d['id'];</td>";
echo "<td>$d['name'];</td>";
echo "<td>$d['date_added'];</td>";
echo "<tr>";
}
Example, I have 4 data.
I want to get 2 last row based on date_added DESC and display it in last row HTML table td.
Please help.
Try this
<table>
<tr>
<td>ID</td>
<td>Name</td>
<td>Date Added</td>
</tr>
<?php
$q = mysql_query("SELECT id,name,date_added FROM table ORDER BY date_added DESC LIMIT 2");
while($d = mysql_fetch_array($q))
{
echo "<tr><td>".$d['id']."</td>";
echo "<td>".$d['name']."</td>";
echo "<td>".$d['date_added']."</td>";
echo "<tr>";
}
?>
</table>

PHP Table. Displaying multiple rows into one

Here is my code:
<table border="2px solid #FFF" width="100%">
<tr>
<td width="50%"><Center>Username:</Center><br /></td>
<td width="50%"><center>Numebr Of Warnings:</center><br /></td>
</tr>
</table>
<?
$query = mysql_query("SELECT * FROM warnings");
$numrows = mysql_num_rows($query);
if($numrows != "0"){
while($row = mysql_fetch_assoc($query)){
$warned = $row['username'];
$by = $row['by'];
$sql = mysql_query("SELECT * FROM warnings WHERE username='$warned'");
$warns = mysql_num_rows($sql);
?>
<table border="2px solid #FFF" width="100%">
<tr>
<td width="50%"><center><b><?php echo $warned; ?></b></center></td>
<td width="50%"><center><b><?php echo $warns; ?></center></b></td>
</tr>
</table>
<?php
}
}
else
echo "No Currently Warned Users";
?>
<hr />
And here is the result:
How can I make it so that instead of showing the 2 results for the user Mrg..... I just want it to show one result with the number or rows there are.
Help would be appreciated.
You can use DISTINCT in your query to avoid duplicate rows in your results:
SELECT DISTINCT username, `by` FROM warnings
Change the mysql_fetch_assoc with mysql_feth_array
You're getting the user with double record. Then for each user you are getting 2 lines
The best way to check it is to add pseudo-points to your code.
$query = mysql_query("SELECT * FROM warnings");
$arr=array();
while($row = mysql_fetch_assoc($query))
{
$arr[]=$row;
}
echo "<pre>";
print_r($arr);
echo "</pre>";
exit;
Here check if the $arr has double entries. Then change the mysql_fetch_assoc with mysql_fetch_array and try again
Here is the table
(by,id,username,date)
a1,1,u1,date
a2,2,u2,date
a3,3,u2,date
a4,4,u2,date
And here is the php code
<?php
$conn = mysqli_connect("localhost","username","password");
mysqli_select_db($conn,"dbname");
$warns = mysqli_query($conn,"select username, count(username) as warncount from dtest.warnings W group by username");
while($line = mysqli_fetch_array($warns))
{
echo $line['username']." has ".$line['warncount']. " warns <br/> ";
}
?>

Categories