After I put the database values into the HTML table, the table gets to big. Any CSS code doesnt help. Shall I change something in the database value type? Or any other suggestion?
Below is the code:
<?php
$connector = mysql_connect('localhost','root','')
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysql_select_db("user_registration", $connector)
or die("Unable to connect");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM login ORDER BY 1 DESC ");
?>
<table class="table1" border="2" >
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Surename</th>
<th>Email</th>
<th>Gender</th>
<th>Username</th>
<th>Password</th>
</tr>
</thead>
<tbody>
<?php
while( $row = mysql_fetch_assoc( $result ) ){
echo
"<tr >
<td >{$row['id']}</td>
<td>{$row['name']}</td>
<td>{$row['surename']}</td>
<td>{$row['email']}</td>
<td>{$row['gender']}</td>
<td>{$row['username']}</td>
<td>{$row['password']}</td>
</tr>\n";
}
?>
</tbody>
</table>
<?php mysql_close($connector); ?>
You need to select a subset of your records, otherwise the page will grow as fast as the database table... use the LIMIT statement to get back a page at a time...
SELECT id, name, etc
FROM MyTable
ORDER BY id
LIMIT (0, 20)
The next page is
SELECT id, name, etc
FROM MyTable
ORDER BY id
LIMIT (20, 20)
And so on.
Related
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>
Im new in php and sql right now so im confused whats wrong in my codes.(Need to do)cart_tbl (order_id) food_name,special_request, quantity, amount are equal to order_id of my order_tbl. When both order_id of my order_tbl and cart_tbl is same. My output will be the value of that 2 table. This is my code right now.
<?php
$connect = mysqli_connect ("localhost", "root", "" , "db");
if(isset($_POST['order_id'])){
$asd = ($_POST['order_id']);
$sql = "SELECT food_name, special_request, quantity, amount
FROM cart_tbl
WHERE order_id='$asd'";
$result = mysqli_query($connect, $sql);
}
?>
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>Food</th>
<th>Special Request</th>
<th>Quantity</th>
<th>Amount</th>
</tr>
</thead>
<?php
if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row["food_name"];?></td>
<td><?php echo $row["special_request"];?></td>
<td><?php echo $row["quantity"];?></td>
<td><?php echo $row["amount"];?></td>
</tr>
<?php
}
}
?>
</table>
Use INNER JOIN.
SELECT *
FROM cart_tbl
INNER JOIN order_tbl
ON cart_tbl.order_id = order_tbl.order_id
WHERE order_id='$asd'
Or, use NATURAL JOIN if the associated tables have the identical column name order_id and the columns are of same data type.
SELECT *
FROM cart_tbl
NATURAL JOIN order_tbl
WHERE order_id='$asd'
So I'm trying to use php and mysql to put data in my datatables on my website. I wrote some code at the top of my file to confirm that I am accessing my database correctly. The table is displayed correctly when i manually entered some data, but with my php code, the table says "No data available in table". Any thoughts on whats wrong with my php?
$q = "SELECT tickets.ticket_id, tickets.section, tickets.row, tickets.price, users.first_name as first_name, users.last_name as last_name
FROM tickets
LEFT JOIN users
ON tickets.seller_id = users.umid
Where(tickets.game_id = '$g')";
$r = #mysqli_query ($dbc, $q);
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Section</th>
<th>Row</th>
<th>Price</th>
<th>Seller</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo '
<tr>
<td>'.$row["section"].'</td>
<td>'.$row["row"].'</td>
<td>'.$row["price"].'</td>
<td>'.$row["first_name"]. " ". $row["last_name"]. '</td>
</tr>
';
}
?>
</tbody>
</table>
This is what the table looks like
Link to the webpage
Figured out the problem. I looped through my row earlier in my code, so when i wanted to put the info in the table, it was at the end of the array.
<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>
Hi I am trying to sort this table on the basis of $fratio. The one who has the highest $fratio will be placed as Number 1 in the table. Here's the code that I have worked so far -
// MySQL connection.
$connection = mysql_connect($host,$username,$password);
#mysql_select_db($database) or die( "Unable to select database. Be sure the databasename exists and online is.");
$query="SELECT `UserID`,`Playername`,`Kills`,`Deaths` FROM users LIMIT 0,50";
$query = mysql_query($query);
echo('<table width="300" border="2" cellspacing="3" cellpadding="3">
<tr>
<td style="min-width:150px;">Playername:</td>
<td style="width:100px">Kills:</td>
<td style="width:100px">Deaths:</td>
<td style="width:100px">Ratio:</td>
</tr>');
while($row = mysql_fetch_assoc($query))
{
$id = $row['UserID'];
$playername = $row['Playername'];
$kills = $row['Kills'];
$deaths = $row['Deaths'];
$ratio = ($kills/$deaths);
$fratio = ceil($ratio);
echo('
<tr>
<td style="min-width:150px;">'.$playername.'</td>
<td style="width:100px">'.$kills.'</td>
<td style="width:100px">'.$deaths.'</td>
<td style="width:100px">'.$fratio.'</td>
</tr>');
}
echo('</table>');
mysql_close($connection);
?>
You could try using
ORDER BY kills / deaths DESC
So the full query would look like
SELECT `UserID`,`Playername`,`Kills`,`Deaths` FROM users ORDER BY kills / deaths DESC LIMIT 0,50
And the full code would look like this:
// MySQL connection.
$connection = mysql_connect($host,$username,$password);
#mysql_select_db($database) or die( "Unable to select database. Be sure the databasename exists and online is.");
$query="SELECT `UserID`,`Playername`,`Kills`,`Deaths` FROM users ORDER BY kills / deaths LIMIT 0,50";
$query = mysql_query($query);
echo('<table width="300" border="2" cellspacing="3" cellpadding="3">
<tr>
<td style="min-width:150px;">Playername:</td>
<td style="width:100px">Kills:</td>
<td style="width:100px">Deaths:</td>
<td style="width:100px">Ratio:</td>
</tr>');
while($row = mysql_fetch_assoc($query))
{
$id = $row['UserID'];
$playername = $row['Playername'];
$kills = $row['Kills'];
$deaths = $row['Deaths'];
$ratio = ($kills/$deaths);
$fratio = ceil($ratio);
echo('
<tr>
<td style="min-width:150px;">'.$playername.'</td>
<td style="width:100px">'.$kills.'</td>
<td style="width:100px">'.$deaths.'</td>
<td style="width:100px">'.$fratio.'</td>
</tr>');
}
echo('</table>');
mysql_close($connection);
?>
Here's an example of the "ORDER BY". You should really consider reading atleast this to get the main idea of it.
http://www.w3schools.com/php/php_mysql_order_by.asp
you will make the ratio in mysql like :
ORDER BY CEILING( kills / deaths ) DESC