Style 1 <td> in a table with 2 <td>s - php

I have a table that gets it´s content from a database, and I want to make the text in the column to the right align-right. I know how to do it when its HTML and the content is in the code, but I can't get it to work when the content is outputted from a database.
Down here you can see my code for the table: (in the collumn "ingredienser", I want to set the text-align to right.
<?php
$con = mysql_connect("****","****","****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("paj", $con);
$result = mysql_query("SELECT * FROM meny");
echo "<div id='menyer'>";
echo "<table border='1'>
<tr>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<div class='mall" . $row['mall'] . "'>";
echo "<tr>";
echo "<td>" . $row['namn'] . "</td>";
echo "<td>" . $row['ingredienser'] . "'</td>";
echo "</tr>";

Just add some CSS above your php code
<style>
table td {
text-align: right;
}
</style>
The above will right align text inside table cells

echo "<td><div align='right'>" . $row['ingredienser'] . "'</div></td>";

if this is the extent of your table, then you can definitely just use the following css:
.mall{
text-align:right;
}
I would suggest putting it in your external css file, but if you don't have one, then put it in a style tag inside of your body or table tag, depending on what kind of scope you want it to have like this:
<style>
.mall{
text-align:right;
}
</style>

set align="right" to your tr
while($row = mysql_fetch_array($result))
{
echo "<div class='mall" . $row['mall'] . "'>";
echo "<tr align='right'>";
echo "<td>" . $row['namn'] . "</td>";
echo "<td>" . $row['ingredienser'] . "'</td>";
echo "</tr>";

Just add styling to that one particular cell, it will apply to the entire column as it loops. As such,
echo "<td style=\"text-align:right;\">" . $row['ingredienser'] . "'</td>";
If you align the Table TD to text-align:right, EVERY single cell will be effected.

Related

How to update data in a table using dropdownlist box in PHP?

Im trying to make an orders table in PHP where i can choose in a dropdownlist box the driver and the truck to each order from the existing sql db.
i managed to create the dropdownlist inside the table but i dont know how to update the db.
im trying using isset function but im probably not doing it right.
here is the code i made + a screenshot:
screen
<!DOCTYPE html>
<html>
<head>
<title>Orders</title>
<link rel="stylesheet" type="text/css" href="table-general.css">
<style type="text/css">
body
{
font-family: Arial, Verdana, sans-serif;
font-size: 90%;
color: #666;
background-color: #f8f8f8;
}
</style>
</head>
<body>
<h1>Orders</h1>
Done</br></br>
<table class="general">
<tr class="head">
<th>Order_ID</th>
<th>Customer_ID</th>
<th>Driver_ID</th>
<th>Truck_ID</th>
<th>Date</th>
<th>Project_Name</th>
<th>Project_Place</th>
<th>Amount</th>
</tr>
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("login");
$result = mysql_query("select * from orders_table") or die("Failed".mysql_error());
$result2 = mysql_query("select * from trucks_table") or die("Failed".mysql_error());
if(mysql_num_rows($result2))
{
$select= '<select name="select">';
while($record2=mysql_fetch_array($result2))
{
$select.='<option value="'.$record2['TruckID'].'">'.$record2['TruckID'].'</option>';
}
}
$select.='</select>';
while($record = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $record['Order_ID'] . "</td>";
echo "<td>" . $record['Customer_ID'] . "</td>";
echo "<td>" . $record['Driver_ID'] . "</td>";
echo "<td>" . $select . "</td>";
if(isset($_POST['select']))
{
$t=$_POST['select'];
$sql = mysql_query("update orders_table set TruckID='$t' where Order_ID='".$record['Order_ID']."' ");
}
echo "<td>" . $record['Date'] . "</td>";
echo "<td>" . $record['Project_Name'] . "</td>";
echo "<td>" . $record['Project_Place'] . "</td>";
echo "<td>" . $record['Amount'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
You probably need to learn something more about HTTP FORMS, how to send data from client to the server with POST or GET and how to use this data on the server with PHP.
see tutorials like these:
http://www.w3schools.com/php/php_forms.asp
In short - to send data from HTML page to the server, you need to wrap the inputs, selects, textareas in
<form action="url"> </form>
where url is pointing to your server php script, where your handling the form data
then you need to include
<input type="Submit">
which will generate a button on which when the user clicks the data are sent back to the server

Adding php variable into a href link into a table

I am working on uploading a file along with a description and table and then displaying it in a table format. My problem is that I'm not sure how to link my the path for the uploaded file into the table so the user can click on the link in the table and it will download.
Code:
This is what I'm attempting to use>
<?php
include 'connect.php';
$result = mysqli_query($con,"SELECT DocDate, Description, DocFile FROM Documents");
echo "<table border='0' width='100%'>
<col width='50'>
<col width='100'>
<tr>
<th>Date</th>
<th>Description</th>
<th>File</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['DocDate'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td>" $row['DocFile'] "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
If you feel it to be usefull I'm happy to add the code where I upload the file to my server.
EdiT Sorry I put in the wrong variable thing into my table, I don't think it changes it too much
Is this what you're looking for?
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['DocDate'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td> " . $row['DocFile'] . " </td>";
echo "</tr>";
}
This is how you can do
echo '<td>'.$name.'</td>';
I prefer to use ' ' and within it have the HTML since HTML will contain a lot of "" so no need to use escape them and then separate PHP and HTML with concatenation.
you have syntax error in this line in while loop:
echo "<td>" $name "</td>";
should be :
echo "<td> $name </td>";
In this line you have an error:
echo "<td>" $row['DocFile'] "</td>";
You need to scape the " character:
echo "<td> " . $row['DocFile'] . " </td>";
With your syntax you are creating an error because you are ending the string after the td tag.
If you are using a web url which uses a php variable in the url and want to open a new tab when you click on the hyper link, use this
echo "<td><a target='_blank' href=\"http://view.php?Id=".$row['Id']."\">". $row['Id'] ."</a></td>";
This is helpful when you have to use a php variable in the hyperlink and is being used in a table. Clicking on the Id will open the page related to that Id in a new tab in this case.

Highlighting a cell PHP a certain color based on MYSQL value

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

Apply Table Style in Dynamic Table Creation with PHP

I am having trouble applying CSS styles to my dynamic table. I am connecting to a database and I know the data is populated correctly and displaying. My issue is that there are 900 records and I want to limit the table size, utilizing a scrollbar. I have read elseware that the proper CSS style nodes to accomplish this are: How to specify table's height such that a vertical scroll bar appears?
overflow: auto;
height: 700px;
display: block;
overflow-y: scroll;
At first I attempted this with inline styling (a no-no.. I realize), but it didn't work. I have read about adding a 'class' to the table and/or individual rows, which would then be reflected in my CSS style sheet, but I can't seem to figure out how to accomplish this. I get syntax errors when I add 'span' or 'class' tag designators to the PHP (I imagine from utilizing 'ECHO' - which both require double quotes).
Good example of what I'm trying to accomplish: http://www.timrivera.com/tests/csstables.html#markupIE
The PHP code snippet below has good syntax, but I don't know where to add the class or span designators appropriately. One thing to note - I need to have different styles for different tables, so changing the global 'table' CSS isn't going to work.
//Function that gets the SQL recordset.
$result2 = Get_Package_Info_EXT($conn, $var_PartNumber);
//do the table edits here.
echo "<table border='1' >
<tr>
<th>Facility</th>
<th>Process Flow</th>
<th>Operation</th>
<th>Device</th>
<th>Item</th>
<th>Value</th>
<th>Database Source</th>
</tr>";
while($row2 = oci_fetch_array($result2)){
echo "<tr>";
echo "<td>" . $row2['FACILITY_AT'] . "</td>";
echo "<td>" . $row2['SUB_FLOW_TYPE'] . "</td>";
echo "<td>" . $row2['OPN_NAME'] . "</td>";
echo "<td>" . $row2['SPEC_DEVICE'] . "</td>";
echo "<td>" . $row2['COMPONENT_NAME'] . "</td>";
echo "<td>" . $row2['COMPONENT_VALUE'] . "</td>";
echo "<td>" . $row2['SOURCE'] . "</td>";
echo "</tr>";
}
echo "</table>";
I think the best way would be to wrap a div around the table and give the div a height.
http://phpfiddle.org/main/code/i7h-9b1
<style type="text/css">
#scroll {
height: 100px; /* Max height of table */
overflow-y: scroll;
width: 340px;
}
</style>
<div id="scroll">
table
</div>
Using your code:
echo '
<style type="text/css">
#scroll {
height: 100px; /* Max height of table */
overflow-y: scroll;
width: 340px;
}
</style>';
//Function that gets the SQL recordset.
$result2 = Get_Package_Info_EXT($conn, $var_PartNumber);
//do the table edits here.
echo "<div id='scroll'><table border='1' >
<tr>
<th>Facility</th>
<th>Process Flow</th>
<th>Operation</th>
<th>Device</th>
<th>Item</th>
<th>Value</th>
<th>Database Source</th>
</tr>";
while($row2 = oci_fetch_array($result2)){
echo "<tr>";
echo "<td>" . $row2['FACILITY_AT'] . "</td>";
echo "<td>" . $row2['SUB_FLOW_TYPE'] . "</td>";
echo "<td>" . $row2['OPN_NAME'] . "</td>";
echo "<td>" . $row2['SPEC_DEVICE'] . "</td>";
echo "<td>" . $row2['COMPONENT_NAME'] . "</td>";
echo "<td>" . $row2['COMPONENT_VALUE'] . "</td>";
echo "<td>" . $row2['SOURCE'] . "</td>";
echo "</tr>";
}
echo "</table></div>";
Edit your PHP code to...
//Function that gets the SQL recordset.
$result2 = Get_Package_Info_EXT($conn, $var_PartNumber);
//do the table edits here.
echo "<div class=\"table-container\">";
echo "<table border='1' >
<tr>
<th>Facility</th>
<th>Process Flow</th>
<th>Operation</th>
<th>Device</th>
<th>Item</th>
<th>Value</th>
<th>Database Source</th>
</tr>";
while($row2 = oci_fetch_array($result2)){
echo "<tr>";
echo "<td>" . $row2['FACILITY_AT'] . "</td>";
echo "<td>" . $row2['SUB_FLOW_TYPE'] . "</td>";
echo "<td>" . $row2['OPN_NAME'] . "</td>";
echo "<td>" . $row2['SPEC_DEVICE'] . "</td>";
echo "<td>" . $row2['COMPONENT_NAME'] . "</td>";
echo "<td>" . $row2['COMPONENT_VALUE'] . "</td>";
echo "<td>" . $row2['SOURCE'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
Then just style using
div.table-container table {}
Using Calum's style format you could do this:
//Function that gets the SQL recordset.
$result2 = Get_Package_Info_EXT($conn, $var_PartNumber);
//do the table edits here.
echo "<style>#size{height:100px;width:340px;overflow-y:scroll;}</style>";
echo "<table id='size' border='1'>
<tr>
<th>Facility</th>
<th>Process Flow</th>
<th>Operation</th>
<th>Device</th>
<th>Item</th>
<th>Value</th>
<th>Database Source</th>
</tr>";
while($row2 = oci_fetch_array($result2)){
echo "<tr>";
echo "<td>" . $row2['FACILITY_AT'] . "</td>";
echo "<td>" . $row2['SUB_FLOW_TYPE'] . "</td>";
echo "<td>" . $row2['OPN_NAME'] . "</td>";
echo "<td>" . $row2['SPEC_DEVICE'] . "</td>";
echo "<td>" . $row2['COMPONENT_NAME'] . "</td>";
echo "<td>" . $row2['COMPONENT_VALUE'] . "</td>";
echo "<td>" . $row2['SOURCE'] . "</td>";
echo "</tr>";
}
echo "</table>";
I tested it and works fine. And for the different table styles you could:
<style>
#table1
{
style code here
}
#table2
{
style code here
}
</style>
and so on... then you could apply them to the tables:
<table id="table1">
...
</table>
<table id="table2">
...
</table>

JQuery Table Sorter - PHP related

Apologies from the off if this is an amateurish question.
I am trying to get the Jquery Tablesorter plugin to work with a table generated with PHP from a MySql database. At the moment I'm unable to get the sorting to work. I'm thinking its something to do with the sequence of the javascript and php operating and may need to implement a callback in the javascript or something. Anyway code I have is:
<html>
<head>
<script src="jquery/jquery.js" type="text/javascript"></script>
<script src="jquery/jquery.tablesorter.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("#table1").tablesorter({ sortlist: [0,0] });
});
</script>
</head>
<body>
<?php
$con = mysql_connect("localhost","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("Database1", $con);
$result = mysql_query("SELECT * FROM PlayerStats1 WHERE G>2 LIMIT 0,20");
echo "<table id=\"table1\" class=\"tablesorter\" border=1px>
<caption align=top>Stats</caption>
<thead>
<tr>
<th width=90px>Forename</th>
<th width=90px>Surname</th>
<th width=50px>Team</th>
<th width=40px>G</th>
<th width=50px>RPG</th>
<th width=50px>APG</th>
<th width=50px>TOPG</th>
<th width=50px>BPG</th>
<th width=50px>SPG</th>
<th width=50px>PPG</th>
</tr>
</thead>";
while($row = mysql_fetch_array($result))
{
echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['FORENAME'] . "</td>";
echo "<td>" . $row['SURNAME'] . "</td>";
echo "<td align='center'>" . $row['TEAM'] . "</td>";
echo "<td align='center'>" . $row['G'] . "</td>";
echo "<td class='col1' align='center'>" . $row['RPG'] . "</td>";
echo "<td class='col2' align='center'>" . $row['APG'] . "</td>";
echo "<td class='col3' align='center'>" . $row['TOPG'] . "</td>";
echo "<td class='col4' align='center'>" . $row['BPG'] . "</td>";
echo "<td class='col5' align='center'>" . $row['SPG'] . "</td>";
echo "<td class='col6' align='center'><b>" . $row['PPG'] . "</b></td>";
echo "</tr>";
echo "</tbody>";
}
echo "</table>";
mysql_close($con);
?>
</body>
</html>
If it is a case of implementing a call back- any guidance/code would be much appreciated.
Has nothing to do with the sequence; your browser executes the Javascript, and as far as it's concerned, the PHP-generated HTML is the same as any HTML, so the fact that it's generated is irrelevant. I suggest you turn on debug mode in tablesorter and use Firebug and see if that illuminates the issue, otherwise I'm not seeing, on the surface, what could be causing a problem. Not that it might not be there, I'm just missing it. :)
the Tbody tag should be called only once and outside of your TR loop.
TBody is a singular collection of ALL your table body TR elements. that's going to jack tablesorter up.

Categories