How do I make a member list with PHP and MySQL?
I have user accounts, login and that stuff. How do I make a page that shows all the members?
I can provide code!
NOTE: Your question is not clear. You have not included any code or query snippet. I will try my best to answer your question.
As per my knowledge I think your users are nothing but the members. Just write a query to pull all the users from the users table and display.
NOTE : I will use mysqli_* function without any escaping please help yourself.
<?php
include_once 'db_connect.php'; /Line to include the database connection file, which had $link as resource */
$membersQuery = mysqli_query($link, "SELECT * FROM users");
$members = array();
if(mysqli_num_rows($membersQuery) > 0){
while($row = mysqli_fetch_assoc($membersQuery)){
$members[] = $row; //Get all the members row by row and store in $members array
}
}
?>
<table>
<thead>
<tr>
<th>Firstname</th>
</tr>
</thead>
<tbody>
<?
if(count($members) > 0){
foreach($members as $member){
<tr>
<td><?php echo $member['firstname']; ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
Even while looping in table you can use the following command
<tbody>
<?
if(mysqli_num_rows($membersQuery) > 0){
while($member = mysqli_fetch_assoc($membersQuery)){
<tr>
<td><?php echo $member['firstname']; ?></td>
</tr>
<?php
}
}
?>
</tbody>
Related
I'm in need of some help. I'm working in dreamweaver and I'm trying to insert values from my MySQL database into a table on my HTML page.
Dreamweaver generated these variables for me from the server behaviours
mysql_select_db($database_connection, $connection);
$query_mwAcc = "SELECT * FROM accounts";
$mwAcc = mysql_query($query_mwAcc, $connection) or die(mysql_error());
$row_mwAcc = mysql_fetch_assoc($mwAcc);
$totalRows_mwAcc = mysql_num_rows($mwAcc);
Now what I need help with is what to put into the while loop for my PHP script, this is what I have so far
<table class="table table-bordered">
<?php while (): ?>
<tr>
<td><?php echo $row['id'] ?></td>
</tr>
<?php endwhile; ?>
</table>
You need to write your fetch command within the while loop itself.
$query_mwAcc = "SELECT * FROM accounts";
$mwAcc = mysql_query($query_mwAcc, $connection) or die(mysql_error());
while($row_mwAcc = mysql_fetch_assoc($mwAcc)) {
?>
<tr>
<td><?php echo $row_mwAcc['id'] ?></td>
</tr>
<?php } ?>
</table>
With an update to use PDO you can change the while loop into a foreach on the query result. PDO should be used over mysql_ interface methods, due to deprecation: Why shouldn't I use mysql_* functions in PHP?
<?php
$dbh = new PDO('mysql:host=...;dbname=...', $user, $pass);
$results = $dbh->query('SELECT * FROM accounts');
?>
<table class="table table-bordered">
<?php foreach ($results as $row): ?>
<tr>
<td><?php echo $row['id'] ?></td>
</tr>
<?php endforeach; ?>
</table>
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'll try to explain the problem straight away. I have one HTML form which takes input just like a comment form and it saves the xyz data into a MySQL database using PHP. Now, what I want is to create and display links for those comments on a page.
I mean the comments which have been saved including the user's email and name, should be opened by clicking a link.
I don't want to display all the details on a single page from the database for all the users. There should be a page on which links are shown, when a user click a link, the full post should be displayed in next page.
There is not something which I know about this process. Please help me out.
// $rows = set of result from your database query
foreach($rows as $row){
echo '<a'
. ' href="my_link_to_display_comment?id='.$row['id'].'">'
. 'Comment from '.$row['user_name']
. '</a>';
}
First a page to display all the links like the below example -
$result = mysql_query("SELECT * FROM calendar WHERE sort_month='11'");
while($row = mysql_fetch_array($result))
{echo
"".$row['event_name'].""
;}
and then in event.php(the next page after clicking link)
$id = $_GET['id'];
$sql = "select * from calendar where id = $id";
$result = mysql_query($sql, $con);
if ($result){
$row = mysql_fetch_row($result);
$title = $row[12];
$content = $row[7];} ?>
<?php echo $title ?>
<?php echo $content ?>
If you want to show details of a single user just do this.
You can make a search box by using a form.
eg. like if I want to display a details of a student, I will search him by using his roll number and run these queries.
<?php //to search student
require_once './secure.inc.php';
$status = 0;
if(isset($_POST['submit'])){
$roll_number = $_POST['roll_number'];
$query = "select * from students where roll_number=$role_number";
require_once '../includes/db.inc.php';
$result = mysql_query($query);
if(mysql_num_rows($result)==1){
$status = 1;
$row = mysql_fetch_assoc($result); //mysql_fetch_array - both numeric and key index
}else{
$status=2;
}
}
?>
//to display
<?php } else if($status==1) { ?>
<table>
<tbody>
<tr>
<td>Roll Number : </td>
<td><?php echo $row['roll_number']; ?></td>
</tr>
<tr>
<td>Name : </td>
<td><?php echo $row['name']; ?></td>
</tr>
<tr>
<td>Gender : </td>
<td><?php echo $row['gender']; ?></td>
</tr>
<tr>
<td>Email : </td>
<td><?php echo $row['email']; ?></td>
</tr>
<tr>
<td>Mobile Number : </td>
<td><?php echo $row['mobile_number']; ?></td>
</tr>
<tr>
<td>Course : </td>
<td><?php echo $row['course']; ?></td>
</tr>
</tbody>
</table>
<?php } ?>
I had a need for a system that could track billable tasks for tickets that we processed. I have a script that is almost complete, but I cannot seem to figure out why the tickets_tasks_activity field is not pulling the right enum_short_description when displaying. When I add a task the values are added correctly, its only not displaying them correctly. It seems that I can add one activity... and then every additional activity has the same description even though the values are different in the database. Could someone please help me out.
Here is the script to pull the information from the database.
* Get the tasks associated with a ticket.
*
* #company_id The ID of the company that is being queried.
* #ticket_id The ID of the ticket that is being queried.
*/
function get_ticket_tasks($company_id, $ticket_id)
{
$sql = "SELECT u.user_first_name,
u.user_last_name,
u.user_id,
tn.tickets_tasks_posted_date,
tn.tickets_tasks_task,
tn.tickets_tasks_activity,
(SELECT enum_short_description FROM tbl_enum WHERE enum_id = tn.tickets_tasks_activity) tickets_tasks_activity_description
FROM tbl_tickets_tasks tn,
tbl_user u
WHERE tn.tickets_tasks_company_id = {$company_id}
AND tn.tickets_tasks_ticket_id = {$ticket_id}
AND tn.tickets_tasks_posted_user_id = u.user_id;";
$query = $this->db->query($sql);
// If nothing was returned invalid query.
if($query->num_rows() == 0)
return false;
return $query->result_array();
}
Here is the code for the View Page
<div class="tab-pane" id="tasks">
<div class="row">
<div class="col-lg-12">
<?php
if($tasks)
{ ?>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>Service Provided</th>
<th>Value</th>
<th>Who?</th>
<th>When?</th>
</tr>
</thead>
<tbody>
<?php
for($i = 0; $i < count($tasks); ++$i)
{ ?>
<tr>
<td><?php echo $tasks[0]['tickets_tasks_activity_description']; ?></td>
<td><?php echo nl2br($tasks[$i]['tickets_tasks_task']); ?></td>
<td><?php echo $tasks[$i]['user_first_name'].' '.$tasks[$i]['user_last_name']; ?></td>
<td><?php echo date("m/d/Y", strtotime($tasks[$i]['tickets_tasks_posted_date'])); ?></td>
</tr>
<?php
} ?>
</tbody>
</table>
<?php
}
else
{ ?>
<p>This ticket does not have any tasks.</p>
<?php
} ?>
</div>
</div>
</div>
Here you have (at the beginning of the loop):
<td><?php echo $tasks[0]['tickets_tasks_activity_description']; ?></td>
you're printing always $task[0], you should print $task[$i]
PS: You should probably state your JOIN in the FROM section of your SQL clause (instead to put it on the WERE section), at first I though you had none :-)
I have a html table where i want to echo content out of a database table:
<table style="width: 100%;">
<tbody>
<tr>
<td>
<?php
$ergebnis = mysql_query("SELECT Test FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->Test;
}
</td>
<td>
<?php
$ergebnis = mysql_query("SELECT Test2 FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->Test2;
}
</td>
</tr>
</tbody>
</table>
Is there a way to cut short the php code, because it´s a bit too long to wirte in every
<?php
$ergebnis = mysql_query("SELECT Test FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->Test;
}
If your example really is what you are doing you might want to rather get all data in a single query:
<?php
$ergebnis = mysql_query("SELECT Test, Test2 FROM testtable");
$recordset = mysql_fetch_array($ergebnis, MYSQL_ASSOC);
?>
<table style="width: 100%;">
<tbody>
<tr>
<?php foreach (array('Test', 'Test2') as $column) { ?>
<td>
<?php foreach ($recordset as $record) { ?>
<?php echo htmlspecialchars($record[$column], ENT_QUOTES, 'UTF-8'); ?>
<?php } ?>
<td>
<?php } ?>
</tr>
</tbody>
</table>
Also note that the mysql_* functions has been deprecated and will be removed from the language soon. If you want to make sure your code doesn't start throwing notices or even will stop functioning in the future you might want to start using either mysqli_* or PDO.
http://php.net/manual/en/mysqlinfo.api.choosing.php
Also note that I have added htmlspecialchars() to prevent possible XSS attacks.
Well, you can simply do it by having a function:
function query($a){
$ergebnis = mysql_query("SELECT $a FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->$a;
}
}
and calling it like this:
<table style="width: 100%;">
<tbody>
<tr>
<td>
<?php
$a = 'Test';
query($a);
?>
</td>
<td>
<?php
$a = 'Test2';
query($a);
?>
</td>
</tr>
</tbody>
</table>
Create a function out of it. And use wherever you want.
You can make a function like this:
function query($a){
$ergebnis = mysql_query("SELECT $a FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->$a;
}
}