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.
Related
I want to display Array data in a table with 3 columns in a row using foreach loop with a condition.
Coding
$value[]='a';
$value[]='b';
$value[]='c';
$value[]='d';
$value[]='e';
echo '<table width=30% border=1>';
echo '<tr>';
$counter=1;
foreach($value as $key){
if($counter>=3){ // if there is more than 3 elements, go to next Row
if($counter%3==0){ // when the Array hit 3th,6th,9th,12th.... element
echo '</tr><tr><td>';
echo $key;
echo '</td>';
}else{
echo '<td>';
echo $key;
echo '</td>';
}
}else{
echo '<td>';
echo $key;
echo '</td>';
}
$counter++;
}
echo '</tr>';
echo '</table>';
I double check the coding and didn't manage to find the error.... my output is the bottom of the image. However, the correct one should be top of the image. Please take a look at the photo
Anyone know what's wrong with my coding?
Change modulas condition with
if($counter % 3 == 1)
and you will get what you desire
You have "if counter >= 3" so you are hitting that condition on your 3rd element, not AFTER your third element.
Change the initialization of your counter to $counter=0;
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\">";
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 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
I have a problem reading out multiple rows that get returned from an SQLite 3 database.
I tested my querys before and i know what the result should be. But i cant seem to figure out how to read these different lines/rows that get returned from this query on a PHP based webpage.
I searched the web, including stackoverflew but i can't seem to find an anwser to this question :/
Any help would be great!
Regards
P.S. this is how i fetch my the multiple rows on my php page:
$ID[$i] = $dbase->querySingle("SELECT rowid FROM rules WHERE parameter".$i." = '$Model'");
I have this in PHP:
$query = $db->query("SELECT * FROM files WHERE OBJECT = '".$id."'") ;
// begin table
echo '<table bgcolor="#FFFFFF">'."\n";
// give titles
echo '<tr>';
echo '<td bgcolor="#669999"><b><u>File</u></b></td>';
echo '<td bgcolor="#669999"><b><u>Night</u></b></td>';
echo '<td bgcolor="#669999"><b><u>Name</u></b></td>';
echo '<td bgcolor="#669999"><b><u>comment</u></b></td>';
echo '</tr>'."\n";
//
while($row = $query->fetchArray()) {
echo '<tr>';
echo '<td bgcolor="#CCCCCC">'.$row["uniquenumber"].'</td>';
echo '<td bgcolor="#CCCCCC">'.$row["NIGHT"].'</td>';
echo '<td bgcolor="#CCCCCC">'.$row["NAME"].'</td>';
echo '<td bgcolor="#CCCCCC">'.$row["COMMENTS"].'</td>';
echo '</tr>'."\n";
}
It works here.
Hope it can help you !