Coloring the table with alternate color - php

Below i have added my css file and my html file i need to color my table with alter name color but its not working i don't know where i have done the mistake.
CSS FILE :-
table.gridtable{
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
float:center;
border-width:1px;
border-style:groove;
border-color:black;
border-collapse:collapse;
width:600px;
}
table.gridtable th{
border-width:1px;
padding:3px;
border-style:solid;
border-color:black;
}
table.gridtable td{
border-width:1px;
padding:3px;
border-style:solid;
border-color:black;
}
.oddrowcolor{
background-color: black;
}
.evenrowcolor{
background-color: red;
}
table.gridtable td.darkcol{
border-top:0px;
border-bottom:0px;
border-right: 1px;
}
table.gridtable td.emptycol{
border-bottom:0px;
border-right: 1px;
}
HTML & PHP:-
<table class="gridtable" id="alternatecolor" align="center"
<tr><th>disease Name/th><th>Gene Name/th><th>OMIM Number</th></tr>
<?php
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
$selected = mysql_select_db("missensencemuttation",$dbhandle) or die("Could not select disease");
$result = mysql_query("SELECT DISTINCT * FROM `gene_data` ORDER BY disease_name");
$last=" ";
while($row = mysql_fetch_array($result))
$now=$row[2];
if($last != $now){
$rcnt=mysql_query("select count(gene_name) from gene_data where gene_name='$now'");
if($rcnt==1){
echo "<tr><td>";
echo "<a href='d1.php?d_name=".$row['disease_name']."'>".$row['disease_name'];
echo "</a></td>";
echo "<td>".$row['gene_name']."</td>";
echo "<td>".$row['ommi_number']."</td></tr>";
}
else{
echo "<tr><td class='emptycol'>";
echo "<a href='d1.php?d_name=".$row['disease_name']."'>".$row['disease_name'];
echo "</a></td>";
echo "<td>".$row['gene_name']."</td>";
echo "<td>".$row['ommi_number']."</td></tr>";
}
}
else
{
echo "<tr'>";
echo "<td class='darkcol'>";
echo '&nbsp';
echo "</td>";
echo "<td>".$row['gene_name']."</td>";
echo "<td>".$row['ommi_number']."</td></tr>";
}
$last=$row[2];
can any one help me with this ??

Maybe I'm misunderstanding, but it looks like you want to apply oddrowcolor or evenrowcolor to alternating rows - but in your actual php/html, I don't see those class names anywhere. They're not being put on the rows, so their rules wont be applied. Just add them to the appropriate <tr> tags in your php and it should work.
Alternatively you could use the nth-of-type selector to apply the rules without altering your php:
tr:nth-of-type(odd) {
background-color: black;
}
tr:nth-of-type(even) {
background-color: red;
}

If you want alternate colors for each table row, then a simple CSS should do it. This way, if you want to insert new rows dynamically, it will adjust the colors automatically.
.stripped-rows:nth-child(odd){
background-color:red;
}
.stripped-rows:nth-child(even){
background-color:blue;
}
and html ...
<table>
<tr class = 'stripped-rows'>
<td>sad</td>
</tr>
<tr class = 'stripped-rows'>
<td>sad</td>
</tr>
<tr class = 'stripped-rows'>
<td>sad</td>
</tr>
DEMO

Related

PHP controlling where a table is placed on a web page

I'm new to PHP and I'm working on a very simple SELECT query pulling information from a MySQL database. I have an echo statement that prints out that my connection to the database was successful which is the very first line on the webpage. After "Connected Successfully" is display the SELECT statement pulls the query information into a table.
Right now everything is pulling and displaying correctly but there is a large gap between my echo connection statement and the actual table results. I've tried to directly align my table to 'TOP' but that is still producing the large gap.
<!DOCTYPE html>
<html>
<style>
#trip th {
padding-top: 5px;
padding-bottom: 5px;
text-align: left;
background-color: #4CAF50;
color: white;
}
#trip {
border-collapse: collapse;
width: 75%;
}
#trip td, #trip th {
border: 1px solid #ddd;
padding: 8px;
}
#trip tr:nth-child(even){
background-color: #f2f2f2;
}
#trip tr: hover {
background-color: #ddd;
}
</style>
<body>
<?php
require "Login.php";
?>
<?php
//Create Connection
$conn = mysqli_connect($serverName, $userName, $password, $dbName);
//Check connection
if ($conn === false) {
die("ERROR: Could not Connect. " . mysqli_connect_error());
}
echo "Connected successfully";
//SQL SELECT query execution
$sql = "SELECT * FROM Trip";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table id='trip' align='top'>";
echo "<tr>";
echo "<th>TID </th>";
echo "<th>NAME </th>";
echo "<th>DATE </th>";
echo "<th>COST </th><br>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['TID'] . " </td>";
echo "<td>" . $row['NAME'] . " </td>";
echo "<td>" . $row['DATE'] . " </td>";
echo "<td>" . $row['COST'] . " </td>";
echo "</tr>";
}
echo "</table>";
//Free result set
mysqli_free_result($result);
}
else {
echo "No records matching your query were found";
}
}
else {
echo "ERROR: Could not execute $sql. " . mysqli_error($conn);
}
//Close connection
mysqli_close($conn);
?>
</body>
</html>
I'd like the table to display right underneath my "Connected Successfully" message but something seems to be putting the table further down the page.
Edit Fixed the '\' that was added when I tried to paste into Stackoverflow. Not sure why that was added. It's not in my actual PHP file.
The problem is the <br> you have at the end of this line:
echo "<td>" . $row['COST'] . " </td><br>";
Since <br> isn't allowed inside <tr> elements, the browser is rendering them before the table. So the number of blank lines before the table is the same as the number of table rows.
Get rid of that, it serves no purpose.
#trip th {
padding-top: 5px;
padding-bottom: 5px;
text-align: left;
background-color: #4CAF50;
color: white;
}
#trip {
border-collapse: collapse;
width: 75%;
}
#trip td,
#trip th {
border: 1px solid #ddd;
padding: 8px;
}
#trip tr:nth-child(even) {
background-color: #f2f2f2;
}
#trip tr: hover {
background-color: #ddd;
}
<div>Connected successfully</div>
<table id='trip' align='top'>
<tr>
<th>TID </th>
<th>NAME </th>
<th>DATE </th>
<th>COST </th>
</tr>
<tr>
<td>TID</td>
<td>NAME</td>
<td>DATE</td>
<td>COST</td>
</tr>
<tr>
<td>TID</td>
<td>NAME</td>
<td>DATE</td>
<td>COST</td>
</tr>
<tr>
<td>TID</td>
<td>NAME</td>
<td>DATE</td>
<td>COST</td>
</tr>
</table>

Edit time in mysql output

Have a question, I found this:
http://www.phpdevtips.com/2013/06/email-open-tracking-with-php-and-mysql/
So was able to get it working and even figured out how to show the data in a page, this one:
<html>
<head>
<meta http-equiv="refresh" content="900">
<title>Who read his e-Mail, and when?</title>
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<style>
* {
box-sizing: border-box;
}
.txt {
font-family: 'Open Sans', sans-serif;
font-size: 11px;
text-align: left;
}
h2 {
padding: 5px;
font-family: 'Open Sans', sans-serif;
font-size: 24px;
text-align: left;
font-weight: bold;
}
#myTable {
border-collapse: collapse;
width: 100%;
border: 1px solid #ddd;
font-size: 18px;
}
#myTable th, #myTable td {
text-align: left;
padding: 12px;
}
#myTable tr {
border-bottom: 1px solid #ddd;
}
#myTable tr.header, #myTable tr:hover {
background-color: #f1f1f1;
}
</style>
</head>
<body>
<h2>Who read his e-Mail, and when?</h2>
<?php
mysql_connect("localhost", "***", "***") or die (mysql_error ());
mysql_select_db("***") or die(mysql_error());
$strSQL = "SELECT * FROM email_log";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {
echo "<table border='1' width='100%' style='border-collapse: collapse' cellpadding='5' class='txt' id='myTable'>";
echo "<tr class='header'>";
echo "<td width='5%' class='txt'>";
echo $row['id'] . "</td><td width='30%' class='txt'>";
echo $row['user'] . "</td><td width='40%' class='txt'>";
echo $row['subject'] . "</td><td width='25%' class='txt'>";
echo $row['opened'] . "</td>";
echo "</tr>";
echo "</table>";
}
mysql_close();
?>
</body>
</html>
So far, so good, now my question is, the date and time that get´s pulled out of the database in the row opened is showing like this on the result page: 2017-03-09 07:53:58
I would like it to show as 07:53:58 09-03-2017
and it should be 6 hours later, as this seems to me that what it is showing now is server time, not our timezone.
So reaching out for your help, anyone can give me a push into the right direction? Any Help appreciated.
Joris
For changing the date format you can use
date('h:i:s d-m-Y', strtotime($row['opened']));
The best way to manage the time zone, you can set a timezone, at the place where you are inserting the date time to database. Initialise the timezone what you want before mapping to database.
date_default_timezone_set("Asia/Bangkok");
instead of "Asia/Bangkok" you can give your timezone
Thank you all, great solutions, this one did it best:
echo date('H:i:s d-m-Y', strtotime($row['opened'])+21600);
Tnx, Greetz from Spain :)
SELECT DATE_FORMAT(opened,'%H:%i:%s %d-%m-%Y')opened,id,user,subject FROM email_log
You can try DATE_FORMAT()of MySQL.
Try above code hope this will helps.

Trouble writing to html from mysql database

I have a question related to php and html.
I am trying to write in a table from a mysql database.
My question is: Why does it write the text "SEMIFINAL 2" before it writes the table, but in the code the table is written before the text "SEMIFINAL 2"?
I could really use some help. Thanks a lot.
P.S. Here is the code
<html>
<style>
table {
color: #333;
font-family: Helvetica, Arial, sans-serif;
width: 640px;
border-collapse:
collapse; border-spacing: 0;
}
td, th {
border: 1px solid transparent; /* No more visible border */
height: 30px;
transition: all 0.3s; /* Simple transition for hover effect */
}
th {
background: #DFDFDF; /* Darken header a bit */
font-weight: bold;
text-align: center;
}
td {
background: #FAFAFA;
text-align: center;
}
/* Cells in even rows (2,4,6...) are one color */
tr:nth-child(even) td { background: #F1F1F1; }
/* Cells in odd rows (1,3,5...) are another (excludes header cells) */
tr:nth-child(odd) td { background: #FEFEFE; }
tr td:hover { background: #666; color: #FFF; } /* Hover cell effect! */
</style>
<?php
$host="localhost";
$username="root";
$password="";
$db_name="lol";
mysql_connect("$host", "$username", "$password") or die("cannot connect");
mysql_select_db("lol") or die("Cannot connect to database");
$query="call semi2";
$results = mysql_query($query);
print('<table align=center>');
print ("<tr>");
echo '<th>First Name</th>';
echo '<th>Last Name</th>';
echo '<th>Victories</th>';
echo "</center>";
$nr_val=0;
if($results === FALSE) {
die(mysql_error()); // TODO: better error handling
}
while($row=mysql_fetch_row($results)){
echo" <tr>\n";
foreach($row as $value){
echo "<td>$value</td>";
$nr_val++;
}
echo"</tr>";
}
$coln=mysql_num_fields($results);
$nr_inreg=$nr_val/$coln;
echo "<br>";
echo "<center>";
if ($nr_inreg>0)
echo "SEMIFINAL 2";
else
die ("Can't find any recording....");
echo "</center>";
mysql_close();
?>
</html>
You forgot the closure table tag
You have a center closure tag that seems misplaced in your table header
Add the closure table tag closing before outputing your text

Css Table doesn't work

I want to modify my table by CSS, but it doesn't work, the only CSS part that doesn't work is the table.
The rest of the CSS works correctly.
The PHP code:
<table style="width:100%">
<tr>
<td>Nif</td>
<td>Nom</td>
<td>Cognoms</td>
<td>Data</td>
<td>Provincia</td>
</tr>
<?php
while ($fila = mysqli_fetch_row($resultado2)) {
$Nif =$fila[0];
$Nombre=$fila[1];
$Cognoms=$fila[2];
$Fecha=$fila[3];
$Provincia=$fila[4];
echo"<tr>";
echo"<td>$Nif</td>";
echo"<td>$Nombre</td>";
echo"<td> $Cognoms</td>";
echo"<td> $Fecha</td>";
echo"<td>$Provincia</td>";
echo"</tr>";
echo" </table>";
}
mysqli_close($con);
?>
Part of the CSS:
input#boton{
color:white;
font-size:2em;
background-color:#00BFFF;
height:0.8cm;
border:2px;
width:80%;
margin-top:1%;
margin-left:10%;
-webkit-border-radius:5px;
-ms-border-radius:5px;
-moz-border-radius:5px;
}
input#boton:hover{
background-color:#2EFE2E;
}
table, th, td {
border: 2px solid black;
color: blue;
width: 50%;
font-size: 150%;
text-align:center;
background-color:yellow;
}
Any help would be appreciated .
For each row, you print echo" </table>"; You should put it at the end of the while loop.
You have an error in your Php code. you must not print table in each row.
while(){
}

Design php output table with css

Please anyone who can help me with this problem. I have a html file with a question. When submit is clicked it calls a php(which is connected with my database in Oracle) and it shows a table inside the html (using ajax).
I would like to know how can i show that table in a nicer form? Can I design it?
Here is my code in php
<?php
{
session_start();
include "connect.php";
}
$conn = DBConnect();
$stid = DBExecute($conn, "select something...");
$total = 0;
echo "<table>
<tr>
<th>Name1</th>
<th>Name2</th>
</tr>";
while ($row = oci_fetch_array($stid, OCI_ASSOC)) {
echo "<tr><td>";
echo $row['something'];
echo "</td>";
echo "<td>";
echo $row['something'];
echo "</td>";
}
echo "</table>";
?>
and I want to have for example this css instructions for the table i will show...
#newspaper-a
{
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 12px;
margin: 45px;
width: 480px;
text-align: left;
border-collapse: collapse;
border: 1px solid #69c;
}
#newspaper-a th
{
padding: 12px 17px 12px 17px;
font-weight: normal;
font-size: 14px;
color: #039;
border-bottom: 1px dashed #69c;
}
#newspaper-a td
{
padding: 7px 17px 7px 17px;
color: #669;
}
#newspaper-a tbody tr:hover td
{
color: #339;
background: #d0dafd;
}
Please does anyone know what should i do to succeed it???
Important the table will be shown inside the same page not in another one..
You need to add your ID to the table div - like this:
<?php
{
session_start();
include "connect.php";
}
$conn = DBConnect();
$stid = DBExecute($conn, "select something...");
$total = 0;
// ADD YOUR ID HERE vvvvvvvv
echo "<table id="newspaper-a">
<tr>
<th>Name1</th>
<th>Name2</th>
</tr>";
while ($row = oci_fetch_array($stid, OCI_ASSOC)) {
echo "<tr><td>";
echo $row['something'];
echo "</td>";
echo "<td>";
echo $row['something'];
echo "</td>";
}
echo "</table>";
?>
http://jsfiddle.net/jakemulley/hs3mF/

Categories