Change tr border based on longest element? - php

Here's the code:
<?php
mysqlLogin();
$username = $_COOKIE['username'];
$sql = mysql_query("SELECT username FROM `users` WHERE username!='$username'");
if(mysql_num_rows($sql) != 0) {
echo "<table class='usertable' align='center'>";
} else {
echo "<center>No users found!</center>";
}
while($row = mysql_fetch_array($sql)){
$length = strlen($row['username']) . 'px';
echo "<tr><td style='width: '$length'; border-bottom: 1px solid #000000'><center>" . $row['username'] . "</center></td></tr>";
}
?>
I want to make the bottom-border the length of the longest username in the SQL database.

This makes no sense... a border directive in CSS has no length - it automatically assumes the length/height of the element it's a border of.
If you mean you want to indicate which particular cell has the longest username and highlight it with a border-bottom, then you'd need to know in advance which username is longest. You can't have PHP "recall" the HTML it's already output to add the border directive after the fact.

Related

How could you use PHP and a SQL database to change HTML <h> content?

I have a webpage that displays cars from the first car in the table to the last car with a while loop.
I have the following columns: Make, Model, Price. In my syntax I have an anchor tag around the Make rows that links to the description page of the Make you click on.
I want my <h> tags to change to the Model of the corresponding Make.
I've spent over an hour trying to achieve this but all I could come up with is this:
<?php
$query = "SELECT Model FROM inventory;";
$Vtitle = $conn->query($query);
$Vtitle_ar = mysqli_fetch_assoc($Vtitle);
echo "<h1>".$Vtitle_ar['Model']."</h1>";
?>
This works to an extent.
Every anchor I click replaces the <h> tags with only the first result under the Model column in my database.
Here is my code for the the entire car inventory page
<?php
$query = "SELECT * FROM inventory;";
/* Try to query the database */
if ($result = $conn->query($query)) {
// Don't do anything if successful.
}
else {
echo "Error getting cars from database:" .$conn->error()."<br>";
}
// Create the table headers
echo "<table id='Grid' style='width: 80%'><tr>";
echo "<th style='width: 50px'>Make</th>";
echo "<th style='width: 50px'>Model</th>";
echo "<th style='width: 50px'>Asking Price</th>";
echo "</tr>\n";
$class = "odd"; // keep track of whether a row was even or odd, so we can style it later
// Loop through all the rows returned by the query, creating a table for each row
while ($result_ar = mysqli_fetch_assoc($result)) {
echo "<tr class=\"$class\">";
echo "<td><a href='viewcar.php?VIN=".$result_ar['VIN']."'>".$result_ar['Make']."<a></td>";
echo "<td>".$result_ar['Model']."</td>";
echo "<td>".$result_ar['ASKING_PRICE']."</td>";
echo "</td></tr>\n";
// if the last row was even, make the next one odd and vice-versa
if ($class=="odd") {
$class = "even";
}
else {
$class = "odd";
}
}
echo "</table>";
$conn->close();
?>
Does anyone how I can do this?
I'm new to programming and I'm trying to use this for an actual project I'm working on for a hair salon's website
Add a WHERE clause to the query.
$vin = $_GET['VIN'];
$stmt = $conn->prepare("SELECT Model FROM inventory WHERE VIN = ?");
$stmt->bind_param("s", $vin);
$stmt->execute();
$stmt->bind_result($model);
$stmt->fetch();
echo "<h1>$model</h1>";
Though not a solution resolved with the use of a where clause as given by #Barmar whilst formatting the code I did find an error within the HTML which was not immediately obvious
The line echo "</td></tr>\n"; has an extra </td> which would break the flow of the html and can have detrimental effects. Also, // Don't do anything if successful. makes no sense - if there are results then process the recordset otherwise show the error ;-)
<?php
$query = "SELECT * FROM inventory;";
if ( $result = $conn->query( $query ) ) {
echo "
<table id='Grid' style='width: 80%'>
<tr>
<th style='width: 50px'>Make</th>
<th style='width: 50px'>Model</th>
<th style='width: 50px'>Asking Price</th>
</tr>";
$i=0;
while( $result_ar = mysqli_fetch_assoc( $result ) ) {
$class = $i %2 == 0 ? 'even' : 'odd';
echo "
<tr class='$class'>
<td><a href='viewcar.php?VIN={$result_ar['VIN']}'>{$result_ar['Make']}<a></td>
<td>{$result_ar['Model']}</td>
<td>{$result_ar['ASKING_PRICE']}</td>
</tr>";
$i++;
}
echo "</table>";
} else {
echo "Error getting cars from database:" .$conn->error()."<br>";
}
$conn->close();
?>
For styling alternate table rows ( the above uses a modulus function to calculate odd/even ) you can do it with some simple CSS - such as
tr:nth-child( odd ){/* rules */}

How to automatically design a dynamic table when fetching row (PHP /MySQLi)

I'd like to design a dynamic table when fetching row in php/mysql.
Please see attached image of the example.
See my code below, it works almost well but the vertical bar in the middle doesn't seems to fit well.
for ($i = 0;$i<$result->num_rows;$i++){
$row = $result->fetch_array(MYSQLI_NUM);
//echo "* ".$row["0"]."<br>"; // Work great
//echo "* " .$row["0"] .$row["1"] ."<br>";
echo "| " .$row["1"] ."&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" ." | " ."&nbsp&nbsp&nbsp" .$row["2"] ."<br>";
}
What I want to do:
that is an example of how to echo HTML tables from php code
echo "<table><thead></thead><tbody>"
for ($i = 0;$i<$result->num_rows;$i++){
$row = $result->fetch_array(MYSQLI_NUM);
echo "<tr>".
"<td>{$row["0"]}</td>".
"<td>{$row["1"]}</td>".
"<td>{$row["2"]}</td>".
"</tr>\n";
}
echo "</tbody></table>"
CSS
td{
padding: 3px; /*add space to all directions (top, right,bottom, left)*/
padding-left: 6px; /*add space to the left side only*/
}
Put that css in the css files of the HTML document (it will be applied to all td elements in the document). Or -which is not recommended- by inline-css as like this
echo "<tr>".
"<td style='padding-left:6px'>{$row["0"]}</td>".
"<td style='padding-left:6px'>{$row["1"]}</td>".
"<td style='padding-left:6px'>{$row["2"]}</td>".
"</tr>\n";

Adding PHP table to mySQL files

hey guys this maybe a stupid question for you but I have a database containing footballers names (please see screen shot) What i need to do is display maybe 5 names side by side and then 5 below, if that make sense. is there an easy way to do this?
the code i currently have after the established connect is
$sql = "SELECT ID, NAME FROM players";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
// Outputting HTML and the data from the DB. Change to $row['the name of the field you want']
echo "<div class='caption'><h3><img src='http://placehold.it/180x180' alt=''>" . $row['NAME'] . "</h3><p>";
}
} else {
echo "No players to show";
}
$conn->close();
?>
is it CSS i should be focusing on to make this look the way i want it?
sorry i am a new at this and cant get my head around it lol
try this:
I have added a counter called i which will change the div after it has listed 5 names. Then I have styled the div to 50% width and float to left which allows 2 div side by side.
<style>
.caption
{
width:50%;
float:left;
}
<?php
$sql = "SELECT ID, NAME FROM players";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
echo "<div class='caption'>";
while($row = $result->fetch_assoc()) {
$i = 0;
if($i>=4)
{
$i = 0;
echo "</div><div class='caption'>";
// Outputting HTML and the data from the DB. Change to $row['the name of the field you want']
<h3><img src='http://placehold.it/180x180' alt=''><h3>" . $row['NAME'] . "<h3>";
}
} else {
echo "No players to show";
}
$conn->close();
?>
</div>
yes, you are doing it right way. You just need to do some css. You can take some help form here :- https://designshack.net/articles/css/5-simple-and-practical-css-list-styles-you-can-copy-and-paste/

make table using php data

I have created a table using information from my database which all works fine. I have this all echo'd back in position how i want it.
My question is when i add a background to this, it adds a background to everything in every row. How can i add a background to each row with maybe 10px padding in between each background?
echo "\n<table id=\"messageboard\" cellpadding=\"0\" cellspacing=\"0\">\n";
echo "<tr><th width=\"150px\" style=\"text-align: center;\"></th>\n";
echo "<th width=\"330px\" style=\"background-color: #c01718;\"></th>\n";
echo "</tr>";;
while($row = mysql_fetch_array($result)){
echo "<tr><td>\n";
echo $row ['username']."<br />".$row ['date_time'];
echo "<td>\n";
echo $row ['message'];
echo "</td></tr>\n";
}
You need to add the css class to the tr in order to give a style for every row
The starting row line should read
echo "<tr class=\"styledRow\"><td>\n";
Check this out:
http://www.utexas.edu/learn/html/colors.html
your problem can be solved by to ways:
add style in tr tag ,and give padding to tr 10px or more
alternatively
$style = ['style1','style2']
$i = 0
while($row = mysql_fetch_array($result))
{
if {$i ==0{$i = 1}}
else {$i = 0}
echo "<tr";
echo "class=$style[$i]";
echo "><td>\n";
echo $row ['username']."<br />".$row ['date_time'];
echo "<td>\n";
echo $row ['message'];
echo "</td></tr>\n";
}
now in css define two style sheet style1 and style2, your table row will have alternating style as style1 and style2,
please ignore syntax error

PHP: html table layout with data coming from query

I'm trying to fix the layout of the data coming from the database in this way:
The query is returning names of games and the points awarded for each game.
The layout I need is like that:
in one row the name of the game and in a row below its points, two columns with this and then carriage break until all results are displayed.
Please see this image, it'll show some light:
http://217.116.9.130/wordpress/HTMLtableFROMquery.gif
What I have done by far is the following code, but I cannot display the layout needed.
![<?php
echo "<table border=1><tr>";
$count=0;
$sql2 = "select * from games";
$res2= $db_class->select($sql2);
if (mysql_num_rows($res2)>0) {
while($row2 = $db_class->get_row($res2)){
$gname = $row2['gamename'];
$gpoints = $row2['gamepoints'];
$count++;
echo"<td>".$gname."</td>";
if($count % 2 != 0)
{
echo "</tr><tr>";
echo"<td>".$gpoints."</td>";
}
}
}
echo "</tr></table>";
?>
What about some divs?
<?php
echo "<table border=1><tr>";
$count=0;
$sql2 = "select * from games";
$res2= $db_class->select($sql2);
if (mysql_num_rows($res2)>0) {
while($row2 = $db_class->get_row($res2)){
$gname = $row2['gamename'];
$gpoints = $row2['gamepoints'];
$count++;
echo"<td>
<div class=\"gname\">".$gname."</div>
<div class=\"gpoints\">".$gpoints."</div>
</td>";
if($count % 2 != 0)
{
echo "</tr><tr>";
}
}
}
echo "</tr></table>";
?>
You may want to create a table for each set of name-score, and then style it after your needs.
You can use the border property in CSS to style your table, and also table:nth-child(2n){ clear: left; to have only two tables on each row.
I believe this method is more flexible, as CSS gives endless possibilities for styling an element.

Categories