PHP-conditional formatting [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
The following code prints nicely the table
<?php
$lines = file('graphdata/GapsVsOthersForDimsOthersJson.csv');
foreach ($lines as $lineNum => $line) {
if($lineNum == 0) {
continue;
}
print " <tr id=\"tr" . $lineNum . "\">";
$tokens = str_getcsv($line);
/*print "<td style=\"width: 200px;\">" . trim($tokens[0]) . "</td>";*/
print "<td style=\"width: 300px;\">" . trim($tokens[0]) ."</td>";
print "<td style=\"width: 100px;\">" . trim($tokens[1]) . "</td>";
print "<td style=\"width: 100px;\">" . trim($tokens[2]) . "</td>";
print "<td style=\"width: 100px;\">" . trim($tokens[3]) . "</td>";
print "<td style=\"width: 100px;\">" . trim($tokens[4]) . "</td>";
print "<td style=\"width: 100px;\">" . trim($tokens[5]) . "</td>";
print "<td style=\"width: 100px;\">" . trim($tokens[6]) . "</td>";
print "</script>\n";
}
?>
I want to format the token(4) cell based on values of token(2) and token(3):
Red background if the value is smaller than token(2)-1.1*token(3).
Green background if the value is greater than token(2)+1.1*token(3)
Please advise

You could just set up some conditional statements. First of all, I recommend using a stylesheet instead of inline styling; it's much cleaner, so you could simply name some classes for different conditions.
for example, you could do something like this
if ($tokens[4] < ($tokens[2]-(1.1*$tokens[3])) {
echo '<td class="tokenred">' . trim($tokens[4]) . '</td>';
}
else if ($tokens[4] > ($tokens[2]+(1.1*$tokens[3])){
echo '<td class="tokengreen">' . trim($tokens[4]) . '</td>';
}
else {
echo '<td>' . trim($tokens[4]) . '</td>';
}
and then your stylesheet would have something like
.tokenred {
background-color: #FF0000;
}
.tokengreen {
background-color: #00FF00;
}
alternately you could use inline styles if you choose

Related

PHP-Formatting table header

This code reads a CSV file for creating a table. It works fine.
How do I make the 1st row to be formatted in header style (from the table CSS)?
<table id="myTable" class="tablesorter animated fadeInDown"> <!-- cellspacing='0' is important, must stay -->
<tbody>
<?php
$lines = file('graphdata/IndicForTableVsOthers.csv');
foreach ($lines as $lineNum => $line) {
if($lineNum == 0) {
print " <tr id=\"tr" . $lineNum . "\">";
}
print " <tr id=\"tr" . $lineNum . "\">";
$tokens = str_getcsv($line);
print "<td style=\"width: 300px;\">" . trim($tokens[0]) . "</td>";
print "<td style=\"width: 100px;\">" . trim($tokens[1]) . "</td>";
print "<td style=\"width: 100px;\">" . trim($tokens[2]) . "</td>";
print "<td style=\"width: 100px;\">" . trim($tokens[3]) . "</td>";
print "<td style=\"width: 100px;\">" . trim($tokens[4]) . "</td>";
print "</script>\n";
}
?>
</tbody>
This should work, I also changed some other parts who I think would have generated wrong marup.
<table id="myTable" class="tablesorter animated fadeInDown">
<?php
$lines = file('graphdata/IndicForTableVsOthers.csv');
foreach ($lines as $lineNum => $line) {
$cellType = ($lineNum == 0 ? "th" : "td");
$tokens = str_getcsv($line);
if ($lineNum == 0) echo "<thead>";
if ($lineNum == 1) echo "<tbody>";
echo "<tr id=\"tr" . $lineNum . "\">";
echo "<" . $cellType . " style=\"width: 300px;\">" . trim($tokens[0]) . "</" . $cellType . ">";
echo "<" . $cellType . " style=\"width: 100px;\">" . trim($tokens[1]) . "</" . $cellType . ">";
echo "<" . $cellType . " style=\"width: 100px;\">" . trim($tokens[2]) . "</" . $cellType . ">";
echo "<" . $cellType . " style=\"width: 100px;\">" . trim($tokens[3]) . "</" . $cellType . ">";
echo "<" . $cellType . " style=\"width: 100px;\">" . trim($tokens[4]) . "</" . $cellType . ">";
echo "</tr>";
if ($lineNum == 0) echo "</thead>";
}
if (count($lines) > 1) echo "</tbody>";
?>
</table>
If the first row is printed but it looks light and you want it Bold and Pretty so the user knows it's the header, add a class for convenience.
table.cs-contents tr:first-child td {
font-weight: bold;
text-align: center;
border-bottom: 2px solid #eaeaea;
}

url from mysql db with <A> tag in table with php

I couldn't find a question with exactly what I want to know and since I already have the majority of code written and just need this little part to work it's hard to follow someone elses question.
this is the part that doesn't work:
echo "<TD width='220px' style='padding:5px;'>" . "<a href='>" . $row['horselink'] ."'><strong>" . $row['name'] . "</strong></a></td>";
I want the a href to collect the data from the column horselink so it links the name. I have tried several way but I just can't get it work. Anyone have a good tip or idea how the write the link/url part correctly since the rest of the table works just fine.
complete code looks like this:
<?php
include("conn.php"); //Databas koppling
mysql_set_charset("utf8");
$result = mysql_query("SELECT name, horselink, born, gender, discipline, breed, sire, dam FROM Horse WHERE horsetype = 'young' ORDER by born");
//Hämtar all information från kurser
echo "<table id='trhover' class='tableclassen' cellpadding='3' cellspacing='1'>";
echo "<TR class='trhead' style='text-transform:uppercase; background-color: #6e3f47;'>";
echo "<TD width='220px' style='padding:5px;'><strong>Name<strong></TD>";
echo "<TD width='60px' style='padding:5px;'><strong>Dob</strong></TD>";
echo "<TD width='90px' style='padding:5px;'><strong>Gender</strong></TD>";
echo "<TD width='110px' style='padding:5px;'><strong>Discipline</strong></TD>";
echo "<TD width='120px' style='padding:5px;'><strong>Breed</strong></TD>";
echo "<TD width='160px' style='padding:5px;'><strong>Sire</strong></TD>";
echo "<TD width='160px' style='padding:5px;'><strong>Dam</strong></TD>";
echo "</TR>";
while($row = mysql_fetch_array($result)) // Skriver ut all information i en tabell
{
echo "<TR>";
echo "<TD width='220px' style='padding:5px;'>" . "<a href='>" . $row['horselink'] ."'><strong>" . $row['name'] . "</strong></a></td>";
echo "<TD width='60px' style='padding:5px;'>" . $row['born'] . "</td>";
echo "<TD width='90px' style='padding:5px;'>" . $row['gender'] . "</td>";
echo "<TD width='110px' style='padding:5px;'>" . $row['discipline'] . "</td>";
echo "<TD width='120px' style='padding:5px;'>" . $row['breed'] . "</td>";
echo "<TD width='160px' style='padding:5px;'>" . $row['sire'] . "</td>";
echo "<TD width='160px' style='padding:5px;'>" . $row['dam'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
This line:
echo /* ... */ "<a href='>" . $row['horselink'] ."'><strong>" /* ... */;
has an extra > in it. It should be:
echo /* ... */ "<a href='" . $row['horselink'] ."'><strong>" /* ... */;
And:
You should not use the deprecated mysql functions. Use PDO or mysqli.
You should escape your output with htmlspecialchars().
It is considered good practice to markup your table headings with <THEAD> and <TH> elements.

How to place database results in ScrollBox

I am trying to place my database results into a ScrollBox but im having a difficult time,
I was checking online and it was saying that i need to use the scrollbox:auto function.
But i tried add it too the CSS regarding the table for which the database results go in
but it did not work and just displayed the results all on the page.
Any guidance would be appreciated
CSS
.results
{
bottom: -10px;
position: relative;
top: 90px;
scrollbox: auto;
}
Database Results
echo "<table class='results' border='1'>";
while ($row = mysqli_fetch_array($result))
{
echo "<tr >";
echo "<td>" . '<img style=height:45px; width:45px src="' . $row['Image'] . '"/>' . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . "£" . $row['Cost'] . "<br>" . '<a href='.$row['Link'].'>' . '</a>' . "</td>";
echo "</tr>";
}
use overflow: auto; Or overflow:scroll instead of scrollbox: auto;
make sure to give the scrolling-div a fixed height.

HTML through PHP not taking a CSS rule

Is there any reason that the following code and syle wouldn't work? The only problem is that I can't get the text to change color, it stays black, which would be fine but the background color is black. I know it's there because I can highlight the text with the cursor and it's there in the table. Here's my code fragment.
This is from the index.php page.
while($row=mysqli_fetch_array($result))
{
echo "<tr id= "Data'" class='"Data'">";
echo "<td id= " cell ">" . $row['Name'] . "</td>";
echo "<td id= " cell ">" . $row['Subject'] . "</td>";
echo "<td id= " cell ">" . $row['Date'] . "</td>";
echo "<td id= " cell ">" . $row['Source'] . "</td>";
echo "<td id= " cell ">" . $row['Comment'] . "</td>";
echo "</tr>";
}
here's the css rule that SHOULD be applied.....but isn't.
#Cell{
border-style: solid;
border-color: black;
border-width: 1px;
color: white;
<td id= " cell "> is not equivalent to <td id="cell"> because HTML does not recursively evaluate itself. So the former tag is invalid gobbledegook without an id. You also cannot give the same id to more than one element.
Why your are using " cell "
Here is correct code
while($row=mysqli_fetch_array($result))
{
echo "<tr id=\"Data\" class=\"Data\">";
echo "<td id=\"cell\">" . $row['Name'] . "</td>";
echo "<td id=\"cell\">" . $row['Subject'] . "</td>";
echo "<td id=\"cell\">" . $row['Date'] . "</td>";
echo "<td id=\"cell\">" . $row['Source'] . "</td>";
echo "<td id=\"cell\">" . $row['Comment'] . "</td>";
echo "</tr>";
}

PHP How to echo a different image every 10th row of my results

I have a simple search engine which echo's out results in a table with CSS formatting, this is working fine, however, i would now like to echo a different image after every 10 results.
thankyou to any one who can help me as I have been stuck on this for hours!
this is my echo output code:
echo "<table width='50%' style='border-bottom:1px solid #000000;'";
echo "<tr>";
echo "<td>";
echo "<div id='page-wrap'>";
echo "<div class='discounted-item freeshipping'>";
echo "<a href='./img/users/" . $row['category'] . "/" . $row['username'] . "/" . $row['filename'] . "' rel='lightbox'><img src=\"./img/users/" . $row['category'] . "/" . $row['username'] . "/" . $row['filename'] . "\" alt=\"\" width='20%' height='98%' /></a>";
echo "<div class='reasonbar'><div class='prod-title' style='width: 70%;'>" .$row['fname'] . "</div><div class='reason' style='width: 29%;'><b>". $row['firstname'] . " " . $row['surname'] ."</b></div></div>";
echo "<div class='reasonbar'><div class='prod-title1' style='width: 70%;'>" . $row['lname'] . "</div><div class='reason1' style='width: 29%;'>Category: ". $row['category'] . "</div></div>";
echo "<div class='reasonbar'><div class='prod-title2' style='width: 70%;'>Contact:" . $row['contact'] . "</div><div class='reason2' style='width: 29%;'>Price: £". $row['price'] . "</div></div>";
echo "</td>";
echo "</tr>";
echo "</td>";
echo "</tr>";
echo "</table>";
}
}
else
echo "No results found for \"<b>$search</b>\"";
You can use modolus operator to determine that...
if ( ! ($i % 10)) {
// Alternate.
}
If you don't have a counter variable at your disposal, just declare one outside of your loop and set it to 0 and increment it for each iteration.

Categories