PHP output sorting - php

Below is a part of my PHP code where it echos the result in an array format. I'm trying to make it so it posts the result in a table. What should I do?
//-display the result of the array
echo "<ul>\n";
echo "<li>" . "<a href=\"search_search.php?id=$ID\">" .
$FirstName . " " . $LastName . " " . $Email . "</a></li>\n";
echo "</ul>";

Do something like for html table-
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "</tr>";
}
echo "</table>";

Go back to basics, http://www.w3schools.com/php/php_mysql_select.asp
Look at Display the Result in an HTML Table.

Related

i have 2 table in db i want to print repeated value only one time

this is my php code for sql query its giving me same value multi time
if (isset($_GET["q"]))
{
$q= $_GET["q"];
$link=mysqli_connect("localhost","root","");
mysqli_select_db($link,"onesports");
$result=mysqli_query($link,"select players.full_name, team.* from players,team where team.team_name='" .$q. " ' and players.team_name=team.team_name ");
echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<a href=>" . $row['full_name'] . "</a>";
echo "<td>" . $row['image'] . "</td>";
echo "<td>" . $row['team_name'] . "</td>";
echo "<td>" . $row['prov'] . "</td>";
echo "<td>" . $row['city'] . "</td>";
echo "<td>" . $row['captain'] . "</td>";
echo "<td>" . $row['coach'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
else {
echo "no result found.";
}
its giving me this output
enter image description here
i want only player name and remaining value want for one time
You should use INNER JOIN in this case.
just update your code with below code.
$result = mysqli_query($link, "SELECT `players`.full_name,`team`.* FROM `players` INNER JOIN `team` ON `players`.team_name= `team`.team_name AND `team`.team_name='".$q."'");

For loop not working in PHP script

I have written a PHP script to pull data from a SQL database to display on a webpage. Everything seems fine but when I run the script it throws the error:
Parse error: syntax error, unexpected ')', expecting ';' in db_data.php on line 21
Line 21 is my FOR loop, I have checked the syntax of the loop and it seems to be correct so I can't understand why it is failing.
$result = mysqli_query($con,"SELECT * igi");
echo "<table border='1'>
<tr>
<th>Ref</th>
<th>Nameame</th>
<th>Location</th>
<th>Email</th>
<th>Issue</th>
<th>Urgency</th>
</tr>";
for($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['REF'] . "</td>";
echo "<td>" . $row['NAME'] . "</td>";
echo "<td>" . $row['LOCATION'] . "</td>";
echo "<td>" . $row['EMAIL'] . "</td>";
echo "<td>" . $row['ISSUE'] . "</td>";
echo "<td>" . $row['URGENCY'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
Change for to while
<?php
// Missing FROM here vv
$result = mysqli_query($con,"SELECT * FROM igi");
echo "<table border='1'>
<tr>
<th>Ref</th>
<th>Nameame</th>
<th>Location</th>
<th>Email</th>
<th>Issue</th>
<th>Urgency</th>
</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['REF'] . "</td>";
echo "<td>" . $row['NAME'] . "</td>";
echo "<td>" . $row['LOCATION'] . "</td>";
echo "<td>" . $row['EMAIL'] . "</td>";
echo "<td>" . $row['ISSUE'] . "</td>";
echo "<td>" . $row['URGENCY'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
As Anthony Thompson and RJParick says, you have to change for to while. The error you're encountering is due tu for that need 3 params like this :
for($i; $i<10; $i++) {//Note the ; used to separate params.
//do something
}
So your loop would begin like this :
while($row = mysqli_fetch_array($result)){
Another detail, your SQL isn't correct use SELECT * FROM igi instead.

Adding up array elements in php table

i wanted to ask I would I add up the points of all the drivers in the table produced below by the following code as this only displays points of all the drivers in each row ... There is a table Points which has points, position and a table Drivers having forename, surname.
<?php
session_start();
if ( !isset($_SESSION['loggedin']) ) {
header("Location: login.php");
}
$con=mysqli_connect("xxxxx","xxxxx","xxxxx","xxxx");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT Drivers.forename, Drivers.surname, Teams.name, Teams.engine, Points.points
From Drivers, Teams, Points");
echo "<table border='1'>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Team Name</th>
<th>Engine</th>
<th>Points</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['forename'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['engine'] . "</td>";
echo "<td>" . $row['points'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
in your code, before the $while
$counter=0
while($row = mysqli_fetch_array($result))
{
$counter += $row['points'];
echo "<tr>";
echo "<td>" . $row['forename'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['engine'] . "</td>";
echo "<td>" . $row['points'] . "</td>";
echo "</tr>";
}
echo $counter; //echoes sum of points

How to create divs / tiles from an sql table

apologies if this sounds noobish, not a lot of experience in web development flash aside, and trying to find the best solution for my project.
I have an sql table from which I can echo data like this in my php file:
echo "<table border='1'>
<tr>
<th>username</th>
<th>objectname</th>
<th>url</th>
<th>description</th>
<th>picture</th>
<th>price</th>
<th>location</th>
<th>category</th>
<th>priority</th>
<th>event</th>
<th>tags</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['objectname'] . "</td>";
echo "<td>" . $row['url'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . $row['picture'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['priority'] . "</td>";
echo "<td>" . $row['event'] . "</td>";
echo "<td>" . $row['tags'] . "</td>";
echo "</tr>";
}
echo "</table>";
This pulls the whole SQL table for that user. I want to echo the content of each row separately in a separate div so that I can vizualize / echo it using tiles from the isotope jquery plug in. http://isotope.metafizzy.co/
What would be the best way to dynamically generate a number of divs based on the number of rows, and fill them in with each row data?
Again, sorry if this is too broad...
Thanks!
if your sql table id has auto increment then you can use that for div seperation or you can use like below
$i = 1;
while($row = mysqli_fetch_array($result))
{
echo "<div id='".$i."'>";
echo "username:" . $row['username'] . "<br />";
echo "objectname:" . $row['objectname'] . "<br />";
echo "url:" . $row['url'] . "<br />";
echo "description:" . $row['description'] . "<br />";
echo "picture:" . $row['picture'] . "<br />";
echo "price:" . $row['price'] . "<br />";
echo "</div>";
$i++;
}
if you want rows in different divs then simply
while($row = mysqli_fetch_array($result))
{
echo "<div>";
echo "<div>" . $row['username'] . "</div>";
// SOME OTHER RECORDS
echo "</div>";
}
additionally you can add ID to divs
while($row = mysqli_fetch_array($result))
{
echo '<div id="sample_'.$row[id].'">';
echo "<div>" . $row['username'] . "</div>";
// SOME OTHER RECORDS
echo "</div>";
}

Putting a link in a php/html table which uses mysql

can anyone help?
Apologies if I'm being stupid but I'm very new to this and I don't really know what I'm doing...
I have generated a table which uses php to get data from a mysql database, but I want to put an extra column at the end (of every row) which contains a link. The link will be to further details about that entry.
The code I have which displays the table is as follows:
$result = mysql_query("SELECT * FROM orders");
echo "<table border='5'>
<tr>
<th>order_no</th>
<th>ord_date</th>
<th>est_completion_date</th>
<th>status</th>
<th>invoice_date</th>
<th>inv_amount</th>
<th>name</th>
<th>fName</th>
<th>lName</th>
</tr>";
// -- Use 'while' to check each row in $result in turn:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['order_no'] . "</td>";
echo "<td>" . $row['ord_date'] . "</td>";
echo "<td>" . $row['est_completion_date'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['invoice_date'] . "</td>";
echo "<td>" . $row['inv_amount'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['fName'] . "</td>";
echo "<td>" . $row['lName'] . "</td>";
echo "</tr>";
}
echo "</table>";
Like I say, I'm a beginner. Basically I am reasonably happy (using the code above) in making html tables which display the results of a mysql query. However I need the users to be able to click on rows/cells in order link to other tables.
Any help much appreciated...
echo "<table border='5'>
<tr>
<th>order_no</th>
<th>ord_date</th>
<th>est_completion_date</th>
<th>status</th>
<th>invoice_date</th>
<th>inv_amount</th>
<th>name</th>
<th>fName</th>
<th>lName</th>
<!-- extra column here -->
<th> </th>
</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['order_no'] . "</td>";
echo "<td>" . $row['ord_date'] . "</td>";
echo "<td>" . $row['est_completion_date'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['invoice_date'] . "</td>";
echo "<td>" . $row['inv_amount'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['fName'] . "</td>";
echo "<td>" . $row['lName'] . "</td>";
// add link here
echo "<td><a href=''>link</a></td>";
echo "</tr>";
}
Please note: You should stop using mysql_* functions. They're being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you're not sure which one to use, read this article.
try this. add a column. It will link to another page "view_more_details.php". To identify rows uniquely, pass a unique id with this link. Here I passed order_no. In the
"view_more_details.php" page, you can select this value by using $_GET['key']
$result = mysql_query("SELECT * FROM orders");
echo "<table border='5'>
<tr>
<th>order_no</th>
<th>ord_date</th>
<th>est_completion_date</th>
<th>status</th>
<th>invoice_date</th>
<th>inv_amount</th>
<th>name</th>
<th>fName</th>
<th>lName</th>
<th> </th>
</tr>";
// -- Use 'while' to check each row in $result in turn:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['order_no'] . "</td>";
echo "<td>" . $row['ord_date'] . "</td>";
echo "<td>" . $row['est_completion_date'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['invoice_date'] . "</td>";
echo "<td>" . $row['inv_amount'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['fName'] . "</td>";
echo "<td>" . $row['lName'] . "</td>";
echo "<td>" . "View More" . "</td>";
echo "</tr>";
}
echo "</table>";

Categories