I'm trying to display data from my SQL database unto this page. When I run my code with only one data in the SQL database so if there is only one Wifi to fetch then everything looks normal and works well, but as soon as the user adds another wifi then the table is drawn again on top of the other table
A picture with only one wifi in my database
A picture with only two wifi in my database
Here is my wifi_page.php:
<?php
require_once 'header.php';
require_once 'connect.php';
$sql = "SELECT ID, Name, Password FROM wifi";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$newWifi = "wifi_register_page.php";
echo"
<table>
<tr>
<td>Name</td>
<td>Password</td>
<td>+
</td></tr>
<tr>
<td> ". $row["Name"]. "</td>
<td>". $row["Password"]. "</td>
<td></td>
</tr>
</table>";
}
} else {
echo "0 results";
}
$mysqli->close();
require_once 'footer.php'
?>
If you need any more code or images just let me know in the comments. Thank you in advance!
You need to take the table and the header out of the while, otherwise it will create a new table for each row of your database.
<?php
require_once 'header.php';
require_once 'connect.php';
$sql = "SELECT ID, Name, Password FROM wifi";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
$newWifi = "wifi_register_page.php";
echo "
<table>
<tr>
<td>Name</td>
<td>Password</td>
<td>+
</td>
</tr>
";
// output data of each row
while($row = $result->fetch_assoc()) {
echo"
<tr>
<td> ". $row["Name"]. "</td>
<td>". $row["Password"]. "</td>
<td></td>
</tr>
";
}
echo '</table>';
} else {
echo "0 results";
}
$mysqli->close();
require_once 'footer.php'
?>
Related
I am outputting newsletter email registrations.
How can I format them into tables and what edits can I do so that I can drop that row also.
My PHP code is :
<?php include "navbar-datacheck.php" ?>
<?php
include "../backend/db.php";
$sql = "SELECT * from newsletter";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
print "id: " . $row["id"]. " - Email ID: " . $row["email"]."<br>";
}
} else {
echo "0 results";
}
$mysqli->close();
?>
Construct your table comprising of all the newsletter details in the following manner,
// your code
if ($result->num_rows > 0) {
// output data of each row
?>
<table>
<tr>
<td>ID</td>
<td>Email</td>
<td>Action</td>
</tr>
<?php
while($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["email"]; ?></td>
<td>Delete</td>
</tr>
<?php
}
?>
</table>
<?php
} else {
echo "0 results";
}
// your code
... what edits can I do so that I can drop that row also.
As you can see, I have added a third column Action in your table so that you could delete a row of your choice at any given time. So once the user clicks on Delete link, this is how you should process and delete the corresponding record in delete.php file.
if(isset($_GET['id']) && !empty($_GET['id'])){
$id = $_GET['id'];
// Now delete the corresponding record from the table
}
I have this table im trying to display users, being 2 users per 2 columns, then list down. Here is what i have so far:
<?php $result = mysql_query("SELECT * from users WHERE adminlevel='5'");
while($row = mysql_fetch_array($result)) { echo
" <table>
<tr>
<td width='85' align='left'><br><center>". $row['username'] . "</center>
</td>
<td align='right'><center></center>
</td>
</tr>
<td width='85' align='left'><center></center>
</td>
<td align='right'><center></center>
</td>
</table>";
} ?>
This just displays the members as rows going down, and missing out the other column completely. I was hoping that it would display the username in each of the table fields. I also did try putting ". $row['username'] ." in the other fields too, but that just duplicated it.
EDIT:
So iv'e changed it to this, I can't see going down as I only have 2 members, Would this work:
<?php $result = mysql_query("SELECT * from users WHERE adminlevel='5'"); ?>
<table>
<tr>
<?php while($row = mysql_fetch_array($result)) { echo
"<td width='85' align='left'><font size='1px'><center>". $row['username'] . "</font></center></td>
<td align='right'><center></center></td>";
} ?>
</tr>
</table>
example:
try something like this
<table>
<tr>
<th>name</th>
<th>name</th>
</tr>
<?php
$result = mysql_query("SELECT * from users WHERE adminlevel='5'");
$i = 0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ($i == '0') echo "<tr>";
echo "<td>{$row['username']}</td>";
if ($i == '1') echo "</tr>";
$i++;
if($i =='2')$i='0';
}
?>
</table>
I think you're asking why the other fields in your "user" mysql table aren't showing up.
They aren't there because you only asked for the username:
$row['username']
If you have first/last name in your mysql table, you can retrieve it in the same way:
$row['firstname']
$row['lastname']
In your code you got the row as a key/value array like this:
$row = mysql_fetch_array($result)
The "key" is the name of the mysql column, like username, lastname, firstname. And the value is what is stored in the mysql table under that row/column, like joecool, smith, joe.
I have a foreach loop to show all of the usernames from my database.
When I run the loop I get, '10' is the rank.
Daniel 10
Daniel 10
Daniel 10
Daniel 10
Daniel 10
This is the loop that I have
<?php
include_once 'dbconfig.php';
if(!$user->is_loggedin())
{
$user->redirect('index.php');
}
$user_id = $_SESSION['user_session'];
$stmt = $DB_con->prepare("SELECT * FROM users ");
$stmt->execute(array(":user_id"=>$user_id));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
?>
<h1>Players</h1>
<table>
<tr>
<th>Battletag</th>
<th>Preferred Role</th>
<th>Rank</th>
</tr>
</tr>
<?php var_dump($row)?>
<?php foreach($userRow as $row): ?>
<td> <?php print($row['user_name']); ?></td>
<td> <?php print($row['user_rank']); ?></td>
</tr>
<?php endforeach; ?>
Home
</table>
I don't see why it is looping the 1 user 5 times, instead of looping through all users once
OK 3 things. First, you don't need parameter binding in
$stmt->execute(array(":user_id"=>$user_id));
so, instead of that, use:
stmt->execute();
Next, instead of fetch, use fetchAll function. You need this in order to get all rows for your query from database, instead of only first one.
And finally, in the loop the problem is that you are using $userRow instead of $row inside for loop. Try:
<?php foreach($userRow as $row): ?>
<td> <?=$row['user_name']?></td>
<td> <?=$row['user_rank']?></td>
</tr>
<?php endforeach; ?>
I think this might work for you. It's not a for each loop though. If you're set on using a for each loop then disregard.
$sql = "SELECT user_name, user_rank,
FROM XXX//your database//";
$result = $conn->query($sql);
//Display results
if ($result->num_rows > 0) {
echo '<table>
<tr>
<th>User Name</th>
<th>User Rank</th>
</tr>';
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>" . $row["user_name"]. "</td>
<td>" . $row["user_rank"]. "</td>
</tr>";
}
echo "</table>";
} else {
$message = "0 results";}
$conn->close();
?>
I have the following code:
$sql = "SELECT * FROM Tickets WHERE stat='Open'";
$result = mysql_query($sql);
mysql_close($con);
?>
<!DOCTYPE>
<html>
<body>
<table class="striped">
<tr class="header">
<td>Username</td>
<td>Title</td>
<td>Description</td>
<td>Admin Name</td>
<td>Category</td>
<td>Status</td>
<td>Urgency</td>
<td>Time In</td>
<td> </td>
</tr>
<?php
while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>".$row[username]."</td>";
echo "<td>".$row[title]."</td>";
echo "<td>".$row[description]."</td>";?>
<td><select>
<?php
echo "<option value'".$row[admin_name]."'>".$row[admin_name]."</option>";
$sql = mysql_query("SELECT username FROM Users WHERE user_type='admin'");
while ($u = mysql_fetch_array($sql)){
echo "<option value='".$u['username']."'>".$u['username']."</option>";
}
?>
</select></td>
<?php
echo "<td>".$row[category]."</td>";
echo "<td>".$row[stat]."</td>";
echo "<td>".$row[urgency]."</td>";
echo "<td>".$row[time_in]."</td>";
echo "<td><a href='close.php'>Close Ticket</a></td>";
echo "</tr>";
}
?>
</table>
<a href='update.php'>Update</a>
</body>
</html>
I have two links on this page. Both of them need to update a SQL database. The Close ticket link needs to just update the single row, while the update link should update all of them. I am not sure how to get the info from one php to the next. It seems like you can put the individual row information into a Post array for the close ticket link, but I am not sure how. For the update link it needs to take the value of the dropdown in the table and change the admin_name field to that value.
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>