How to make usernames display in rows and columns in a Table - php

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.

Related

How to list out two different product's details at the same table with php

I only can list out all the data row by row but I want to list out those 2 products like this from top to bottom, not row by row within
index.php
while ($row = mysqli_fetch_array($results)) {
echo '<td><'.$row['name'].'></td>';
}
server.php
$db = mysqli_connect('localhost','root','','crud');
$results = mysqli_query($db, "SELECT * FROM info");
If I really understand, you want each product to be in a different line. If yes, you could create new rows directly inside your loop like this
while ($row = mysqli_fetch_array($results)) {
echo '<tr><td>'.$row['name'].'</td></tr>';
}
<?php
while($row = mysqli_fetch_array($results)){
$detail_row.="<td>" . $row['items'] . "</td>";
$category_row.="<td>" . $row['items'] . "</td>";
}
?>
<table>
<tr>
<td>Details</td>
<?php echo $detail_row; ?>
</tr>
<tr>
<td>Categories</td>
<?php echo $category_row; ?>
</tr>
</table>

Group list of array in a table [duplicate]

This question already has answers here:
Creating dynamic tables in HTML using MySQL and PHP
(2 answers)
Closed 11 months ago.
I want to show the list of an array in a table, I want to insert each ID in the ID column, each name in the "name" column, etc...
But it's showing all the content in single column, How I can fix it?
It shows like that right now:
[![enter image description here][1]][1]
PHP:
<table class="blueTable">
<thead>
<tr>
<th>ID</th>
<th>USER</th>
<th>MAIL</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="3">
<div class="links">« <a class="active" href="#">1</a> 2 3 4 »</div>
</td>
</tr>
</tfoot>
<tbody>
<?php
$query = $db->query("SELECT * FROM users ORDER by id");
echo "<tr>";
while ($row = $query->fetch_array()) {
echo "<td>" . $row['id'] . "</td>";
echo "<tr><td>" . $row['username'] . "</td></tr>";
echo "<tr><td>" . $row['email'] . "</td></tr>";
}
?>
</tbody>
</table>
[1]: https://i.stack.imgur.com/jPTR8.png
change while() loop code like below:
$query = $db->query("SELECT * FROM users ORDER by id");
while($row = $query->fetch_array()){
echo "<tr>"; //put <tr> opening code inside, not outside
echo "<td>".$row['id']."</td>";
echo "<td>".$row['username']."</td>"; //remove <tr></tr>
echo "<td>".$row['email']."</td>"; //remove <tr></tr>
echo "</tr>"; //</tr> need to be added at last
}
Try this
<?php
$query = $db->query("SELECT * FROM users ORDER by id");
while($row = $query->fetch_array()){
?>
<tr>
<td> <?php echo $row['id'] ?></td>
<td> <?php echo $row['username'] ?> </td>
<td> <?php echo $row['email'] ?> </td>
</tr>
<?php
}
?>

How to add a Checkbox beside each row in table

How to make so a checkbox is added beside each row in a dynamic table? So if i get 5 result from databse i want to display 5 checkbox next to these results.
<table border="2">
<tr>
<th>title</th>
</tr>
<?php
include 'database.php';
$valid_query = "SELECT * FROM agencies ";
$valid_result = mysqli_query($link, $valid_query);
while ($row = mysqli_fetch_array($valid_result)) {
echo '<tr>';
echo '<td>'.$row['title'].'</td>';
<input type = 'checkbox' name = '$row[title]'>
echo '<tr>';
}
?>
</table>

PHP looping with mysql Showing the same username 5 times, instead of all usernames

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();
?>

Updating a row of SQL with html table

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.

Categories