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"] ."               " ." | " ."   " .$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";
Related
I was wondering how I could add a scroll to my table that is written in the php file. I do not want to write it in a style.css file, I want it directly in the php file. below is my code, but I am not able to make it to work. The table gets content from mySql database, which works. but the problem is that I get to much of content so it fills out the whole page. That is why I want to make it scrollable :
if(mysqli_num_rows($result) > 0){
echo '<table border="1">';
echo "<tr>";
echo "<th>Name</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "</tr>";
}
echo "</table>";
If im getting your question right you need to set a width and height then set the overflow-y. This will give you a table set to a certain size with a scroll bar. Note you need to set your own width and height.
echo "<table style=\"width:500px; height:500px; overflow-y:auto\">";
I've got my query working, it brings me the results I expect, but one column is going to be images, and I'm unsure of the best way to go about displaying them.
Initially I used the below code to get my results, without worrying about the images.
<?php
#requires connect script - similar to include to avoid including security details
require 'connectscript.php';
#query
$sql = "SELECT * FROM products WHERE CategoryName = 'Surfboards'";
#result
$result = mysqli_query($dbc, $sql) or die ("Bad Query: $sql");
#Opening table
#while loop to bring all results
echo "<table border='2'>";
echo "<tr><td>ProductID</td><td>Name</td><td>Brand</td><td>Model</td>
<td>Board Length</td><td>Board Type</td><td>Colour</td><td>Image</td>
<td>UnitPrice</td></tr>";
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>{$row['ProductID']}</td>
<td>{$row['Name']}</td>
<td>{$row['Brand']}</td>
<td>{$row['Model']}</td>
<td>{$row['BoardLength']}</td>
<td>{$row['BoardType']}</td>
<td>{$row['Colour']}</td>
<td style='width:200px; height: 100px; overflow-hidden;'>{$row['Image']}</td>
<td>{$row['UnitPrice']}</td>
</tr>";
}
echo"</table>"
?>
I've also adjusted the code slightly with an img src in there, and it presents the broken as if it's looking for an image, but can't find one.
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>{$row['ProductID']}</td>";
echo "<td>{$row['Name']}</td>";
echo "<td>{$row['Brand']}</td>";
echo "<td>{$row['Model']}</td>";
echo "<td>{$row['BoardLength']}</td>";
echo "<td>{$row['BoardType']}</td>";
echo "<td>{$row['Colour']}</td>";
echo "<td style='width: 400px; height: 400px; overflow-hidden;'><img
src=images/{$row['Image']}</td>";
echo "<td>{$row['UnitPrice']}</td>";
echo "</tr>";
}
echo"</table>"
?>
The images are stored in an images folder, within the htdocs folder on my xampp installation. I've double checked the path. The data type us VARCHAR and i've tried with and without the extention.
Any Suggestions?
Thanks
Will
The array $carry_over returns a really long list of entries, too long for my printout page. I would love to make it in such a way that after 4 entries, it breaks and goes to the next line and breaks at the next four entries until all entries are in.
How can i do this?
Thanks
echo "<table class=\"altrowstable\" bgcolor = gold >\n";
$count = 0;
echo "<tr align= \"center\">\n";
$carry_over = array();
$score_count = mysql_numrows($query8);
echo "<td>"."Failed: ";
if($score_count !== 0){
while ($row8 = mysql_fetch_assoc($query8)) {
echo "<th>".$row8['course_code']."</th>";
if ( $count == 7 ){
echo "</tr>\n";
echo "</table>";
}
}
}
Update : Now only the first 7 entries are covered inside the table tags, the subsequent ones are outside the table tags. How can i put them in the tabke tags?
Thanks
Not sure if this is what you are looking for, but you could try:
$count = 0;
echo "<tr>";
while ($row8 = mysql_fetch_assoc($query8)) {
echo "<th>" . $row8['course_code'] . "</th>";
$count++;
if(count == 4) {
echo "</tr><tr>";
$count = 0;
}
}
echo "</tr>";
It looks like you're using a table for list data. Instead of using a <table>, you should be using a <ul> with <li> for each element. This can then be styled so that each li has display: inline-block;, for example.
I cannot tell what the table looks like from the code above, but you can put an iterator in to count to four then wrap. You also may want to check out Wordwrap
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
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.