How to Display Only One Column of a Result in Table - php

I am new to PHP and I am having trouble displaying results from a MySQL query.
So I have a table called Teams, and it contains two fields per entry:
Name, and Continent.
I send a query to the MySQL database to grab all the records where the Continent field equals "Asia." Then I am trying to print only the names of these teams in an HTML table.
Right now I can get the desired names, however my table has an extra empty column on the right and I don't understand how to get rid of it.
Here is my code:
$query =
"SELECT name from Teams WHERE continent='Asia'";
$result = mysql_query($query);
if(!$result){
echo 'Retreive Query Fail' . mysql_error();
}
?>
<div style="text-align: center; color: #000000">
Teams in Asia
</div>
<table>
<caption>
<dfn title="my table"></dfn>
</caption>
<tr>
<th>Team Names</th>
</tr>
<?php
while($row=mysql_fetch_array($result)){
echo '<tr>';
echo '<td>' . $row["name"] . '<td>';
echo '</tr>';
}
?>
</table>
And here is a picture of the result, the thing I want to get rid of is those boxes on the right of the table. (Please ignore the horrendous color scheme / layout. I have never done web design before.)

Close your <td> tag properly instead of adding another one:
while($row=mysql_fetch_array($result))
{
echo '<tr>';
echo '<td>' . $row["name"] . '</td>';
// ^ This guy.
echo '</tr>';
}

echo '<td>' . $row["name"] . '<td>';
should be
echo '<td>' . $row["name"] . '</td>';

Related

Update value if the checkbox if checked multiple rows php

I am trying to update each row with a value of 1 if the checkbox is checked and do nothing if not.
$query= "SELECT Name, Surname, Age, Club, age_group, School, team_select FROM players WHERE Age < 9";
$statement = $db->prepare($query);
$statement->execute();
$players = $statement->fetchAll(PDO::FETCH_ASSOC);
echo '<form nethod="POST" action="add_to_team.php">';
echo '<p align="center"><a href="new.php" >Add Player</a></p>';
echo '<table class="table table-bordered"';
echo '<tr><th>Name</th>
<th>Surname</th>
<th>Club</th>
<th>Age Group</th>
<th>School</th>
<th>Edit</th>
<th>Delete</th>
<th>Add to Team</th></tr>';
// loop through results of database query, displaying them in the table
foreach ($players as $player) {
// echo out the contents of each row into a table
echo '<tr>';
echo '<td>' . $player['Name'] . '</td>';
echo '<td>' . $player['Surname'] . '</td>';
echo '<td>' . $player['Club'] . '</td>';
echo '<td>' . $player['age_group'] . '</td>';
echo '<td>' . $player['School'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo '<td><input type="checkbox" name="team_select" value="1"> </td>';
echo '</tr>';
}
echo '</table>';
echo'<input type="submit" value="Submit" name="submit"><br/>';
Code (It just won't work)
<?php
include 'connect.php';//database connection
isset($_POST['team_select'])
?>
You need to give the checkboxes different names. If they're all named team_select there will just be one $_POST['team_select'], but you won't be able to tell which checkboxes were checked.
Use name="team_select[]" and they'll all be put into an array. Then you can put the player name into the value, so the array will contain the names of all the players who should be added.
echo '<td><input type="checkbox" name="team_select[]" value="' . $player['Name'] . '"> </td>';
When you're processing the form, you can do:
foreach ($_POST['team_select'] as $name) {
...
}
to process all the players who were selected.

How do I organize data in a PHP search query?

I'm in the process of making a PHP website, where a user can input data, then search through it later, but I can't seem to organize the data :/
Heres my code:
<form action="uploads.php" method="GET"><input id="search" type="text" placeholder="Type here"><input id="submit" type="submit" value="Search"></form></body></html>
When a user searched for their name, it results as so:
firstname=Mickey lastname=Mouse item1= item2= item3= item4= item5= item6=
Is there any way I can add a CSS or something to get the entries to line break or seperate?
In Your display page while displaying the data you can use the <br> tag so that it will display each and every data in different line.
First Name: <?php echo $loopvariable['fname'].'<br />'; ?>
Last Name: <?php echo $loopvariable['lname'].'<br />'; ?>
Like this you can provide for all the data which you print.
Output:
First Name: Name One
Last Name: Name Two
And you can provide as such information using the break tags in separate lines.
Basically in PHP you can echo html code so you can echo <br> statements as in the previous answer you can also echo css and you can create a block of code that you include
<?php
include('SomeMoreCode.php');
?>
You can also echo formatting commands to create a table. Just place the html statements in'' and join html and mysql/php values with .
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>date</th> <th>Home Team</th> <th></th><th></th><th>Away Team</th> <th></th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['date'] . '</td>';
echo '<td>' . $row['hometeam'] . '</td>';
echo '<td>' . $row['fthg'] . '</td>';
echo '<td>' . $row['ftag'] . '</td>';
echo '<td>' . $row['awayteam'] . '</td>';
echo '<td>Edit</td>';
echo "</tr>";
}
echo "</table>";
non mysql - php only
<?php
$date='19/6/16';
$hometeam='Man United';
$fthg='3';
$ftag='2';
$awayteam='Man City';
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>date</th> <th>Home Team</th> <th></th><th></th><th>Away Team</th> </tr>";
echo "<tr>";
echo '<td>' . $date . '</td>';
echo '<td>' . $hometeam . '</td>';
echo '<td>' . $fthg . '</td>';
echo '<td>' . $ftag . '</td>';
echo '<td>' . $awayteam . '</td>';
echo "</tr>";
echo "</table>";
?>

Using PHP and SQL to populate array(s) based on a table value

I'm wondering how to populate array(s) with data retrieved from an SQL database and outputting this into a HTML table.
I'm using the following table structure
CREATE TABLE PROJECT_RECORDS (
RECORDS_ROWID number(3) not null,
RECORDS_LISTCOLUMNID number (3) not null,
RECORDS_RECORDVALUE varchar2 (25),
constraint PROJECT_RECORDS primary key(RECORDS_ROWID, RECORDS_LISTCOLUMNID));
Sample Data
RECORDS_ROWID RECORDS_LISTCOLUMNID RECORDS_RECORDVALUE
------------- -------------------- --------------------
1 1 Sample
1 2 Sample description
2 1 Data
2 2 Data Description
From this I want to populate a html table e.g.
|Sample | Sample description |
|Data | Data Description |
I have currently only been able to select the data and place into a single array with all of the RECORDS_RECORDVALUE into a single table column which isn't what I want.
I believe you would populate an array using a loop for each RECORDS_ROWID and then echo this into a table. Any help would be greatly appreciated.
First, you need a connection to your database, so:
<?php
$con=mysqli_connect("localhost","your username","your password","your database");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Then, make a variable with your MySQL Query for the information you want to pull up, so:
$result = mysqli_query($con,"SELECT * FROM project_records");
That will query your database. Now, echo out your query in table form:
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>Sample</th> <th>Sample Description</th></tr>";
while($row = mysqli_fetch_array($result))
{
echo '<td>' . $row['name of your desired outputted column'] . '</td>';
echo '<td>' . $row['name of your other desired outputted column'] . '</td>';
echo "</tr>";
}
echo "</table>";
?>
That should work. Now I just made a two column table, but I assume you know enough html to make the exact table you want.
Replace the code in the echo tag the field name..
<table border="1" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td>Sample</td>
<td>Sample Description</td>
<td>Data</td>
<td>Data Description</td>
</tr>
<?php
$stmt = mysql_query("SELECT * from PROJECT_RECORDS");
while ($data = mysql_fetch_array($stmt))
{
<tr>
<td><?php echo $data['Sample'];?></td>
<td><?php echo $data['Sample_Description'];?></td>
<td><?php echo $data['Data'];?></td>
<td><?php echo $data['Data_Description'];?></td>
</tr>
<?php } ?>
</table>
The following code could solve your problem.
<?php
$link=mysqli_connect("host","username","password","database") or die("Error " . mysqli_error($link));
$result = mysqli_query($link,"SELECT * FROM PROJECT_RECORDS");
$counter = 1;
echo "<table border='1'>";
echo "<tr> <th>Serial No.</th><th>Row ID</th> <th>Column ID</th> <th>Sample Description</th></tr>";
while($row = mysqli_fetch_assoc($result)) {
echo '<td>' . $counter .'</td>'.
echo '<td>' . $row['RECORDS_ROWID'] . '</td>';
echo '<td>' . $row['RECORDS_LISTCOLUMNID'] . '</td>';
echo '<td>' . $row['RECORDS_RECORDVALUE'] . '</td>';
echo "</tr>";
}
echo "</table>";
?>

Show records from MySQL Database

I have this PHP page which is supposed to be a list of employees with their positions..
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View Records</title>
</head>
<body>
<?php
// connect to the database
include('connection.php');
?>
<?php
// get results from database
$result = "SELECT employees.id, CONCAT( fname, lname ) AS FullName, employees.hphone, employees.cphone, employees.email, position.pos\n"
. "FROM employees\n"
. "INNER JOIN position ON employees.posid = position.id\n"
. "ORDER by employees.id ASC LIMIT 0, 30 ";
or die(mysql_error());
// display data in table
echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>";
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>Employee Name/th> <th>Home Phone</th> <th>Cell Phone</th> <th>Email</th> <th>Position</th> </tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['employees.id'] . '</td>'
echo '<td>' . $row['FullName'] . '</td>';
echo '<td>' . $row['employees.hphone'] . '</td>';
echo '<td>' . $row['employees.cphone'] . '</td>';
echo '<td>' . $row['employees.email'] . '</td>';
echo '<td>' . $row['position.pos'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete </td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
<p>Add a new record</p>
</body>
</html>
But I can only get a white page..no errors no nothing....can someone help me please..i am using linux mysql and PHP....i know the sql works because i can get the records from it via MyPHPAdmin..
Please help.
Try this:
$con=mysqli_connect("example.com","peter","abc123","my_db");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT employees.id, CONCAT( fname, lname ) AS FullName, employees.hphone, employees.cphone, employees.email, position.pos\n"
. "FROM employees\n"
. "INNER JOIN position ON employees.posid = position.id\n"
. "ORDER by employees.id ASC LIMIT 0, 30 ";
$result = mysqli_query($con,$sql);
// display data in table
echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>";
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>Employee Name/th> <th>Home Phone</th> <th>Cell Phone</th> <th>Email</th> <th>Position</th> </tr>";
// loop through results of database query, displaying them in the table
while($row = mysqli_fetch_array($result)) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['employees.id'] . '</td>';
echo '<td>' . $row['FullName'] . '</td>';
echo '<td>' . $row['employees.hphone'] . '</td>';
echo '<td>' . $row['employees.cphone'] . '</td>';
echo '<td>' . $row['employees.email'] . '</td>';
echo '<td>' . $row['position.pos'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete </td>';
echo "</tr>";
}
// close table>
echo "</table>";
At first glance, you did not run $result = mysqli_query($conn, $some_query_string_here) and are calling or die() on a string declaration. Since we cannot see what is in your connection.php file we don't see what you do there, but your issue is you don't execute a query on the SQL string and assign that to result, you just assign the string to result.
To troubleshoot in future, you should enable errors on your server:
Find the php.ini file on your computer/server and set these as shown:
error_reporting = E_ALL & ~E_NOTICE | E_STRICT
display_errors = On
Restart your web server ( Apache or Nginx or PHP-FPM depending on how you set it up )
View errors on the screen and fix them one at a time until it works.
Alternate way to enable error reporting is .htaccess file in your script directory or a parent dir with the following php_value error_reporting 0
If you cannot view errors then comment out the php lines in your code, save, then refresh your browser until page renders. The last thing you commented out is typically the culprit and then debug.

Display MySQL data when linking to new page

I have a php script that connects to my database and returns results: name, and price, however, now I am trying to also create a link - that opens a new php page "more_info.php" and displays the correct description that corresponds to correct name.
I have been trying to accomplish this, but I haven't been able to get it working.
while($row = mysqli_fetch_array($result)) //mysql_fetch_array bring back an object, in this case the $result of the mysql query and puts it into a variable $row
{
echo "<td align='center'>" . $row['name'] . "</td> <td align='center'> <input type='button' value='More Info'; onclick=\"window.location='?start=' . $row['name.description'] . ?more_info.php?';\"> </td>";
within the onclick, I want the new window to open up, and bring in the description that matches the name, into the more_info.php
I input that code, but obviously it isn't working. Does this look like i'm on the right path at all?
Change it like this.
echo '<table>';
while($row = mysqli_fetch_array($result))
{
echo '<tr>';
echo '<td align="center">' . $row['name'] . '</td>';
echo '<td align="center">';
echo '<input type="button" value="More Info" onclick="window.location=\'more_info.php?start=' . urlencode($row['description']) . ' \';" />';
echo '</td>';
echo '</tr>';
}
echo '</table>';
and read it in more_info.php like:
<?php
$start = (!empty($_GET['start']) ? $_GET['start'] : false);
echo '<h1>Info about: ' . $start . '</h1>';
?>
shouldn't it be:
window.location='more_info.php?start=' . $row['name.description']
an alternative to using JS triggers on buttons for navigation is you could also just use a link and css style it to look like a button <a href="more_info.php?start=' . $row['name.description'].'" class="button">

Categories