PHP-Formatting table header - php

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;
}

Related

PHP- Insert an Icon on a table column header

I'm using this code for creating a table based on a CSV file and it works fine;
<?php
$lines = file('graphdata/GapsVsOthersForDimsOthersJson.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>";
?>
I'm interested in inserting a button icon next the the last column header using:
<button id="popupWindowDimensions" type="button" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-info-sign"></span>
</button>
Any idea?
You can use the below format but this will create a empty last column
So Please can you clearly give how you need
<?php
$lines = file('graphdata/GapsVsOthersForDimsOthersJson.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 . ">";
if ($lineNum == 0)
{
echo "<" . $cellType . ' style=\"width: 100px;\">';
echo '<button id="popupWindowDimensions" type="button" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-info-sign"></span>
</button>';
echo '</' . $cellType . '>';
}
echo "</tr>";
if ($lineNum == 0) echo "</thead>";
}
if (count($lines) > 1) echo "</tbody>";
?>

PHP-conditional formatting [closed]

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

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>";
}

Sort sql query & ID reset and start in numerical order every time I make a query or sort from <th> tag

I have a database called forms1 and a table named demo
The fields in the table are ID, Autore, Titolo, cit
I wish I had a first line that gives me the ability to sort alphanumeric values ​​that are retrieved by the query, such as
How can i modify my code?
This is cerca2.php:
<style>
br {margin-bottom:-10px;}
</style>
<form action="cerca2.php" method="post">
<b>Nome</b> <input type="text" name="Nome">
<b>Numero </b><input type="text" name="Numero">
<b>city </b><input type="text" name="city">
<input type="Submit">
</form>
<style>
tr:nth-of-type(odd) { background-color: AZURE; }
tr:nth-of-type(even) { background-color: CYAN; }
</style>
<style>
tr:hover{background-color<img src="images/smilies/biggrin.gif" border="0" alt="">EEPSKYBLUE;}
</style>
<?php
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='auto' bgcolor=”#7FFFD4″> <i>ID<i/></td>";
echo "<td width='auto' > <i>Nome<i/></td>";
echo "<td width='auto' ></td>";
echo "<td ></td>";
echo "</tr>";
define('DB_NAME', 'forms1');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can't use ' . DB_NAME . ': ' . mysql_error());
}
$Nome = str_replace(' ', '%', $_POST['Nome']);
$Numero = str_replace(' ', '%', $_POST['Numero']);
$city = str_replace(' ', '%', $_POST['city']);
$arNome = str_split($Nome);
$arNumero = str_split($Numero);
$arcity = str_split($city);
$Nome='';
foreach ($arNome as $value)
{
if ($value=='%') continue;
$Nome.=$value.'%';
}
$Numero='';
foreach ($arNumero as $value)
{
if ($value=='%') continue;
$Numero.=$value.'%';
}
$city='';
foreach ($arcity as $value)
{
if ($value=='%') continue;
$city.=$value.'%';
}
$sql = mysql_query("SELECT * FROM demo WHERE Autore LIKE '%$Nome%' AND Titolo LIKE '%$Numero%' AND cit LIKE '%$city%' ORDER BY Autore") or die(mysql_error());
while($row=mysql_fetch_array($sql)){
echo "<tr>";
echo "<td width='auto' bgcolor=”#FF0000 ″>" . " ". "<b>" . $row[0] . " ". "<b/>". "</td>";
echo "<td width='auto'>" . " " . $row[1] . " " . "</td>";
echo "<td width='auto'>". "</td>";
echo "<td width='auto'>" . " ". "<i>" . $row[2] . "<i/>". " " . "</td>";
echo "<td width='auto'>" . " ". "<i>" . $row[3] . "<i/>". " " . "</td>";
echo "</tr>";
}
mysql_close();
?>
I would click on Nome and other th (except ID) to reorder the values ​​alphanumeric AZ or ZA viceversa when i click. At the moment this is my result page (look code above) when i call a query:
I would, too - but I do not know how to do - that the ID value always start from 1 and continue in numerical order according to the number of x values ​​retrieved every time I make a query or sort from th tag. In my case, instead, is always dependent on the value it represents.
You see it from the picture that the numbers appear in random order.
Here, I do not want this.
Try this code-part:
$i = 0; while($row=mysql_fetch_array($sql)){
$i++;
echo "<tr>";
echo "<td width='auto' bgcolor=”#FF0000 ″>" . " ". "<b>" . $i . " ". "<b/>". "</td>";
echo "<td width='auto'>" . " " . $row[1] . " " . "</td>";
echo "<td width='auto'>". "</td>";
echo "<td width='auto'>" . " ". "<i>" . $row[2] . "<i/>". " " . "</td>";
echo "<td width='auto'>" . " ". "<i>" . $row[3] . "<i/>". " " . "</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