I'm quit new with mysql, and php but everytime i'm learning.
For a friend of my I'm building a website with a small pricing-list on it.
The prices are from a database, but I now want to create a empty line bewtween the 2nd and the 3th line, but I don't know how to do that.
I use the following code to receive the information from the database;
<table id="nails" class="prijzen">
<thead>
<tr>
<th>Nails</th>
<th>Price first visite</th>
<th>Regular price</th>
</tr>
</thead>
<?php
// while ($row = $queryNails->fetch_assoc()) {
$i = 0;
while ($row = $regularNails->fetch_assoc())
{
if($i%3 == 0)
{
echo "<tr class='even'>";
echo "<td>" . $row['name'] ."</td>";
echo "<td>" . $row['prijs'] ."</td>";
echo "<td>" . $row['actie_prijs'] ."</td>";
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td>" . $row['name'] ."</td>";
echo "<td>" . $row['prijs'] ."</td>";
echo "<td>" . $row['actie_prijs'] ."</td>";
echo "</tr>";
}
$i++;
?>
<tbody>
<?php
}
?>
</tbody>
</table>
<br>
I now have every odd-line a styling, but that is not something I like. I would like to create a sort of group of the output.
Hope someone can help me with that. If more information is needed, please let me know.
Please stop suggestion to enter a <br /> in between lines. That's not gonna work at all, tables-101: Everything not placed in a td will be placed in before the table. So all you get is a bunch of empty lines before the whole table.
The code is allready there (if you want it before every 3rd row:
if($i%3 == 0)
{
// The code right here will only run when $i / 3 is exactly 0 (3,6,9,etc)
echo '<tr><td colspan="3">Blank line</td></tr>';
echo "<tr class='even'>";
echo "<td>" . $row['name'] ."</td>";
echo "<td>" . $row['prijs'] ."</td>";
echo "<td>" . $row['actie_prijs'] ."</td>";
echo "</tr>";
}
If you only want it once before the 3rd row, make this the first code in your while:
if($i==3){ echo '<tr><td colspan="3">Blank line</td></tr>'; }
The % is modulus. It gives you how much is left after getting the max amount of whole divisions:
echo 1 %3 = 1;
echo 2 %3 = 2;
echo 3 %3 = 0; // exactly dividable by 3
echo 4 %3 = 1;
echo 5 %3 = 2;
echo 6 %3 = 0; // exactly dividable by 3
Related
i am trying this from 5hrs don't know i am going wrong.My table echos results but not echo total for the same.
<table width="102%" border="0">
<tr>
<th><b>Amount</b></th>
<th ><strong>County</strong></th>
</tr><?php
(this fetches data ...$row = mysqli_fetch_array($res) or die(mysqli_error($db)))
echo "<tr>";
echo "<td>" . $price . "</td>";
echo "<td>" . $rui['county'] . "</td>";
echo "</tr>";
(I get these entreis correct )
}
}
(Below table is not echoed)
echo "<tr>";
echo "<td> .<b>Total </b>. </td>";
echo "<td>" .$x. "</td>";
echo "<td></td>";
echo "</tr>";
?></table>
Remove the } } after the closing tag of the tr.
if you can't close the whole PHP block then you probably did something wrong in your syntax.
I don't know if you have more code or not, if you do - post it so we will get more accurate view of your code.
<table width="102%" border="0">
<tr>
<th><b>Amount</b></th>
<th ><strong>County</strong></th>
</tr>
<?php
(this fetches data ...$row = mysqli_fetch_array($res) or die(mysqli_error($db)))
echo "<tr>";
echo "<td>" . $price . "</td>";
echo "<td>" . $rui['county'] . "</td>";
echo "</tr>";
(I get these entreis correct )
(Below table is not echoed)
echo "<tr>";
echo "<td> .<b>Total </b>. </td>";
echo "<td>" .$x. "</td>";
echo "<td></td>";
echo "</tr>";
}
?>
</table>
You can remove the wrong bracket, OR check that the two bracket are opened when fetching data
<table width="102%" border="0">
<tr>
<th><b>Amount</b></th>
<th ><strong>County</strong></th>
</tr><?php
//(this fetches data ...$row = mysqli_fetch_array($res) or die(mysqli_error($db)))
echo "<tr>";
echo "<td>" . $price . "</td>";
echo "<td>" . $rui['county'] . "</td>";
echo "</tr>";
//(I get these entreis correct )
//(Below table is not echoed)
echo "<tr>";
echo "<td> .<b>Total </b>. </td>";
echo "<td>" .$x. "</td>";
echo "<td></td>";
echo "</tr>";
?>
</table>
I am a beginner to PHP. I am looking to highlight or color the values of first and last row in a table fetched using PHP and Mysql.
Below is the code.
<?php
$result_trend = mysql_query("select Week, New_Tickets, Closed_Tickets,
Backlogged, Resolution_Rate, Transactions from table");
while($row = mysql_fetch_array($result_trend))
{
echo "<tr>";
echo "<td>" . $row['Week'] . "</td>";
echo "<td>" . $row['New_Tickets'] . "</td>";
echo "<td>" . $row['Closed_Tickets'] . "</td>";
echo "<td>" . $row['Backlogged'] . "</td>";
echo "<td>" . $row['Resolution_Rate'] . "</td>";
echo "<td>" . $row['Transactions'] . "</td>";
}
echo "</tr>";
?>
I am not really sure how to proceed further. Any suggestion is much appreciated. Thanks in advance.
You can define a CSS style for the first and last rows as below:
.my-table tbody tr:first-child {
background-color: yellow;
}
<table class="my-table">
<thead>
<tr>
<td>Col 1</td>
<td>Col 2</td>
</tr>
</thead>
<tbody>
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>row</td>
</tr>
<tr>
<td>Third</td>
<td>row</td>
</tr>
</tbody>
</table>
You can simply obtain number of results and then count iterations. And if this is 1. row or last, add the style you need or css class that you'll style in your css file. Like this:
<?php
$result_trend = mysql_query("select Week, New_Tickets, Closed_Tickets,
Backlogged, Resolution_Rate, Transactions from table");
$rows_count = mysql_num_rows($result_trend);
$i = 0;
while($row = mysql_fetch_array($result_trend))
{
$i++;
$style = "";
if($i == 1 || $i == $rows_count) {
// $style .= "bgcolor=\"#FF0000\"" //for inline style, bad practise
$style .= "class=\"colorful\""
}
echo "<tr " . $style . ">";
echo "<td>" . $row['Week'] . "</td>";
echo "<td>" . $row['New_Tickets'] . "</td>";
echo "<td>" . $row['Closed_Tickets'] . "</td>";
echo "<td>" . $row['Backlogged'] . "</td>";
echo "<td>" . $row['Resolution_Rate'] . "</td>";
echo "<td>" . $row['Transactions'] . "</td>";
}
echo "</tr>";
?>
I am trying to make a table in PHP.
All things are fine but all the rows are printed in same line.
How to print new line after each row in table? But in html there is no need to write extra code for new line why it is showing not a new line with PHP?
Here is that part of code:
<div class="contest-table" id="contest-table">
<table class="contest-details" id="contest-details">
<tr>
<th>CODE</th>
<th>NAME</th>
<th>START</th>
<th>END</th>
</tr>
<?php
//Show contest detials -> Contest Code|Contest Name |Start Date | End Date
$con=mysqli_connect("localhost","root","chandan","judge");
$result=mysqli_query($con,"SELECT * FROM judge_contest ");
echo "<tr>";
while($row = mysqli_fetch_array($result))
{
$contest_code=$row['contest_code'];
$contest_name=$row['contest_name'];
$contest_start_date=$row['start_date'];
$contest_end_date=$row['end_date'];
echo "<td>";
echo " $contest_code ";
echo "</td>";
echo "<td>";
echo " $contest_name ";
echo "</td>";
echo "<td>";
echo $contest_start_date;
echo "</td>";
echo "<td>";
echo $contest_end_date;
echo "</td>";
}
echo "</tr>";
?>
</table>
</div>
The problem is obvious, because you have put the statement echo "<tr>" and echo "</tr>" outside the while loop. You should put these two statement into the while loop.
echo '<tr>'
and
echo '</tr>'
should be inside the wile loop
I have three values that can be displayed from a table/column in mysql, RED, GREEN, YELLOW for the field "ProspectStatus"
Is there anyway I can make a cell change its background color based on the value?
e.g.
echo "<td>" . $row['ProspectStatus'] . "</td>";
PHP Code:
$result = mysql_query("SELECT * FROM customerdetails");
//List the Columns for the Report
echo "<table border='1'>
<tr>
<th>CustomerID</th>
<th>Customer Name</th>
<th>Prospect Status</th>
<th>Address</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['CustomerID'] . "</td>";
echo "<td>" . $row['CustomerName'] . "</td>";
echo "<td>" . $row['ProspectStatus'] . "</td>"; //this is the field I want to show either RED, GREEN or YELLOW
echo "<td>" . $row['Address'] . "</td>";
echo "</tr>";
}
echo "</table>";
You can do this with this:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['CustomerID'] . "</td>";
echo "<td>" . $row['CustomerName'] . "</td>";
if($row['ProspectStatus']=='[val1]') // [val1] can be 'approved'
echo "<td style='background-color: #00FF00;'>".$row['ProspectStatus']."</td>";
else if($row['ProspectStatus']=='[val2]')// [val2]can be 'rejected'
echo "<td style='background-color: #FF0000;'>".$row['ProspectStatus']."</td>";
else if($row['ProspectStatus']=='[val3]') //[val3] can be 'on hold'
echo "<td style='background-color: #FFFF00;'>".$row['ProspectStatus']."</td>";
echo "<td>" . $row['Address'] . "</td>";
echo "</tr>";
}
echo "</table>";
The value of the status may depend on the color. I assume that val1, val2 and val3 are the values of the 3 colors.
First, use CSS for this, not messy in-line style definitions
The most maintainable option would probably separate the color codes themselves from the view logic, so in CSS, create some new classes:
td.done { background-color: green; }
td.working { background-color: yellow; }
td.stopped { background-color: red; }
Then, in your loop just do this (you don't need the echo statements, anything outside of <? and ?> tags will be parsed as HTML rather than PHP):
<?php while($row = mysql_fetch_array($result)): ?>
<tr>
....
<td class='<?= $row['ProspectStatus']; ?>'><?= $row['ProspectStatus']; ?></td>
....
</tr>
<?php endwhile; ?>
This way, if ProspectStatus is 'done', the cell will have a green background, if it's 'working', it will have a yellow background and if it's stopped it will have a red background.
If ProspectStatus is a range, or otherwise not this simple
Let's say ProspectStatus doesn't have done, working and stopped or similar simple values, and instead it's a range, for this you can use a simple nested ternary operator to get the color you want instead of <tr class='<?= $row['ProspectStatus']; ?>'>:
<tr class='<?php echo ($row['ProspectStatus'] >= 80) ? "done" : (($row['ProspectStatus'] >= 40) ? "working" : "stopped"); ?>'>
With this one liner, if ProspectStatus is greater than 80, the field will be green, if it's between 40 and 80 it will be yellow and if it's below 40 it will be red.
Note: mysql_ functions are DEPRECATED
By using the mysql_ features, you run the risk of your program not working in future versions of PHP as those functions have been formally deprecated as of 5.5.
The new recommended way to perform your query is this:
$mysqli = new mysqli("username","password","hostname","password");
$result = $mysqli->query("SELECT * FROM customerdetails");
while($row = $result->fetch_assoc())...
Hope this helps.
Create a class of the color you want for that cell and then make an if statement that will check the value. Depending on the value, echo that class.
yes you can add it to you Table data tag like below
The following code will work:
while($row = mysql_fetch_array($result))
{
echo "<tr style='background-color: ". $row['ProspectStatus'] ."'>";
echo "<td>" . $row['CustomerID'] . "</td>";
echo "<td>" . $row['CustomerName'] . "</td>";
echo "<td>" . $row['ProspectStatus'] . "</td>"; //this is the field I want to show either RED, GREEN or YELLOW
echo "<td>" . $row['Address'] . "</td>";
echo "</tr>";
}
echo "<tr style='background-color: ". $row['ProspectStatus'] ."'>";
or make an if statement checking what color || then displaying the echo
That should work.
Since you said you want to change the color of only one particular cell, this should do the trick:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['CustomerID'] . "</td>";
echo "<td>" . $row['CustomerName'] . "</td>";
echo "<td style='background-color: ". $row['ProspectStatus'] ."'>" . $row['ProspectStatus'] . "</td>";
echo "<td>" . $row['Address'] . "</td>";
echo "</tr>";
}
I have a MySql table like this
client_id client_name inrement
123 Jhon 5
555 Smith 10
666 Aron 15
777 Herath 2
888 Jaya 1
999 RRR 20
And I 'm using the following code to get this table information to a HTML table
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("offlinesurv", $con);
$result = mysql_query("SELECT * FROM increments ");
echo "<table border='3' BORDERCOLOR=BLUE align='center' >
<tr>
<th>Client-id</th>
<th>Client-Name</th>
<th>Incre</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['client_id'] ."</td>";
echo "<td>" . $row['client_name'] ."</td>";
echo "<td>" . $row['inrement'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
My out out is like below
client_id client_name inrement
123 Jhon 5
555 Smith 10
666 Aron 15
777 Herath 2
888 Jaya 1
999 RRR 20
My problem is that I need to color in red the rows in the Output report(HTML table) where increment is greater than 10 only (increment>10). Could someone please help me on doing this. Can I do it from PHP? AJAX? if so please help me on this becoz I'm new in to both
Try this:
while($row = mysql_fetch_array($result))
{
$class = $row['inrement']>10?'red':'normal';
echo "<tr>";
echo "<td class=\"$class\">" . $row['client_id'] ."</td>";
echo "<td class=\"$class\">" . $row['client_name'] ."</td>";
echo "<td class=\"$class\">" . $row['inrement'] . "</td>";
echo "</tr>";
}
Then set your CSS to handle the two classes (you can name them whatever you like - I just called them red and normal for clarity):
<style>
.red{
color: #ff0000;
}
.normal{
color: #000000;
}
</style>
use if else block like this
while($row = mysql_fetch_array($result))
{
if($row['inrement'] > 10) {
echo "<tr style='color:red;'>";
echo "<td>" . $row['client_id'] ."</td>";
echo "<td>" . $row['client_name'] ."</td>";
echo "<td>" . $row['inrement'] . "</td>";
echo "</tr>";
}
else {
echo "<tr>";
echo "<td>" . $row['client_id'] ."</td>";
echo "<td>" . $row['client_name'] ."</td>";
echo "<td>" . $row['inrement'] . "</td>";
echo "</tr>";
}
}
<?php
echo "<table border='3' BORDERCOLOR=BLUE align='center' >
<tr>
<th>Client-id</th>
<th>Client-Name</th>
<th>Incre</th>
</tr>";
for($i=1;$i<=5;$i++){
if($i>1){
echo "<tr style='background:red'>";
}else{
echo "<tr>";
}
echo "<td>" . 1 ."</td>";
echo "<td>" . 2 ."</td>";
echo "<td>" . 3 . "</td>";
echo "</tr>";
}
echo "</table>";
?>
This is one example implementing your requirement.You can apply the logic in your code.