Im would like to style a table row based on the value in the first cell.
order_status has 3 options. 'Done' 'Optie' 'Def'
// output data of each row
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>" . $row['order_status'] . "</td>";
echo "<td>" . $row['planning'] . "</td>";
echo "<td>" . $row['order_name'] . "</td>";
echo "<td>" . $row['order_number'] . "</td>";
echo "<td>" . $row['order_pl'] . "</td>";
echo "<td>" . $row['order_date_in'] . "</td>";
echo "<td>" . $row['order_date_out'] . "</td>";
echo "<td>" . $row['order_location'] . "</td>";
echo "<td>" . $row['order_customer'] . "</td>";
echo "<td>" . $row['order_contact'] . "</td>";
echo "<td>" . $row['order_content'] . "</td>";
echo "</tr>";
}
echo "</table>";
If you add a class to the row you can easily do this by updating your CSS file.
PHP
Update your PHP to look like this:
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr class=\"order_status_{$row['order_status']}\">
<td> {$row['order_status']} </td>
<td> {$row['planning']} </td>
<td> {$row['order_name']} </td>
<td> {$row['order_number']} </td>
<td> {$row['order_pl']} </td>
<td> {$row['order_date_in']} </td>
<td> {$row['order_date_out']} </td>
<td> {$row['order_location']} </td>
<td> {$row['order_customer']} </td>
<td> {$row['order_contact']} </td>
<td> {$row['order_content']} </td>
</tr> ";
}
echo "</table>";
The following line...
class=\"order_status_{$row['order_status']}\"#
Will give class names in the format of order_status_*; for example:
order_Status_confirmed
order_Status_received
CSS
Then you can add something similar to the following to your CSS and control exactly how it looks.
.order_status_pending{
background: #faa;
}
.order_status_confirmed{
background: #afa;
}
Related
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 produce a spreadsheet like page - right now ive got the information from the database being displayed and an additional page allowing the user to add information.
I cannot understand how i would call the database to display a set row. I am not running an AI id so that is out the question. I tried to implement one after but it resulted in my code not working and giving me a blank white page.
Here is the code i am using to display the database:
<?php
$link = mysql_connect('', '', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
//connect to the database
mysql_select_db('');
//query the database
$query = mysql_query("SELECT * FROM ");
echo "<table border='1'>
<tr>
<td class='td'>Date</td>
<td class='td'>Region</td>
<td class='td'>Cameraman</td>
<td class='td'>Livestream?</td>
<td class='td'>Event Title</td>
<td class='td'>Lecturer</td>
<td class='td'>Time</td>
<td class='td'>Speaker</td>
<td class='td'>ICE Contact</td>
<td class='td'>Venue Address</td>
<td class='td'>Venue Contact</td>
<td class='td'>Additional Comments</td>
<td class='td'>On App?</td>
<td class='td'>Edit</td>
<td class='td'>Delete</td>
</tr>";
WHILE($rows = mysql_fetch_array($query)):
$rows = array_filter($rows);
if (!empty($rows)) {
echo "<tr>";
echo "<td>" . $rows['date'] . "</td>";
echo "<td>" . $rows['region'] . "</td>";
echo "<td>" . $rows['cameraman'] . "</td>";
echo "<td>" . $rows['livestream'] . "</td>";
echo "<td>" . $rows['eventitle'] . "</td>";
echo "<td>" . $rows['lecturer'] . "</td>";
echo "<td>" . $rows['time'] . "</td>";
echo "<td>" . $rows['speaker'] . "</td>";
echo "<td>" . $rows['icecontact'] . "</td>";
echo "<td>" . $rows['venueaddress'] . "</td>";
echo "<td>" . $rows['venuecontact'] . "</td>";
echo "<td>" . $rows['additionalcomments'] . "</td>";
echo "<td>" . $rows['onapp'] . "</td>";
echo "<td><img src=""></td>";
echo "<td><img src=""></td>";
echo "</tr>";
}
endwhile;
echo "</table>";
?>
Any help would be appreciated.
Thanks
There was a quotes mismatch in the third last and second last line of the if block where you used double quotes within the double quotes which causes for syntax error. Here outer double quotes is replace with single quotes. Update the if block like this:
if (!empty($rows)) {
echo "<tr>";
echo "<td>" . $rows['date'] . "</td>";
echo "<td>" . $rows['region'] . "</td>";
echo "<td>" . $rows['cameraman'] . "</td>";
echo "<td>" . $rows['livestream'] . "</td>";
echo "<td>" . $rows['eventitle'] . "</td>";
echo "<td>" . $rows['lecturer'] . "</td>";
echo "<td>" . $rows['time'] . "</td>";
echo "<td>" . $rows['speaker'] . "</td>";
echo "<td>" . $rows['icecontact'] . "</td>";
echo "<td>" . $rows['venueaddress'] . "</td>";
echo "<td>" . $rows['venuecontact'] . "</td>";
echo "<td>" . $rows['additionalcomments'] . "</td>";
echo "<td>" . $rows['onapp'] . "</td>";
echo '<td><img src=""></td>';
echo '<td><img src=""></td>';
echo "</tr>";
}
I'm pretty confused about the fact that I have this code:
while($row = mysql_fetch_array($result)) {
echo "<tr href='http://google.com'>";
echo "<td>" . $row['rowname'] . "</td>";
echo "<td>" . $row['rowname2'] . "</td>";
echo "<td>" . $row['rowname3'] . "</td>";
echo "<td>" . $row['rowname4'] . "</td>";
echo "</tr>";
}
and for some reason my link doesn't work. I've tried putting a tag around the tr but with no success. Somebody to have a clue?
Guess you are looking for link to entire row. This can be achieved by having onClick function on the row, instead of using href.
<tr onClick="location.href='target url'">
<td></td>
<td></td>
</tr>
while($row = mysql_fetch_array($result)) {
echo "<tr onclick='window.location.href = \"http://google.com\";'>";
echo "<td>" . $row['rowname'] . "</td>";
echo "<td>" . $row['rowname2'] . "</td>";
echo "<td>" . $row['rowname3'] . "</td>";
echo "<td>" . $row['rowname4'] . "</td>";
echo "</tr>";
}
If you want to make entire row clickable and work like an a tag you should use javascript for this:
echo "<tr onclick='location.href = \"http://google.com\"'>";
And then it would be nice to add CSS cursor pointer to clickable table cells:
tr td {
cursor: pointer;
}
a tr-tag has no href attribute, see here.
you can do something like:
<tr>
<td>
Google
</td>
....
</tr>
Another Solution would be adding javascript if the whole row should be clickable:
<tr onclick="window.location.href = google.com">
<td>
<a href="google.com"</a>
</td>
....
</tr>
I'm trying to update a mySQL table after a button click..The button click is not the problem but I wonder how I can get the klant_pk which is unique to update a certain record in mySQL. As you see I print out the mySql table at first. So is there anyone who know how I can get the according klant_pk after I click on a button in the table..
Thanks
$result = mysqli_query($con, "SELECT * FROM bestelling");
echo "Bestellingen";
echo "<table border='1' align='center'>
<tr>
<th>Bestelling_pk</th>
<th>Klant_pk</th>
<th>Product</th>
<th>Commentaar</th>
<th>Tijd</th>
<th> Voortgang </th>
<th> Status </th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['bestelling_pk'] . "</td>";
echo "<td>" . $row['klant_pk'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['opmerking'] . "</td>";
echo "<td>" . $row['tijd'] . "</td>";
echo "<td> <input type='button' value='In Wacht' onclick='return change(this);' />";
echo "<td>" . $row['status'] . "</td>";
echo "</tr>";
}
echo "</table>";
while ($row = mysqli_fetch_array($result)) {
...
echo "<td id='klank_pk_".$row['klant_pk']."'>" . $row['klant_pk'] . "</td>";
...
echo "<td> <input type='button' value='In Wacht' onclick='change(getElementById('klank_pk_".$row['klant_pk']."').value);' />";
...
}
I am new to PHP and I have problem. I have a table with check boxes. I need to add if statement inside the check box.
echo "<div class='table1'>
<table>
<tr>
<td></td>
<td>Module code</td>
<td>Module Title</td>
<td>Option</td>
</tr>";
echo "<form action='confirmsubmission.php' method='post'>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . "<input type='checkbox' name='check[]'
value='".$row['module_id']."' />" . "</td>";
echo "<td>" . $row['module_id'] . "</td>";
echo "<td>" . $row['module_title'] . "<a href=# content='".$row['description']."'
class='tooltip'><span title='Module Description'><img src='images/i.png'/></span>
</a>". "</td>";
echo "<td>" . $row['module_choice'] . "</td>";
echo "</tr>";
}
echo "</table></div>";
Below is a If statement I need to add after value='".$row['module_id']."'
if($row['module_choice']=='Mandatory'){ echo "checked=\"true\""; }
A bit of a mouthful, but try:
...
echo "<td>" . "<input type='checkbox' name='check[]' value='".$row['module_id']."'".($row['module_choice']=='Mandatory' ? 'checked="true"' : "")." />" . "</td>";
...
Even though I'll advise you to use a templating system (Smarty, etc.), you can do the following code:
echo "<div class='table1'>
<form action='confirmsubmission.php' method='post'>
<table>
<tr>
<td></td>
<td>Module code</td>
<td>Module Title</td>
<td>Option</td>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . "<input type='checkbox' name='check[]'
value='".$row['module_id']."'";
if($row['module_choice']=='Mandatory'){
echo " checked='checked' ";
}
echo "/>" . "</td>";
echo "<td>" . $row['module_id'] . "</td>";
echo "<td>" . $row['module_title'] . "<a href=# content='".$row['description']."'
class='tooltip'><span title='Module Description'><img src='images/i.png'/></span>
</a>". "</td>";
echo "<td>" . $row['module_choice'] . "</td>";
echo "</tr>";
}
echo "</table></form></div>";