how to place a space between to tables in php - php

How do I place a space between two tables which are vertical. The tables are within the php script:
$result = mysql_query("SELECT * FROM t2 WHERE FIRSTNAME='{$_POST["fname"]}' AND HOSPNUM='{$_POST["hnum"]}'");
echo "<table border='1'>
<tr>
<th>HospNum</th>
<th>RoomNum</th>
<th>LastName</th>
<th>FirstName</th>
<th>MidName</th>
<th>AdmitDate</th>
<th>AdmitTime</th>
<th>Address</th>
<th>TelNum</th>
<th>Civil Status</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['HOSPNUM'] . "</td>";
echo "<td>" . $row['ROOMNUM'] . "</td>";
echo "<td>" . $row['LASTNAME'] . "</td>";
echo "<td>" . $row['FIRSTNAME'] . "</td>";
echo "<td>" . $row['MIDNAME'] . "</td>";
echo "<td>" . $row['ADDATE'] . "</td>";
echo "<td>" . $row['ADTIME'] . "</td>";
echo "<td>" . $row['ADDRESS'] . "</td>";
echo "<td>" . $row['TELNUM'] . "</td>";
echo "<td>" . $row['CSTAT'] . "</td>";
echo "</tr>";
}
echo "</table>";
$result2 = mysql_query("SELECT * FROM t2 WHERE FIRSTNAME='{$_POST["fname"]}' AND HOSPNUM='{$_POST["hnum"]}'");
echo "<table border='1'>
<th>Sex</th>
<th>Age</th>
<th>Birthday</th>
<th>STAT1</th>
<th>STAT2</th>
<th>STAT4</th>
<th>STAT5</th>
<th>STAT6</th>
<th>STAT7</th>
<th>STAT8</th>
</tr>";
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['SEX'] . "</td>";
echo "<td>" . $row['AGE'] . "</td>";
echo "<td>" . $row['BDAY'] . "</td>";
echo "<td>" . $row['STAT'] . "</td>";
echo "<td>" . $row['STAT2'] . "</td>";
echo "<td>" . $row['STAT3'] . "</td>";
echo "<td>" . $row['STAT4'] . "</td>";
echo "<td>" . $row['STAT5'] . "</td>";
echo "<td>" . $row['STAT6'] . "</td>";
echo "<td>" . $row['STAT7'] . "</td>";
echo "<td>" . $row['STAT8'] . "</td>";
echo "</tr>";
}
echo "</table>";

use CSS to apply margin between the two tables:
<style type="text/css">
table{
margin: 10px 0;
}
</style>

Related

Group output of MySql Query

I have the following PHP/MySql:
$sql = "SELECT * from tblDigest";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr>
<th>Diagram</th>
<th>Headcode</th>
<th>Date</th>
<th>Dep Time</th>
<th>Origin</th>
<th>Destination</th>
<th>Arr Time</th>
<th>Booked Traction</th>
<th>Actual Traction</th>
<th>Type</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["diagram"] . "</td>";
echo "<td>" . $row["headcode"] . "</td>";
echo "<td>" . $row["depDate"] . "</td>";
echo "<td>" . $row["depTime"] . "</td>";
echo "<td>" . $row["origin"] . "</td>";
echo "<td>" . $row["destination"] . "</td>";
echo "<td>" . $row["arrTime"] . "</td>";
echo "<td>" . $row["bookedTraction"] . "</td>";
echo "<td>" . $row["actualTraction"] . "</td>";
echo "<td>" . $row["type"] . "</td>";
echo "</tr>";
}
echo "</table>";
}
I would like to group the results by 'diagram', so that I can display them as an accordion. Is there any easy way to do this?
Failing that, how can I get the value for the first row of the table (so I can have a $diagNo variable and compare $row["diagram"] against that to find out when it has changed.
Sort your results by diagram and watch when its value changes:
$sql = "SELECT * from tblDigest ORDER BY diagram";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr>
<th>Diagram</th>
<th>Headcode</th>
<th>Date</th>
<th>Dep Time</th>
<th>Origin</th>
<th>Destination</th>
<th>Arr Time</th>
<th>Booked Traction</th>
<th>Actual Traction</th>
<th>Type</th>
</tr>";
// output data of each row
$oldDiagram = '';
while($row = $result->fetch_assoc()) {
if($oldDiagram == '')
{
// this is the first diagram - create the first accordeon
$oldDiagram = $row['diagram'];
}
elseif($oldDiagram != $row['diagram'])
{
// a new group starts right now - create new accordeon
$oldDiagram = $row['diagram'];
}
echo "<tr>";
echo "<td>" . $row["diagram"] . "</td>";
echo "<td>" . $row["headcode"] . "</td>";
echo "<td>" . $row["depDate"] . "</td>";
echo "<td>" . $row["depTime"] . "</td>";
echo "<td>" . $row["origin"] . "</td>";
echo "<td>" . $row["destination"] . "</td>";
echo "<td>" . $row["arrTime"] . "</td>";
echo "<td>" . $row["bookedTraction"] . "</td>";
echo "<td>" . $row["actualTraction"] . "</td>";
echo "<td>" . $row["type"] . "</td>";
echo "</tr>";
}
echo "</table>";
}

Edit database row using php page

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

HTML table is adding additional row when calling information from database table

I seem to be having an issue with my HTML formatting. I am calling the database to display all information within the table on a webpage. I have got the information coming down into the table. Unfortunately the table is adding an additional row to the bottom of the table making 4 rows when in fact there is only 3 rows displaying in the database.
Any idea as to why?
<?php
//connect to the server
$link = mysql_connect('*****', '*****', '****');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('******');
$query = mysql_query("SELECT * FROM tablename");
echo "<table border='1'>
<tr>
<td>Date</td>
<td>Region</td>
<td>Cameraman</td>
<td>Livestream?</td>
<td>Event Title</td>
<td>Lecturer</td>
<td>Time</td>
<td>Speaker</td>
<td>ICE Contact</td>
<td>Venue Address</td>
<td>Venue Contact</td>
<td>Additional Comments</td>
<td>On App?</td>
</tr>";
WHILE($rows = mysql_fetch_array($query)):
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['additioncomments'] . "</td>";
echo "<td>" . $rows['onapp'] . "</td>";
echo "</tr>";
endwhile;
echo "</table>";
?>
I have added the link to the page below.
http://cpdonline.tv/spreadsheet/spreadsheet.php
Update your while loop with the following:
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['additioncomments'] . "</td>";
echo "<td>" . $rows['onapp'] . "</td>";
echo "</tr>";
}
endwhile;
echo "</table>";
array_filter() function's default behavior will remove all values from array which are equal to null, 0, '' or false.
It seems that you have empty data in your database, as many lines are empty
Check your database for empty records or run the query
DELETE FROM table_name WHERE column='';

Adding autonumber column to php table that reads from mysql

I am trying to add a column before my first column that starts numbering each record, starting from 1. I was trying to add autoincrement to this line echo "" . $row['ss'] . "";, but that does not seem to want to work. Any ideas how to do this?
My code looks like this:
$result = mysqli_query($con,"SELECT * FROM `results` WHERE Event='100' AND Gender='M' ORDER BY Performance ASC");
echo "<table border='0'>
<tr>
<th align='left'>Pos</th>
<th align='left'>Event</th>
<th>Performance</th>
<th>Wind</th>
<th>Place</th>
<th align='left'>Name</th>
<th align='left'>Surname</th>
<th>Age</th>
<th>Team</th>
<th>Meet</th>
<th>Date</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ss'] . "</td>";
echo "<td>" . $row['Event'] . "</td>";
echo "<td>" . $row['Performance'] . "</td>";
echo "<td>" . $row['Wind'] . "</td>";
echo "<td>" . $row['Pos'] . "</td>";
echo "<td width='100' align='left'>" . $row['Surname'] . "</td>";
echo "<td Width='100'>" . $row['Name'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Team'] . "</td>";
echo "<td>" . $row['Meet'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Add this line in header
<th align='left'>#</th>
And here php code
$count = 1;
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $count . "</td>";
echo "<td>" . $row['ss'] . "</td>";
echo "<td>" . $row['Event'] . "</td>";
echo "<td>" . $row['Performance'] . "</td>";
echo "<td>" . $row['Wind'] . "</td>";
echo "<td>" . $row['Pos'] . "</td>";
echo "<td width='100' align='left'>" . $row['Surname'] . "</td>";
echo "<td Width='100'>" . $row['Name'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Team'] . "</td>";
echo "<td>" . $row['Meet'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "</tr>";
//other code
$count=$count+1;
}
A better solution would be to use a variable counter to "number" your rows:
$counter = 1;
while($row = mysqli_fetch_array($result))
echo "<td>" . $counter . "</td>";
$counter++;
}
This is the right way to do it since if that ss column is the mysql auto increment, then your rows will not be numbered by the SORT but rather from the order in which they were inserted into the database regardless of any sorting you apply.
Try to change fetch_array with fetch_assoc

Search a Table using PHP

I have a form on my website that I use to send information to a MySQL table, then I have a PHP file called read.php that retrieves the information from the MySQL table and displays it on a page. This works completely fine and I have no issues with it. However I would now like to add a search function so the table can be searched by things such as first name, last name, email etc.
Here is my read.php file:
<html>
<head>
<title>messages</title>
</head>
<body>
<h1>Message Center</h1>
<?php
$con = mysqli_connect('localhost', 'removed', 'removed', 'removed');
$result = mysqli_query($con,"SELECT*FROM specialisttable");
echo"<table border='1'>
Specialist Table
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Phone</th>
<th>Email</th>
<th>Website</th>
<th>Speciality</th>
<th>Street Name</th>
<th>Suburb</th>
<th>City</th>
<th>Post Code</th>
<th>Comments</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
{
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['website'] . "</td>";
echo "<td>" . $row['speciality'] . "</td>";
echo "<td>" . $row['streetname'] . "</td>";
echo "<td>" . $row['suburb'] . "</td>";
echo "<td>" . $row['city'] . "</td>";
echo "<td>" . $row['postcode'] . "</td>";
echo "<td>" . $row['comments'] . "</td>";
echo "</tr>";
}
}
echo"</table>";
mysqli_close($con);
?>
<br>
<br>
Click to return to homepage
</body>
</html>
I would to like search by things such as first name, email and city so for example if I have an entry with the name John and I type J into the first name search bar John and his other information would show in the table as well as anyone else with J in their name.
Also the same thing for something such as city, if I type auck then all the entries with auck in the city section would show as well as the other information.
I'm unsure of how I would do this, I've managed to work out how to search for first name using $search = $_GET['search']; and then using:
while($row = mysqli_fetch_array($result))
{
if($search=="")
{
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['website'] . "</td>";
echo "<td>" . $row['speciality'] . "</td>";
echo "<td>" . $row['streetname'] . "</td>";
echo "<td>" . $row['suburb'] . "</td>";
echo "<td>" . $row['city'] . "</td>";
echo "<td>" . $row['postcode'] . "</td>";
echo "<td>" . $row['comments'] . "</td>";
echo "</tr>";
}
else if($row['firstname']==$search)
{
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['website'] . "</td>";
echo "<td>" . $row['speciality'] . "</td>";
echo "<td>" . $row['streetname'] . "</td>";
echo "<td>" . $row['suburb'] . "</td>";
echo "<td>" . $row['city'] . "</td>";
echo "<td>" . $row['postcode'] . "</td>";
echo "<td>" . $row['comments'] . "</td>";
echo "</tr>";
}
I can do the same thing for last name and any other field but it makes the code very messy and I don't think it's the most efficient way of doing things.
If someone could please point me in the right direction on how to efficiently make a search function I would greatly appreciate it.
Thank you very much.

Categories