How to put database information as headers in table - php

I want some information that is stored in my database to be an header in a table.
Basically its got a list of competitors down the side with theirs scores running in rows. Each competitor has their own row. At the top of the table is the name of the event they took part in. I cant manage to make the event name come from the database so got to edit the page each time I add a set of scores.
I have attempted as you will see in the code below;
<?php
require("db_connect.php");
$result = mysql_query("SELECT *, (ct1 + ct2 + ct3 + ct4 + ct5 + ct6) AS results FROM resultsopen WHERE ct='1'");
if(mysql_num_rows($result) == 0) { echo 'No competitors found'; } else {
echo "<table width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"2\" align=\"center\">
<tr align=\"center\">
<td>Competitor</td>
<td>Member No</td>
<td>" . $row['cta'] . "</td>
<td>E2</td>
<td>E3</td>
<td>E4</td>
<td>E5</td>
<td>E6</td>
<td>E7</td>
<td>Overall</td>
<td></td>
</tr>";
$x=1;
while($row = mysql_fetch_array($result))
{
if($x%2): $rowbgcolor = "#FFFFFF"; else: $rowbgcolor = "#EEEEEE"; endif;
echo "<tr align=\"center\" bgcolor=\"" .$rowbgcolor. "\">";
echo "<td>" . $row['competitor'] . "</td>";
echo "<td>" . $row['memberno'] . "</td>";
echo "<td>" . $row['ct1'] . "</td>";
The line in question is <td>" . $row['cta'] . "</td>
The other lines are just text which I manually change each time.
I also know that mysql should be changed to mysqli but I am not confident in the change just yet.

Related

PHP MYSQL - $_GET and Query Issue

I have a page which contains a table. The 'change_id' field contains hyperlinks which direct the user to a different page with an overview of that change_id's details.
'<td>' . $row['change_id'] . '</td>';
Now, just to test that the change_id is being received on the home2.php page, I used the following code:
<?php
include 'config.php';
$change_id=$_GET['change_id'];
print_r($_GET);
?>
This test successfully displayed the correct change id's:
Array ( [changeid] => 1006 )
Now, when I go to query the SQL Database using the change_id it doesn't work as desired.
<?php
include 'config.php';
$change_id=$_GET['change_id'];
$query1 = "SELECT * FROM `change_request_tbl` WHERE `change_id` = $change_id";
$result = mysqli_query($conn, $query1);
echo "<fieldset><legend><strong>New Requests:</legend></strong>
<table border=4 bordercolor=black class=table>
<tr>
<th>Change ID:</th>
<th>Customer Name:</th>
<th>Change Requestor:</th>
<th>Date CR raised:</th>
<th>CPM/Ticket:</th>
<th>Out of Hours:</th>
<th>Change Category:</th>
</tr>";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['change_id'] . "</td>";
echo "<td>" . $row['customer_name'] . "</td>";
echo "<td>" . $row['change_requestor'] . "</td>";
echo "<td>" . $row['date_cr_raised'] . "</td>";
echo "<td>" . $row['cpm_ticket'] . "</td>";
echo "<td>" . $row['out_of_hours'] . "</td>";
echo "<td>" . $row['category_of_change'] . "</td>";
echo "</tr>";
}
echo "</table></fieldset><br><br><br>";
?>
The table are headers are shown without any data. Any ideas on how to fix? Thanks in advance
You are setting $change_id to $_GET['change_id']
However you are passing $_GET the parameter name of changeid
If you change
$change_id = $_GET['change_id'];
To
$change_id = $_GET['changeid'];
It should work as expected :)

How to hide a column if no data is present

I have a table that pulls information from the database. To keep it looking neat for the people visiting the webpage, I would like some columns to hide if no information is there.
My table is for results from motorsport events throughout the year. At the beginning of the year there will only be 1 event but by the time it comes to December there will be 20+ results added. So, instead of having lots of empty columns, I want them to be hidden until the title of that event has been added.
I tried to use !empty but this did not work as I wanted to it. It just moved the empty rows to the left leaving the headers not matching the correct results.
Here is my code:
<?php
//MySqli Select Query
$results = $mysqli->query("SELECT *, (u181 + u182 + u183 + u184 + u185 + u186 + u187 + u188 + u189 + u1810 + u1811 + u1812 + u1813 + u1814) AS total FROM results WHERE u18 = '1'");
$title = $mysqli->query("SELECT * FROM closedevents");
while ($t = $title->fetch_assoc()){
echo "<table width=\"1000\" border=\"0\" cellpadding=\"5\" cellspacing=\2\" class=\"entrywriting\" align=\"center\">
<tr align=\"center\">
<td>Overall</td>
<td>Competitor</td>
<td>" . $t["u18a"] . "</td>
<td>" . $t["u18b"] . "</td>
<td>" . $t["u18c"] . "</td>
<td>" . $t["u18d"] . "</td>
<td>" . $t["u18e"] . "</td>
<td>" . $t["u18f"] . "</td>
<td>" . $t["u18g"] . "</td>
<td>" . $t["u18h"] . "</td>
<td>" . $t["u18i"] . "</td>
<td>" . $t["u18j"] . "</td>
<td>" . $t["u18k"] . "</td>
<td>" . $t["u18l"] . "</td>
<td>" . $t["u18m"] . "</td>
<td>" . $t["u18n"] . "</td>
<td>Total</td>
</tr>";
}
//set counter
$counter = 1;
while($row = $results->fetch_assoc()) {
echo "<tr align=\"center\">";
echo "<td>" . $counter . "</td>";
echo '<td>'.$row["competitor"].'</td>';
echo '<td>'.$row["u181"].'</td>';
echo '<td>'.$row["u182"].'</td>';
echo '<td>'.$row["u183"].'</td>';
echo '<td>'.$row["u184"].'</td>';
echo '<td>'.$row["u185"].'</td>';
echo '<td>'.$row["u186"].'</td>';
echo '<td>'.$row["u187"].'</td>';
echo '<td>'.$row["u188"].'</td>';
echo '<td>'.$row["u189"].'</td>';
echo '<td>'.$row["u1810"].'</td>';
echo '<td>'.$row["u1811"].'</td>';
echo '<td>'.$row["u1812"].'</td>';
echo '<td>'.$row["u1813"].'</td>';
echo '<td>'.$row["u1814"].'</td>';
echo '<td>'.$row["total"].'</td>';
echo "</tr>";
$counter++; //increment count by 1
}
echo "</table>";
?>
So, if there was no title name in u18j, k, l, m, n then them columns would not be shown on the table.
A PHP alternative:
<?php
//MySqli Select Query
$results = $mysqli->query("SELECT *, (u181 + u182 + u183 + u184 + u185 + u186 + u187 + u188 + u189 + u1810 + u1811 + u1812 + u1813 + u1814) AS total FROM results WHERE u18 = '1'");
$title = $mysqli->query("SELECT * FROM closedevents");
$all_cols=array("u18a" => "u181","u18b"=> "u182","u18c"=> "u183","u18d"=> "u184","u18e"=> "u185","u18f"=> "u186","u18g"=> "u187",
"u18h"=> "u188","u18i"=> "u189","u18j"=> "u1810","u18k"=> "u1811","u18l"=> "u1812","u18m"=> "u1813","u18n"=> "u1814");
while ($t = $title->fetch_assoc()){
// remember empty cols
$empty_cols=array();
echo "<table width=\"1000\" border=\"0\" cellpadding=\"5\" cellspacing=\2\" class=\"entrywriting\" align=\"center\">
<tr align=\"center\">
<td>Overall</td>
<td>Competitor</td>";
foreach ($all_cols as $col => $value) {
if (!empty($t[$col])) {
echo "<td>" . $t[$col] . "</td>";
} else {
// set this column as empty for later
$empty_cols[]=$col;
}
} unset($col); unset($value);
echo "
<td>Total</td>
</tr>";
}
//set counter
$counter = 1;
while($row = $results->fetch_assoc()) {
echo "<tr align=\"center\">";
echo "<td>" . $counter . "</td>";
echo '<td>'.$row["competitor"].'</td>';
foreach ($all_cols as $col => $value) {
if (!in_array($col, $empty_cols)) {
// echo non-empty values
echo '<td>'.$row[$value].'</td>';
}
} unset($col); unset($value);
$counter++; //increment count by 1
}
echo "</table>";
?>
Well. I help you in jQuery. You must to include jquery library in your code, obviously. More information: http://jquery.com
First, you must to change your table with an identifier attribute:
echo "<table width=\"1000\" border=\"0\" cellpadding=\"5\" cellspacing=\2\" class=\"entrywriting\" align=\"center\">
<tr align=\"center\">
<td>Overall</td>
<td>Competitor</td>
<td rel='a'>" . $t["u18a"] . "</td>
<td rel='b'>" . $t["u18b"] . "</td>
<td rel='c'>" . $t["u18c"] . "</td>
<td rel='d'>" . $t["u18d"] . "</td>
<td rel='e'>" . $t["u18e"] . "</td>
<td>Total</td>
</tr>";
And down, in your while, the same:
echo '<td rel="a">'.$row["u181"].'</td>';
echo '<td rel="b">'.$row["u182"].'</td>';
echo '<td rel="c">'.$row["u183"].'</td>';
echo '<td rel="d">'.$row["u184"].'</td>';
echo '<td rel="e">'.$row["u185"].'</td>';
Then the javascript magic:
// select the first row and found all td in the first row
$('table.entrywriting tr:first-child').find('td').each(function() {
var text = $(this).text(); // get the inner text
if(!text) { // if text is empty
var position = $(this).attr('rel'); //position like a, b, c...
// iterate all tds in this position
$('table.entrywriting td[rel="'+position+'"]').each(function() {
$(this).hide(); // hide the td with the position
});
}
});
Good luck!
I think you can test varibables and reduc to one echo instructions like this :
<?php
$counter = 1;
$out ='';
while($row = $results->fetch_assoc()) {
$out .= "<tr align=\"center\">";
$out .= "<td>" . $counter . "</td>";
$out .= (isset($row["competitor"]) && !empty(trim($row["competitor"]))?'<td>'.$row["competitor"].'</td>':'';
..... etc
$out .= "</tr>";
$counter++; //increment count by 1
}
echo $out;

Generate a Unique Page with the details after a form submit

I have a form, where the user can fill in his name, email, address, phone, age and they can also order something like a product.
After submitting the form, all information will be stored in a database & also phpmail will send an email with all the information.
But after the submit, I want the user to see this :
This is your details: http://example.com/order.php?unique=21434612
On clicking link, they will see a page with all the information they had filled in the form. Like this :
```
Your name is : $name
Your age is: $age
etc.
```
So how can I generate auto HTML pages after submit?
write this code on the page where you want the detail.configure your details. like dbname and row details etc. yes this data is printed in table format. if you dont like it just remove it.
.
if(isset($_POST['submit']))
{
$con=mysqli_connect("localhost", "root", " ") or die (mysqli_error ());
// Select database
mysqli_select_db($con,"rdb") or die(mysqli_error());
// SQL query
$strSQL = "SELECT * FROM tablename";
// Execute the query (the recordset $rs contains the result)
$rs = mysqli_query($con,$strSQL);
print "
<table border=\"5\" cellpadding=\"5\" cellspacing=\"0\" style=\"border- collapse: collapse\" bordercolor=\"#808080\" width=\"100%\" id=\"AutoNumber2\" bgcolor=\"#C0C0C0\">
<tr>
<td width=100>ID:</td>
<td width=100>First Name</td>
<td width=100>Last Name</td>
<td width=100>Email</td>
<td width=100>User Name</td>
<td width=100>Password</td>
<td width=100>Date Of Birth</td>
<td width=100>Gender</td>
</tr>";
while($row = mysqli_fetch_array($rs))
{
print "<tr>";
print "<td>" . $row['id'] . "</td>";
print "<td>" . $row['fname'] . "</td>";
print "<td>" . $row['lname'] . "</td>";
print "<td>" . $row['Email'] . "</td>";
print "<td>" . $row['uname'] . "</td>";
print "<td>" . $row['password'] . "</td>";
print "<td>" . $row['dob'] . "</td>";
print "<td>" . $row['gender'] . "</td>";
print "</tr>";
}
print "</table>";
}
}

item to hide once overdue by 1 day

Ok, I am fairly new to the PHP scene so please correct anything I might have done wrong ect. I am trying to do a calender list, which as soon as it get to the day after the due date it hides it from view. I currently have it getting the information that is submitted however not sure how to continue from here to make the overdue items hide? I have attached my code below for feedback also.
<?php
include 'db-connect.php';
$result = mysqli_query($con,"SELECT * FROM tasks");
echo "<table border='0' align='center'>
<tr>
<th>Task Title:</th>
<th>Task Description:</th>
<th>Due Date:</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['task'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . $row['datedue'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
You could do it in your MYSQL query
SELECT * FROM tasks WHERE datedue >= CURDATE()
This would only return the rows that were higher or equal to the current date.

Displaying MySQL data in PHP Table Not working

Currently I'm creating just a simple website that I'm fooling around with. Users can add movies to watch, and then can view them later on. What my current problem is, is this.
Sorry for the large image. As you can see its displaying the first result correctly, but the second result gets all skrewy and displays at the top of the screen. My code for displaying the data is:
$result = mysql_query("SELECT * FROM `movies`");
echo "
<table id=\"allTable\" align=\"center\" border=\"0\" cellpadding=\"4\" cellspacing=\"0\" width=\"100%\">
<tr>
<th>ID</th>
<th>Movie</th>
<th>Genre</th></tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['genre'] . "</td";
echo "</tr><br />";
echo "</table>";
}
Any help would be greatly appreciated!
EDIT Fixed the problem right after I created this. Removed from while loop and put it under. Fixed.
echo "</table>"; should be moved outside of your while($row = mysql_fetch_array($result))
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['genre'] . "</td";
echo "</tr><br />";
}
echo "</table>";

Categories