I am trying to make something so it shows the database results. The problem is, it only shows 1 result. I want it to show multiple results using an quick and dirty template system. Also, is there a way to make my system better? Perhaps a class or a function? I need some insight on this. Thanks a bunch!
cp.php
<?php
require("db.php");
chdir("../"); // path to MyBB
define("IN_MYBB", 1);
require("./global.php");
$title = "********";
require("templates/header.php");
if($mybb->user['uid'])
{
$uid = $mybb->user['uid'];
// Active Tournaments
// run queries, do all the brainwork
// lets get the forum name
$query = "SELECT tourneys.name, tourneys.date, tourneys.time
FROM tourneys
INNER JOIN players ON players.tid = tourneys.id
WHERE players.forumname = {$uid}";
$result = mysqli_query($conn, $query);
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$activetournaments = "<td>". $row['name'] ."</td><td>" . $row['date'] . "</td><td>". $row['time'] ."</td>";
// $team = mysqli_query($conn, "SELECT * FROM tourneys WHERE id=" . $row['tid'] . "");
// $playing = mysqli_query($conn, "SELECT `` FROM tourneys WHERE id=" . $row['tid'] . "");
}
}
else
{
$error = "Only registered members may access this area.";
include ('templates/error.php');
}
require ("templates/cp.php")
?>
templates/cp.php
<h2>Welcome back <?=$mybb->user['username']; ?>!</h2>
<?=$postrequirement?>
<h3>Active Tournaments</h3>
<table>
<tr>
<th>Name</th>
<th>Date/time</th>
<th>Team</th>
<th>Playing as</th>
<th>Options</th>
</tr>
<tr>
<?=$activetournaments?>
</table>
<hr />
<h3>Participated Tournaments</h3>
<table>
<tr>
<th>Position</th>
<th>Name</th>
<th>Date/time</th>
<th>Team</th>
<th>Played as</th>
<th>Options</th>
</tr>
<tr>
<?=$participatedtournaments?>
</table>
I have changed $activetournaments to append to the end using '.='.
while($row = mysqli_fetch_assoc($result)) {
$activetournaments .= "<td>". $row['name'] ."</td><td>" . $row['date'] . "</td><td>". $row['time'] ."</td> ";
}
At the moment, for each record, it is overwriting $activetournaments. You could also use $activetournaments as an array then do a while / foreach in the template.
In your cp.php you overwrite $activetournaments in each while execution, change your code to:
<?php
require("db.php");
chdir("../"); // path to MyBB
define("IN_MYBB", 1);
require("./global.php");
$title = "1ShotGG Tournaments | Control Panel";
require("templates/header.php");
if($mybb->user['uid'])
{
$uid = $mybb->user['uid'];
// Active Tournaments
// run queries, do all the brainwork
// lets get the forum name
$query = "SELECT tourneys.name, tourneys.date, tourneys.time
FROM tourneys
INNER JOIN players ON players.tid = tourneys.id
WHERE players.forumname = {$uid}";
$result = mysqli_query($conn, $query);
// output data of each row
$activetournaments = '';
while($row = mysqli_fetch_assoc($result)) {
$activetournaments .= "<td>". $row['name'] ."</td><td>" . $row['date'] . "</td><td>". $row['time'] ."</td>";
// $team = mysqli_query($conn, "SELECT * FROM tourneys WHERE id=" . $row['tid'] . "");
// $playing = mysqli_query($conn, "SELECT `` FROM tourneys WHERE id=" . $row['tid'] . "");
}
}
else
{
$error = "Only registered members may access this area.";
include ('templates/error.php');
}
require ("templates/cp.php")
?>
In your templates/cp.php you can add a </tr> tag here:
<tr>
<?=$activetournaments?>
</tr>
your new templates/cp.php:
<h2>Welcome back <?=$mybb->user['username']; ?>!</h2>
<?=$postrequirement?>
<h3>Active Tournaments</h3>
<table>
<tr>
<th>Name</th>
<th>Date/time</th>
<th>Team</th>
<th>Playing as</th>
<th>Options</th>
</tr>
<tr>
<?=$activetournaments?>
</tr>
</table>
<hr />
<h3>Participated Tournaments</h3>
<table>
<tr>
<th>Position</th>
<th>Name</th>
<th>Date/time</th>
<th>Team</th>
<th>Played as</th>
<th>Options</th>
</tr>
<tr>
<?=$participatedtournaments?>
</table>
Related
I have a select box that shows the names of all the users in the database, however, I need, using a "Find Button" on the selected user on the combo box, that the data attached to that user shows up on the table
Table that currently shows the data of all users
<table class="table table-hover">
<thead class="thead-dark"></thead>
<tr>
<th scope="col">Shift ID</th>
<th scope="col">Name</th>
<th scope="col">Origin</th>
<th scope="col">Destination</th>
<th scope="col">Date</th>
</tr>
</thead>
<?php
global $result, $query;
$sql = "SELECT * FROM shifts";
$result = $db->query($sql);
//Fetch Data form database
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["shift_id"]. "</td><td>" . $row["name"] . "</td><td>" . $row["origin"] . "</td><td>" . $row["destination"] . "</td><td>" . $row["date"] . "</td><td>"
. $row["password"]. "</td></tr>";
}
echo "</table>";
} else { echo "0 results"; }
?>
</table>
And here's the form that shows the users in the select box
<form name="form1" method="POST" action="">
<select name="getUser">
<?php
$res = mysqli_query($db, "SELECT * FROM shifts");
while ($row = mysqli_fetch_array($res))
{
?>
<option><?php echo $row ["name"]; ?></option>
<?php
}
?>
</select>
<button class="btn-primary rounded">Find</button>
</form>
I'm trying to make it that so when the selected user in the combo box and the find button is pressed, that the data found goes all into the table described above.
I was maybe gonna try to attach a variable to the select box and compare it with the names field on the database.
Something like this
$query = "SELECT * FROM shifts WHERE $name == $nameSelected ";
Thanks.
first echo the user id into the option's value
<option value-"<?echo your id?>"><?php echo $row ["name"]; ?></option>
then when your form submits you get get the value from the $_POST
$userId = $_POST['getUser'];
not you can use the variable to query the database, but you should NEVER put it straight in, you should use PDO prepared statements to prevent injection.
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
//something like this
$query = $conn->prepare("SELECT * FROM shifts WHERE id = :id");
$query->bindParam(':id',$userId,PDO::PARAM_INT);
$query->execute()
return $query->fetchAll();// I realised you wanted to get all the shifts so you don want fetchAll(),
notice that in mysql we only use a single = for our comparison unlike php. Also i've changed name to the unique row in the database, as unless your name field is unique how do you know which use called Dan you want?
If you want to do this without re-loading the whole page you will need to look into using Ajax and passing the value of the option tag via jQuery.
Here are some places to start:
https://www.w3schools.com/php/php_mysql_connect.asp
https://www.w3schools.com/xml/ajax_intro.asp
if you are not comfortable with javascript (AJAX), try on your form
<?php $res = mysqli_query($db, "SELECT * FROM shifts"); ?>
<form name="form1" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"
<select name="getUser">
<option value='All'>All</options>
<?php
while ($row = mysqli_fetch_array($res)) { ?>
<option value='$row ["name"]'><?php echo $row ["name"]; ?></option>
<?php } ?>
</select>
<button class="btn-primary rounded">Find</button>
</form>
And in your table
<table class="table table-hover">
<thead class="thead-dark"></thead>
<tr>
<th scope="col">Shift ID</th>
<th scope="col">Name</th>
<th scope="col">Origin</th>
<th scope="col">Destination</th>
<th scope="col">Date</th>
</tr>
</thead>
<?php
global $result, $query;
if ($_POST['getUser'] == 'All'){
$sql = "SELECT * FROM shifts";
} else {
$sql = "SELECT * FROM shifts WHERE name = " . $_POST['getUser'];
}
$result = $db->query($sql);
//Fetch Data form database
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["shift_id"]. "</td><td>" . $row["name"] . "</td><td>" . $row["origin"] . "</td><td>" . $row["destination"] . "</td><td>" . $row["date"] . "</td><td>"
. $row["password"]. "</td></tr>";
}
echo "</table>";
} else { echo "0 results"; }
?>
</table>
As I'm new to PHP, I want to know that how to put data from different database tables into one table form on the page.
My codes so far as below,
<?php
include('DBconnect.php');
mysql_query("USE onlinerecruitment");
$username =$_SESSION['user'];
$result = mysql_query("SELECT * FROM application_data_file");
$rows = mysql_fetch_array($result, MYSQL_ASSOC);
$pos_id = $rows['Position_ID'];
$resultt = mysql_query("SELECT * FROM position WHERE Position_ID = '".$pos_id."' ");
$resulttt = mysql_query("SELECT * FROM resume_data_file WHERE App_Email = '".$pos_id."' ");
?>
<TABLE border ='1'>
<table style="width:100%">
<tr>
<th>Application ID</th>
<th>Applicant E-mail</th>
<th>Position Selected</th>
<th></th>
<th></th>
<th></th>
</tr>
<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC) & $rowss = mysql_fetch_array($resultt, MYSQL_ASSOC)){
echo "<TR>";
echo "<TD>".$row['App_Data_ID']."</TD>";
echo "<TD>".$row['App_Email']."</TD>";
echo "<TD>".$rowss['Position_Name']."</TD>";
echo "<TD><a href='view-app-form.php?app_mail=".$row['App_Email']."'>View Application Data</a></TD>";
echo "<TD><a href='view-resume-form.php?app_mail=".$row['App_Email']."'>View Resume Data</a></TD>";
echo "<TD><a href='view-test-score.php?app_mail=".$row['App_Email']."'>View Testing Score Data</a></TD>";
echo "</TR>";
}
?>
</table>
I will focus the part here.
<TABLE border ='1'>
<table style="width:100%">
<tr>
<th>Application ID</th>
<th>Applicant E-mail</th>
<th>Position Selected</th>
<th></th>
<th></th>
<th></th>
</tr>
<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC) & $rowss = mysql_fetch_array($resultt, MYSQL_ASSOC)){
echo "<TR>";
echo "<TD>".$row['App_Data_ID']."</TD>";
echo "<TD>".$row['App_Email']."</TD>";
echo "<TD>".$rowss['Position_Name']."</TD>";
echo "<TD><a href='view-app-form.php?app_mail=".$row['App_Email']."'>View Application Data</a></TD>";
echo "<TD><a href='view-resume-form.php?app_mail=".$row['App_Email']."'>View Resume Data</a></TD>";
echo "<TD><a href='view-test-score.php?app_mail=".$row['App_Email']."'>View Testing Score Data</a></TD>";
echo "</TR>";
}
?>
</table>
But if there is any problem in the section that I didn't focused, I still appreciate your solution.
Thank you in advance.
To do this you would need to use a JOIN in the sql statement.
mysql_query("SELECT resume_data_file.App_Email, position.Position_ID FROM position INNER JOIN resume_data_file ON position.Position_ID = position.Position_ID WHERE position.Position_ID = '".$pos_id."' ");
http://www.w3schools.com/sql/sql_join.asp
I have a question in relation to displaying PHP tables that should be straight forward but I cannot get my head around it at the moment so any help would be appreciated, basically what I want to do is display a team of players in a table, but display multiple tables of users with their team name display above it.
What I currently have : http://puu.sh/ilUJp/4a6ae5e47b.png
What I am looking to achieve : http://puu.sh/ilUJ8/7756033517.png
<div class="col-lg-6">
<h3>Team Name Goes Here </h3>
<?php
echo "<table class='table table-striped'>";
echo " <thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
";
while($row = mysqli_fetch_array($result)) {
$teamName = $row['teamName'];
$fName = $row['firstName'];
$surName = $row['surName'];
echo "
<tbody>
<tr>
<td>$teamName</td>
<td>$fName</td>
<td>$surName</td>
</tr>
</tbody>
";
}
echo "</table>";
?>
</div>
with my query :
$sql = "SELECT t.teamID,t.teamName,u.firstName,u.surName From users as u INNER JOIN team as t where u.teamID = t.teamID ";
I know the idea I need to do but cannot get it done, so any help would be appreciated.
Try this code
<?php $teemid=array();
while($row = mysqli_fetch_array($result)) {
if(!in_array($row['teamID'],$teemid)){
array_push($teemid,$row['teamID']);
if(!empty($teemid)){ ?>
</tbody>
</table>
</div>
<?php }
?>
<div class="col-lg-6">
<h3><?php echo $row['teamName']; ?></h3>
<table class='table table-striped'>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php } ?>
<tr>
<td><?php echo $row['teamName']; ?></td>
<td><?php echo $row['firstName']; ?></td>
<td><?php echo $row['surName']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
SQL Query Change as below
$sql = "SELECT t.teamID,t.teamName,u.firstName,u.surName From users as u INNER JOIN team as t where u.teamID = t.teamID ORDER BY u.teamID";
You can do this logic
$teams = "get all teams sql query";
while ($row = mysqli_fetch_array($teams)) {
$teamid = $row['teamid'];
$teamname = $row['teamname'];
$teammemberquery = "select all member in the where team = $teamid sql query";
echo "<table>";
while ($r = mysqli_fetch_array($teammemberquery)) {
$teamName = $r['teamName'];
$fName = $r['firstName'];
$surName = $r['surName'];
echo "
<tbody>
<tr>
<td>$teamName</td>
<td>$fName</td>
<td>$surName</td>
</tr>
</tbody>
";
}
echo "</table>";
}
Try as below (Please replace table column name as yours and mysql to mysqli):
<?php
$link = mysql_connect('localhost', 'root', 'root');
$db_selected = mysql_select_db('test', $link);
$sql = "SELECT t.team_id,t.team,u.fname,u.lname,u.email From users as u INNER JOIN team as t where u.team_id = t.team_id order by t.team_id ";
$result = mysql_query($sql);
?>
<html><head><title>team</title></head><body><div class="col-lg-6">
<?php
echo "<table>";
$teamName = "";
$i=0;
while($row = mysql_fetch_array($result))
{
if($teamName == "" || $teamName != $row['team'])
{
if($i!=0)
echo "</table>";
echo "<tr><td colspan='3'><h3>".$row['team']."</h3></td></tr>";
$teamName = $row['team'];
$i=0;
}
$fName = $row['fname'];
$surName = $row['lname'];
$email = $row['email'];
if($i==0)
{
echo "<table class='table table-striped'><tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>";
}
echo "<tr>
<td>$fName</td>
<td>$surName</td>
<td>$email</td>
</tr>";
$i++;
}
echo "</table>";
?>
</div></body></html>
So far, I have this program that links my phpmyadmin database to my php script. Now, I need to display certain things in a table like "all records," "all contacts whose last name starts with S," "all pet owners," etc. My question is: is there a simpler way to insert code into my php script to display the information from my database. Right now I have that long echo statement to display the information. Is there a way I can just use something like a SELECT * statement to display all records and to simplify my code?
<?php
$db_hostname='localhost';
$db_username='root';
$db_password='';
$db_database='Address Book';
$connection = new mysqli( $db_hostname,
$db_username,
$db_password,
$db_database);
if ($connection->connect_error) {
echo "Sorry";
} else {
echo "Connected!<br><br>";
$sql = "SELECT * FROM People";
$result = $connection->query($sql);
if (!$result) die ($connection->error);
$n = $result->num_rows;
for ($i=1; $i<=$n; $i++) {
$row = $result->fetch_array(MYSQLI_ASSOC);
echo "<table>
<tr><th>ID</th><th>First Name</th><th>Last Name</th>
<th>Street Address</th><th>City</th>
<th>State</th><th>Zip Code</th>
<th>Email Address</th><th>Comment</th>
<th>Number of pets</th></tr>";
echo "<tr><td width=20>" . $row['iD'] . "</td><td>" . $row['First Name'] . "</td><td width=40>" .
$row['Last Name'] . "</td><td width=200>" . $row['Street Address'] . "</td><td width=30>" .
$row['City'] . "</td><td width=40>" . $row['State'] . "</td><td width=30>" .
$row['Zip Code'] . "</td><td width=40>" . $row['Email Address'] . "</td><td width=20>" .
$row['Comment'] . "</td><td width=10>" . $row['Number of pets'] . "</td></tr>";
}
echo "</table>";
}
?>
You should a table structure first then insert your PHP codes within the structure. E.g:
<?php
// Put your MYSQLI retrievals codes here
?>
<table>
<tr>
<th>FIELD_1</th>
<th>FIELD_2</th>
<th>FIELD_3</th>
</tr>
<?php
while ($rows = $result->fetch_array(MYSQLI_ASSOC))
{
?>
<tr>
<td><?php echo $rows['field_1']; ?></td>
<td><?php echo $rows['field_2']; ?></td>
<td><?php echo $rows['field_3']; ?></td>
</tr>
<?php
}
?>
</table>
Technically you are doing everything alright, but there are some more sophisticated ways to develop the things you want.
Take a look at the following links:Object oriented programming in PHP and templating in PHP.
Hope this works for you:
<?php
$db_hostname='localhost';
$db_username='root';
$db_password='';
$db_database='Address Book';
$connection = new mysqli($db_hostname,$db_username,$db_password,$db_database);
if ($connection->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
} else {
echo "Connected!<br><br>";
}
$last_name = "Lastname to search"; //You can post or get lastname in here.
/* create a prepared statement */
if ($sql = $connection->prepare("SELECT * FROM People WHERE `Last Name`=?")) {
/* bind parameters for markers */
$sql->bind_param("s", $last_name);
/* execute query */
$sql->execute();
/* instead of bind_result: */
$result = $sql->get_result();
/* close statement */
$sql->close();
}
?>
<table>
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Street Address</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Email Address</th>
<th>Comment</th>
<th>Number of pets</th>
</tr>
</thead>
<tbody>
<?
/* now you can fetch the results into an array */
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td width=20><?=$row['iD'];?></td>
<td><?=$row['First Name'];?></td>
<td width=40><?=$row['First Name'];?></td>
<td width=200><?=$row['Street Address'];?></td>
<td width=30><?=$row['City'];?></td>
<td width=40><?=$row['State'];?></td>
<td width=30><?=$row['Zip Code'];?></td>
<td width=40><?=$row['Email Address'];?></td>
<td width=20><?=$row['Comment'];?></td>
<td width=10><?=$row['Number of pets'];?></td>
</tr>
<?
}
?>
</tbody>
</table>
I have this form to search names in mysql database
<form action="search.php" method="GET">
<input type="text" placeholder="Search" name="name">
<input type="submit" value="Search">
this is the search.php
<?php
name = $_GET['name'];
require_once("connect.php");
$records = $connect->query("SELECT * FROM Userlists WHERE Name = '$name'");
echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Description</th>
</tr>
</thead>
<tbody>";
if (mysqli_num_rows($records)== 0){
echo "No data available for that name specified";
}
else {
while($row=mysqli_fetch_array($records)) {
$name = $row['Name'];
$email = $row['Email'];
$desc = $row['Desc'];
echo
"<tr>
<td>".$name."</td>
<td>".$email."</td>
<td>".$desc."</td>
</tr>";
}
}
echo
"</tbody>
</table>";
?>
so there is no problems when I search for a name that exists in database it displays correctly, but the problem comes when I search for a name that doesn't exist in database.. I want it to display only
"No data available for that name specified" for the output but I will also see empty table in the output like this ------------> IMAGE..
so how can I get rid of the empty table for the output?
Just pu the if outside the table....
<?php
name = $_GET['name'];
require_once("connect.php");
$records = $connect->query("SELECT * FROM Userlists WHERE Name = '$name'");
if (mysqli_num_rows($records)== 0){
echo "No data available for that name specified";
} else {
echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Desc</th>
</tr>
</thead>
<tbody>";
while($row=mysqli_fetch_array($records)) {
$name = $row['Name'];
$email = $row['Email'];
$desc = $row['Desc'];
echo
"<tr>
<td>".$name."</td>
<td>".$email."</td>
<td>".$desc."</td>
</tr>";
}
echo
"</tbody>
</table>";
}
?>
change your if clause as below and remember to add exit() or die() function,this will end your php if there is no any data in database, and if there is any it will then start creating table for once and repeatedly fill up the table rows for given rows of data on database.
if (mysqli_num_rows($records)== 0){
echo "No data available for that name specified";
exit();
} else {
echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Desc</th>
</tr>
</thead>
<tbody>";
while($row=mysqli_fetch_array($records)) {
$name = $row['Name'];
$email = $row['Email'];
$desc = $row['Desc'];
echo
"<tr>
<td>".$name."</td>
<td>".$email."</td>
<td>".$desc."</td>
</tr>";
}
echo
"</tbody>
</table>";
}
Move your echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Desc</th>
</tr>
</thead>
<tbody>";
into the the if statement. This way it will only display the table when data is available!
$name = $_GET['name'];
require_once("connect.php");
$records = $connect->query("SELECT * FROM Userlists WHERE Name = '$name'");
if (mysqli_num_rows($records)== 0){
echo "No data available for that name specified";
}
else {
echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Desc</th>
</tr>
</thead>
<tbody>";
while ($row = mysqli_fetch_array($records)) {
$name = $row['Name'];
$email = $row['Email'];
$desc = $row['Desc'];
echo
"<tr>
<td>" . $name . "</td>
<td>" . $email . "</td>
<td>" . $desc . "</td>
</tr>";
}
echo "</tbody></table>";
}
Try this one. But don't forget to escape $_GET['name'] like htmlspecialchars and real_escape_string