Trouble writing to html from mysql database - php

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

Related

Table page number position problems

I have a problem with page numbers showing in footer on table. Here is my PHP code
<tfoot>
<tr>
<th colspan="5">Page</th>
?php
$sql = "SELECT COUNT(ID) AS total FROM Settings";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$total_pages = ceil($row["total"] / $results_per_page); // calculate total pages with results
for ($i=1; $i<=$total_pages; $i++) { // print links for all pages
echo "<th> <a href='example.php?page=".$i."'";
if ($i==$page) echo " class='curPage' </th>";
echo ">".$i."</a> </th>";
};
?>
</tr>
</tfoot>
CSS:
body {
font-size: 15px;
color: #343d44;
font-family: "segoe-ui", "open-sans", tahoma, arial;
padding: 0;
margin: 0;
}
table {
margin: auto;
font-family: "Lucida Sans Unicode", "Lucida Grande", "Segoe Ui";
font-size: 12px;
}
h1 {
margin: 25px auto 0;
text-align: center;
text-transform: uppercase;
font-size: 17px;
}
table td {
transition: all .5s;
}
/* Table */
.data-table {
border-collapse: collapse;
font-size: 14px;
min-width: 537px;
}
.data-table th,
.data-table td {
border: 1px solid #e1edff;
padding: 7px 4px;
}
.data-table caption {
margin: 7px;
}
/* Table Header */
.data-table thead th {
background-color: #508abb;
color: #FFFFFF;
border-color: #6ea1cc !important;
text-transform: uppercase;
}
/* Table Body */
.data-table tbody td {
color: #353535;
}
.data-table tbody td:first-child,
.data-table tbody td:nth-child(4),
.data-table tbody td:last-child {
text-align: right;
}
.data-table tbody tr:nth-child(odd) td {
background-color: #f4fbff;
}
.data-table tbody tr:hover td {
background-color: #ffffa2;
border-color: #ffff0f;
}
/* Table Footer */
.data-table tfoot th {
background-color: #e5f5ff;
text-align: right;
}
.data-table tfoot th:first-child {
text-align: left;
}
.data-table tbody td:empty{
background-color: #ffcccc;
}
Here is image
How to make this right? How to fit all pages numbers to end of table footer?
You just remove the th tag from the for loop . And echo the page no inside the th tag the give some space between the nos. Please replace your code with below given code.
This will work in your case.
<tfoot>
<tr>
<th colspan="5">Page</th>
<?php
$sql = "SELECT COUNT(ID) AS total FROM Settings";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$total_pages = ceil($row["total"] / $results_per_page); // calculate total pages with results
echo "<th>";
for ($i=1; $i<=$total_pages; $i++) { // print links for all pages
echo " <a href='example.php?page=".$i."'";
if ($i==$page) echo " class='curPage' </th>";
echo ">".$i."</a> ";
};
echo "</th>";
?>
</tr>
</tfoot>

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.

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/

select name from php query and refresh seprate query on same page.

I have just used my first bit of AJAX to search a MySQL database. This is for a persons name I would normally add a href to the result (which i have) and post to a seprate page to show all the the relevant details, eg address, grade and location. However i was wondering if this is possible staying on the same page. This is what i have so far. ( and yes the CSS will be moving to a seprate page)
<?php
if(!strlen(trim($_SESSION['Unique_Id']))) {
header("Location:roster.php");
exit();
}
?>
<title>Search Roster</title>
<style>
body {
font-family: "Lucida Grande", Verdana, Arial, sans-serif;
font-size: 12px;
}
#results {
color: #fff;
background: #414141;
width: 180px;
max-height: 200px;
padding-left: 4px;
border: 1px solid #000;
overflow-y: scroll;
overflow-x: hidden;
}
a { color: #fff; display: block; }
a:hover { background: #666666; }
</style>
Keyword Search:
<form id="searchform" method="post" onsubmit="return false;"><input autocomplete="off" id="searchbox" name="searchq" onkeyup="sendRequest()" type="textbox">
</form><div id="show_results"></div><script src="config/prototype.js" type="text/javascript">
</script>
<script>
function sendRequest() {new Ajax.Updater('show_results', 'pages/login/search.php', { method: 'post', parameters: $('searchform').serialize() });
}
</script>
<?php include("pages/include/footer.php"); ?>
and the search.php page
<?php
$host="****"; // Host name
$sqlusername="****"; // Mysql username
$sqlpassword="****"; // Mysql password
$db_name="****"; // Database name
$tbl_name="****"; // Table name
mysql_connect("$host", "$sqlusername", "$sqlpassword")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$searchq = $_POST['searchq'];
if(empty($searchq)) {
echo "";
} else {
echo "<div id='results'-->";
//This query searches the name field for whatever the input is.
$sql = "SELECT * FROM USER WHERE Employee LIKE '%$searchq%' OR staff LIKE '%$searchq%'";
echo "<table border='3'>
<tr>
</tr>";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
$name = $row['EmployeeName'];
$id=$row['Unique_Id'];
echo "<a href=anotherpage.php?item=".$id."</a>";
echo "$name";
echo "</br>";
}
echo "</div>";
}
?>
You have did mistake in following line of code in "search.php". Also verify other line of code.
echo "<a href=anotherpage.php?item=".$id."</a>";
echo "$name";
Above code should be like as follows :
echo "".$name."";

Coloring the table with alternate color

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

Categories