Add profile avatar to the leaderboard - php

I recently made a leaderboard and I would like to make some changes.
This is the code :
<table>
<tr>
<td></td>
<td>Name</td>
<td>Likes</td>
</tr>
<?php
$con=mysqli_connect("localhost", "user", "pass", "database");
$sql = ("SELECT username, likes FROM users ORDER BY likes DESC LIMIT 5");
$result = mysqli_query($con, $sql);
$rank = 1;
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>{$rank}</td>
<td>{$row['username']}</td>
<td>{$row['likes']}</td>
</tr>";
$rank++;
}
}
?>
</table>
Now, what I would like to do is add instead of 'username' the avatar of each user with 'alt' for username. E.g :
<img src="<?php echo $example->avater;?>" alt="<?php echo $example->username;?>" />
This is the location of avatar in mysql !
I also have a css code created for the avatar to appear in a round circle.That means we will use
<div class="pro_usrs_container">
I tried but I don't understand how to make the codes so that I can integrate them in <td> instead of <td> $ row ['username'] </td>
Thank you very much !

Select avater in your Query and use it in your table.
<table>
<tr>
<td></td>
<td>Name</td>
<td>Likes</td>
</tr>
<?php
$con=mysqli_connect("localhost", "user", "pass", "database");
$sql = ("SELECT username, avater, likes FROM users ORDER BY likes DESC LIMIT 5");
$result = mysqli_query($con, $sql);
$rank = 1;
if (mysqli_num_rows($result)) :
while ($row = mysqli_fetch_assoc($result)) : ?>
<tr>
<td><?php echo $rank; ?></td>
<td><img src="<?php echo $row['avater'];?>" alt="<?php echo $row['avater'];?>" /></td>
<td><?php echo $row['likes']; ?></td>
</tr>
<?php $rank++;
endwhile;
endif;
?>
</table>

Based on the requirement above:
<table>
<tr>
<td></td>
<td>Name</td>
<td>Likes</td>
</tr>
<?php
$con=mysqli_connect("localhost", "user", "pass", "database");
$sql = ("SELECT avater, username, likes FROM users ORDER BY likes DESC LIMIT 5");
$result = mysqli_query($con, $sql);
$rank = 1;
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>{$rank}</td>
<td><img src="{$row['avater']}" alt="{$row['username']}" /></td>
<td>{$row['likes']}</td>
</tr>";
$rank++;
}
}
?>

Related

PHP table leaderboard

I'm trying to make a leaderboard of my favorite movies.
<table>
<tr>
<td>Rank</td>
<td>Score</td>
<td>Year</td>
</tr>
<?php
$con=mysqli_connect("localhost", "", "", "movies");
$sql = ("SELECT score, year FROM mymovies ORDER BY score DESC");
$result = mysqli_query($con, $sql);
$rank = 1;
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<td>{$rank}</td>
<td>{$row['score']}</td>
<td>{$row['number_of_times']}</td>";
$rank++;
}
}
?>
</table>
The problem is that it shows the data in one line instead of showing it underneath each other like this:
Rank Score Year
1 25 1999
2 23 1987
3 20 2005
Probably because you need to include a <tr> tag ..
<table>
<tr>
<td>Rank</td>
<td>Score</td>
<td>Year</td>
</tr>
<?php
$con=mysqli_connect("localhost", "", "", "movies");
$sql = ("SELECT score, year FROM mymovies ORDER BY score DESC");
$result = mysqli_query($con, $sql);
$rank = 1;
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>{$rank}</td>
<td>{$row['score']}</td>
<td>{$row['number_of_times']}</td>
</tr>";
$rank++;
}
}
?>
</table>

Displaying data in tables depending on group

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>

How can I delete the selected record in mySQL from my HTML/PHP page?

My idea is to click 'Delete' link and it will pass the id to another PHP page (deleteSession.php), and then execute the query in deleteSession.php. but I couldn't seems to get the id from manageSession.php
In manageSession.php,
<table align='center' border='1' cellpadding='5' cellspacing='0'>
<tr>
<th>Session Id</th>
<th>Type</th>
<th>Date & Time</th>
<th>Venue</th>
<th>Pax</th>
<th>Delete</th>
<th>Edit</th>
</tr>
<?php
$sql = "SELECT booking_id, booking_types, dates_sessions, venue_available, room_count FROM bookings_available ORDER BY dates_sessions asc";
$result = mysqli_query($link, $sql) or die(mysqli_error($link));
//mysqli_close($link);
while ($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row['booking_id']; ?></td>
<td><?php echo $row['booking_types']; ?></td>
<td><?php echo $row['dates_sessions']; ?></td>
<td><?php echo $row['venue_available']; ?></td>
<td><?php echo $row['room_count']; ?></td>
<td><input type="button" value="Delete"/></td>
<td><input type="button" value="Edit"/></td>
</tr>
<?php } ?>
</table>
In deleteSession.php,
<?php
include "dbFunctions.php";
include "manageSession.php";
//$sql = "SELECT booking_id, booking_types, dates_sessions, venue_available, room_count FROM bookings_available";
//$result = mysqli_query($link, $sql) or die(mysqli_error($link));
$bookingId = filter_input(INPUT_GET, 'booking_id');
$deleteQuery = "DELETE FROM bookings_available WHERE booking_id = '$bookingId'";
?>
I think in deleteSession.php file code should be as follows.
$bookingId = filter_input(INPUT_GET, 'id');
OR
$bookingId = $_GET['id'];
Because you are passing get parameter as follows.
deleteSession.php?id=
And also keep anchor as follows.
Delete
In the deleteSession.php you can try and replace:
$bookingId = filter_input(INPUT_GET, 'booking_id');
with the below code:
$bookingId = $_REQUEST['id'];
Finally at the last line you have to execute the query which is stored in $deleteQuery variable, which is not executed yet by using below code:
$qry = mysql_query("DELETE FROM bookings_available WHERE booking_id = '$bookingId'");
//will show you error if not able to delete
if(!$qry)
die("Error: ".mysql_error());
Added this at line 3 and it works:
mysqli_select_db($link ,$DB);
Because in the code I have not selected the mysql database and also the query was not executing as the first parameter $link was missing.

retrieve and add the data into database

I need to retrieve the relevant data from database, and add to the attendance table again. this code shows many error. actually i dunno how to use the array to add the data. can anyone help?? thankssssssssssss.
<?php
$sql = "select p_module_ID, student_ID,
p_attendance_Date, p_attendance_Time,
p_attendance_Status,p_attendance_reason
from attendance";
$result = mysqli_query($con, $sql);
if(!$result)
{
echo mysqli_error($con);
exit();
}
while($rows = mysqli_fetch_array($result))
{
$attendance_list[] = array('p_module_ID' => $rows['p_module_ID'],
'student_ID' => $rows['student_ID'],
'p_attendance_Date' => $rows['p_attendance_Date'],
'p_attendance_Time' => $rows['p_attendance_Time'],
'p_attendance_Status' => $rows['p_attendance_Status'],
'p_attendance_reason' => $rows['p_attendance_reason']);
}
?>
<html>
<body>
<form action="attendance.php" method="post" accept-charset='UTF-8'>
<table border="0" cellspacing="20" >
<tr>
<td>
<select name="p_module_ID">
<?php
$sql = "SELECT p_module_ID FROM schedule";
$result = $con->query($sql);
while($row = mysqli_fetch_assoc($result)) {
echo "<option>".$row['p_module_ID']."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>
<select name="p_attendance_Date" >
<?php
$sql = "SELECT p_StartDate FROM schedule";
$result = $con->query($sql);
while($row = mysqli_fetch_assoc($result)) {
echo "<option>".$row['p_StartDate']."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>
<select name="p_attendance_Time">
<?php
$sql = "SELECT p_Time FROM schedule";
$result = $con->query($sql);
while($row = mysqli_fetch_assoc($result)) {
echo "<option>".$row['p_Time']."</option>";
}
?>
</select>
</td>
</tr>
<table id="t01">
<tr>
<th> Student ID </th>
<th> Name </th>
<th> Attendance </th>
<th> Reason </th>
<?php foreach($attendance_list as $attend) : ?>
<tr>
<td>
<?php
$sql = "SELECT p.student_ID,CONCAT(s.student_fname, ' ', s.student_lname) AS fullname FROM Pals p JOIN student s ON p.student_ID = s.student_ID WHERE student_role = 'Student' GROUP BY student_ID";
$result = $con->query($sql);
while($row = mysqli_fetch_assoc($result)) {
echo $attend[$row['student_id']];
}
?>
</td>
<td>
<?php
$sql = "SELECT p.student_ID,CONCAT(s.student_fname, ' ', s.student_lname) AS Fullname FROM Pals p JOIN student s ON p.student_ID = s.student_ID WHERE student_role = 'Student' GROUP BY student_ID";
$result = $con->query($sql);
while($row = mysqli_fetch_assoc($result)) {
echo $attend['Fullname'];
}
?>
</td>
<td>
<?php echo $attend["p_attendance_Status"]; ?>
</td>
<td>
<?php echo $attend["p_attendance_reason"]; ?>
</td>
</tr>
</tr>
<?php endforeach; ?>
</table>
</table>
</form>
</body>
</html>
Use $row['student_Id'] instead of $row['student_id']. Same as for Fullname. You selecting AS fullname not AS Fullname. Listen to the case sensitiveness.

Automatically make new box by reading from database

I've got a box with information that I read from database(news) , but I've got another database(categories) with the category of that box. If the categories are 7 I want to automatically make 7 box.
<?php
mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("tnews2") or die(mysql_error());
mysql_query("set names 'utf8'");
$rowsPerPage = 2;
$query1 = "SELECT id,name FROM categories ORDER BY ID";
$result1 = mysql_query($query1) or die(mysql_error()."[".$query1."]");
$query2 = "SELECT id,name,text,img,cat_id FROM news WHERE cat_id=2 ORDER BY ID DESC LIMIT $rowsPerPage";
$result2 = mysql_query($query2) or die(mysql_error()."[".$query2."]");
?>
while($row = mysql_fetch_array($result1)){ ?>
<?php for($i=0; $i<$row['id']; $i++){ ?>
<div class="focusBussines" >
<?php while ($rowB = mysql_fetch_array($result2)){ ?>
<a class="titleMini" href="categories.php?id=<?= $rowB['id'] ?>&cat_id=<?=$rowB['cat_id']?>">Бизнес</a>
<?php } ?>
<table class="table" width="100%">
<?php while ($rowB2 = mysql_fetch_array($result2)){
if($rowB2['cat_id'] == 2){ ?>
<tr>
<td align="left" width="150" >
<img class="pic" src="<?php echo $rowB2["img"];?>" height="120" width="120">
</td>
<td align="left" colspan="100%" id="title" ><a id="Zaglavie" href="novina.php?id=<?= $rowB2['id'] ?>&cat_id=<?=$rowB2['cat_id']?>" > <?php echo $rowB2['name']; ?></a></td>
</tr>
<tr >
<td colspan="100%"><hr/> </td>
</tr>
<?php } } ?>
</table>
</div>
<?php } } ?>
but it doesn't work.
Your problem originates from those unnecessary opening/closing php statements (emphasize added):
$result2 = mysql_query($query2) or die(mysql_error()."[".$query2."]");
>>>>> ?> <<<<<
while($row = mysql_fetch_array($result1)){ >>>>> ?> <<<<<
>>>>> <?php <<<<< for($i=0; $i<$row['id']; $i++){ >>>>> ?> <<<<<
This is the same as writing
$result2 = mysql_query($query2) or die(mysql_error()."[".$query2."]");
while($row = mysql_fetch_array($result1)) {
for($i=0; $i<$row['id']; $i++){ ?>
and increases the readability and reduces possible errors due to missing open/close tags.

Categories