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
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'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";
The code I am posting below, iterates through a result set and prints elements (paths to images on database) so that images display in order. Only 3 images per row. I want to be able to have the same concept (i.e. 3 images or cells per row) but I want to have another div underneath it to have the word delete photo.
$n = 3;
echo "<table style='margin-right: 100px;'>";
echo "<tr>";
for($i=1; $i<=count($gallery);$i++){
$temp = array();
$temp = $gallery[$i-1];
echo "<td><div id='gallery_pic'><img id='single_pic' src='". $temp->path . "' /></div></td>";
if($i % $n ==0){
echo "</tr><tr>";
}
}
echo '</tr>';
echo '</table>';
echo "</table>";
The idea is that owner of profile should be able to delete photo by clicking it. I will handle that I am just not sure how to handle printing the table with same order by adding another row per row with delete word.
Simple. Add a new div right after div#gallery_pic and give it a width of 100%, this will force it to be below the image...
echo "<table style='margin-right: 100px;'>";
echo "<tr>";
for($i=1; $i<=count($gallery);$i++){
$temp = array();
$temp = $gallery[$i-1];
echo "<td><div id='gallery_pic'><img id='single_pic' src='". $temp->path . "' /></div><div class='delete_wrap'><a href='?delete_id=" . {image_id} . " style='width:100%'>Delete</a></div></td>";
if($i % $n ==0){
echo "</tr><tr>";
}
}
echo '</tr>';
echo '</table>';
echo "</table>";
I have a database of images which I want to output as a gallery ...
I want to display each image in its own cell <td>image1</td> and limit the number of cells to 7 per row.
<tr>
<td>image1</td><td>image2</td><td>image3</td><td>image4</td><td>image5</td><td>image6</td><td>image7</td>
</tr>
Then a new row would be created and continue to output all the images.
<tr>
<td>image8</td>
and so on ..
I know how to do a query, but I am lost as to how to assemble the rows into the format I am looking for.
Can anyone please help me it would be greatly appreciated. Thanks.
First run your query, then get the mysql_fetch_assoc in a variable. Then echo your beginning tags for the table and tr. Then create a foreach loop of the query assoc, as another variable such as row, and echo $row['image'] in a td. This will add a new td with the image for every entry in the database. Then echo your closing tags for the tr and table.
$query = mysql_query("SELECT image FROM table_name");
$query_a = mysql_fetch_assoc($query);
echo "<table><tr>"
foreach ($query_a as $row)) {
echo "
<td>" . $row['image'] . "</td>
";
}
echo "</tr></table>";
Not tested, but try something like this
<table>
<?php
// make sure there is at least 1 row first
$cnt = 0;
$while($row = mysql_fetch_assoc($query))
{
if(++$cnt == 1) {
echo '<tr>';
} elseif($cnt % 7 == 0) {
echo '</tr><tr>';
}
// show images in <td>'s
echo "<td>" . $row['image']. "</td>";
}
echo '</tr>';
?>
</table>
Keep it simple:
Query the database to get the desired information, loop through the results, construct a new row come each new loop.
$sql = "Select * from table";
mysql_query($sql) or die(mysql_error());
if(mysql_num_rows !=0)
{
// Found at least one record, create table
echo "<table>";
echo "<tr>";
while($row = mysql_fetch_assoc($sql)
{
$cell_count = 0;
// Create a new row for each record found
echo "<td>" . $row['image_column']. "</td>";
$cell_count++;
if($cell_count == 7)
{
echo "</tr>";
echo "<tr>";
$cell_count = 0;
}
}
// Close the table
echo "</tr>";
echo "</table>";
}
The code below echoes out an HTML table, populated with values from a MySQL database. The CSS changes when a field called "topten" equals 1.
When "topten" equals 1, I would like to have a rectangular background 100 pixels high, 800 pixels wide, and color #A6FFFF;. When "topten" does not equal 1, I would like there to be no background. I would imagine that I could do this by applying some CSS where there is "backgroundtt" below. How can I do this?
$result = mysql_query($sqlStr);
$arr = array();
echo "<table class=\"samplesrec\">";
while ($row = mysql_fetch_array($result)) {
if($row["topten"] == 1) {
echo '<div class="backgroundtt"></div>';
echo '<tr class="class2a">';
echo '<td class="sitename1"></td>';
echo '</tr>';
echo '<tr class="class2b">';
echo '<td class="sitename2name"></td>';
echo '</tr>';
echo '<tr class="class2c">';
echo '<td class="sitename2"></td>';
echo '</tr>';
} else {
echo '<tr class="class3a">';
echo '<td class="sitename1"></td>';
echo '</tr>';
echo '<tr class="class3b">';
echo '<td class="sitename2name"></td>';
echo '</tr>';
echo '<tr class="class3c">';
echo '<td class="sitename2"></td>';
echo '</tr>';
}
}
echo "</table>";
It is not valid markup to have a div inside of a table that is not in an actual table cell. What you will want to do instead is apply a class to the three rows that are in the topten call so that section might look like this:
if($row["topten"] == 1) {
echo '<tr class="class2a coolBackgroundClass">';
echo '<td class="sitename1"></td>';
echo '</tr>';
echo '<tr class="class2b coolBackgroundClass">';
echo '<td class="sitename2name"></td>';
echo '</tr>';
echo '<tr class="class2c coolBackgroundClass">';
echo '<td class="sitename2"></td>';
echo '</tr>';
}
Then you can set the background color like so in css:
.coolBackgroundClass { background-color: #A6FFFF; }
So instead of trying to absolutely position a div behind those rows (which in its placement is invalid markup) you just set the background color of each of the three rows so it looks like on large rectangular box. Also if your rows grow in size the box will grow with them.
Edited: missed a semicolon in css markup
a div inbetween a table and a table row tag is invalid table syntax. this may be the cause of your problem.
The simplest way to do this would probably to write it inline, like so:
if($row["topten"]==1){
echo '<div style="position:absolute;width:800px;height:100px;background-color:#A6FFFF;z-index:-1;"></div>';
}
echo "<table class=\"samplesrec\">";
while ($row = mysql_fetch_array($result)) {
if($row["topten"] == 1) {
// move div above table to make it proper markup.
echo '<tr class="class2a">';
This however assumes that the container is positioned relative, or that your table is positioned at 0,0 on your page with no margins.
If I were you I'd add a class name to the <td>s that you'd like to have a background on instead of adding an element though.