I'm having trouble with my members list. It shows EVERY username but i would like it to show only 15 per row.
The code:
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="23%"><em><strong>Username</strong></em></td>
</tr>
<?
$sql = mysql_query("select * from usr_users");
while($m = mysql_fetch_array($sql))
{ ?>
<tr>
<td width="23%"><div style="float: left;" onMouseover="ddrivetip('<em><? echo("$m[username]");?></em> <br /> <b>Rank:</b> <? echo("$m[rankerslevel]");?><br /> <b>Bits:</b> <? echo("$m[credits]");?><br /> <b>Score:</b> <? echo("$m[points]");?><br /> <b>Mood:</b> <? echo("$m[usrstatus]");?><br /> <b>ID:</b> <? echo("$m[id]");?><br /> <b>Sex:</b> <? echo("$m[sexmalefemale]");?><br /> <b>Country:</b> <? echo("$m[countrywhere]");?><br />','white', 100)";
onMouseout="hideddrivetip()"><img src="/bolt.png" alt="member_icon"/> <font color="<? echo("$m[usercolour]");?>"><? echo("$m[username]");?></font></td></div>
<? } ?>
Thanks in advance!
Use LIMIT clause. Here is usage:
LIMIT [offset,] rows
Examples:
SELECT * from usr_users LIMIT 0, 10
This query will retrieve 1-10 rows (from 0 to 10).
SELECT * from usr_users LIMIT 10, 10
This query will retrieve 11-19 rows.
If you want to get row with specific ids use IN statement:
SELECT * from usr_users WHERE id IN (1,2,3)
Also read this: http://dev.mysql.com/doc/refman/5.0/en/select.html
If you want to learn how to make pagination, look at this topic: http://www.codediesel.com/php/simple-pagination-in-php/
the simpliest soultion would be:
SELECT * FROM usr_users LIMIT 15
use mysql limit
select * from usr_users order by createddate desc LIMIT 0, 15
assuming you have a column called createddate on which you are doing an order by.
This will get you the last 15 users created.
You can get the total number of records and divide it by the "items to be displayed " to get the number of paging elements.
http://www.dharshin.com/2007/09/paging-results-with-php-and-mysql-part.html
Supposing you have an 'id' column - and it is set as Auto_Increment (as-well as an INT)
SELECT * FROM usr_users ORDER BY -id LIMIT 15
This will grab the 15 most latest users, and show them in order.
Related
I can count number of a table rows by using mysqli_num_rows. I have a table which contains similar rows. How can I count by grouping by similar row?
For instance: I have a table with 2 columns : Student and Option. Let say there are 50 students. 20 are in Economy option, 20 are in Management option and 10 are in Secretary Option. How can display those numbers. I can display only the 50.
my codes
$qry = "select * from table group by option";
$req= #mysqli_query($conn, $qry);
$result = mysqli_query( $conn, "select id from table");
$num_rows = mysqli_num_rows($result);
Students Total (<?php echo $num_rows ?>)
<table >
<tr>
<th>Student</th>
<th>Option</th>
</tr>
<?php
while($row=mysqli_fetch_array($req))
{
?>
<tr>
<td><?php echo $row['student'] ?></td>
<td><?php echo $row['option'] ?></td>
</tr>
<?php
}
?>
</table>
Here is the query you need:
SELECT COUNT(*) AS `option_count`, * -- returns a count aliased as "options_count"
FROM `table`
GROUP BY `option` -- will group the options and show the counts
Now you can echo the count, along with other data:
echo $row['count_options'];
echo $['options'];
The problem that you have here is that you will not be able to display each student in the option because this counts / groups only the three options.
Behold the proper query :
$qry = "select option as opt, COUNT(*) AS option from table group by option";
I have this code which display a list of players and their points in a contest:
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php
$result=mysql_query("SELECT * FROM `pointstable` WHERE `contestfield` = 1 ORDER BY `pointsfield` ASC");
while ($info=mysql_fetch_assoc($result)) {
$playerid=$info['playeridfield'];
$playername=$info['playernamefield'];
$pointsfield=$info['pointsfield'];
?>
<tr><td style="text-align:center; padding-bottom:5px" valign="middle"><?=$playername?> made <?=number_format($pointsfield)?></td></tr>
<?php } ?>
</table>
This will display the complete list, but when a user plays more than one time, it displays all the points he has...
So what I want is to display only the first value (in this case, the lowest points since the objetive of the contest is to get the lowest score) The playeridfield is a unique number for each player, each player has one.
Who can this be done?
Ad this to your SQL query at the end LIMIT 1
Just use the mysql min function and group by id:
SELECT playeridfield, playernamefield, MIN(pointsfield) AS pointsfield FROM `pointstable`
WHERE `contestfield` = 1 GROUP BY `playeridfield` ORDER BY `pointsfield` ASC
Try changing your SQL query to this:
$result=mysql_query("SELECT * FROM `pointstable` WHERE `contestfield` = 1 ORDER BY `pointsfield` ASC LIMIT 1");
The LIMIT 1 should make it so only one row is returned from the query.
How do I get the 3 latest values from the database.
My codes to show all values is like this :
<?php
$query="select hari from reg";
$hasil=mysql_query($query);
?>
<table style="text-align:center;">
<tr>
<th>Data</th>
</tr>
<?php
if($hasil === FALSE) {
die(mysql_error());
}
while ($data=mysql_fetch_array($hasil)) {
echo ("<tr><td> $data[hari] </td></tr>");
}
?>
</table>
Thanks for the help.
$query="select id , hari from reg ORDERBY id desc LIMIT 3";
This will get you the latest 3 records.
$query="select hari from reg ORDER BY hari DESC LIMIT 3";
$query= SELECT hari FROM reg ORDER BY hari DESC LIMIT 3
Try this
$query = mysql_query("SELECT id,hari FROM reg ORDER BY id DESC LIMIT 0,3") or die(mysql_error());
Consider to use mysqli or PDO_extension --> http://www.php.net/manual/en/intro.mysql.php
Then like all other's recommendation it's possible with ORDER BY and LIMIT 3.
If you would use timestamps per record you could ORDER BY timestamp and LIMIT 3 to them OR if you use an autoincrement int ORDER BY id and LIMIT 3
Mysql also has functions that can return the last entry.. Maybe there is a function to get a specific amount of last entries.
In short, a leaderboard.
The person with the highest coins wants to be listed, first. listing the top 10 only, based on the highest coin count joined with their username.
This lists the usernames, but not sure how to get their coins count, as well as ascend them in a top 10 formation.
<?php
$query = mysql_query("SELECT username FROM `users`");
echo '<table>';
while($rowtwo = mysql_fetch_array($query)){
echo '<tr>
<td><font size="2" face="Lucida Sans Unicode" color="red">'.$rowtwo['username'].'</td>
</tr>';
}
echo '</table>';
?>
SELECT username FROM `users` ORDER BY coins DESC LIMIT 10
The DESC is important or else it will list coins in order of lowest to highest. This also assumes that coin count is a column of users. If it's not, you'll have to let me know.
Change the query to:
SELECT username FROM `users` ORDER BY `coins` DESC LIMIT 0,10
I can't seem to find the right way to do this so I was hoping someone could give me some direction?
The SQL Database is structured like this (I've removed the irrelevant stuff):
Requests
R_ID R_FulfilledBy
1 Bob
2 Craig
3 Bob
SIMs
SM_ID SM_FulfilledBy
1 Bob
2 Craig
3 Bob
I'm hoping to end up with this output:
Fulfilled By Requests
Bob 4
Craig 2
Here's my PHP/HTML:
<div id="table">
<?php
//Connect to MySQL Database
$connection = mysql_connect($runnerdbServer, $runnerdbUser, $runnerdbPass);
mysql_select_db($runnerdbName, $connection) or die("MysQL Error");
$query = "SELECT R_FulfilledBy, COUNT(R_ID) FROM Requests GROUP BY R_FulfilledBy ORDER BY COUNT(R_ID) DESC";
$result = mysql_query($query) or die(mysql_error());
?>
<!-- Number of Runners (Counts total number of records in Requests table) -->
<table border='0' width='50%'>
<tr>
<th>Runners Fulfilled</th>
<tr><td><?php
$query = mysql_query("SELECT * FROM Requests");
$number=mysql_num_rows($query);
echo $number;
?>
</td></tr>
</table>
<!-- Fulfillment Stats -->
<table border='0' width='50%'>
<tr>
<th>Name</th>
<th>Runners Fulfilled</th>
</tr>
<?
// Print out result (I want this to calculate requests fulfilled by each user in 'Requests' and 'SIMs' table)
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>". $row['R_FulfilledBy'] ."</td>";
echo "<td>". $row['COUNT(R_ID)'] ."</td>";
echo "</tr>";
}
?>
</table>
At present it's only calculating the records from the 'Requests' table :(
You could union all the two tables together in a subquery:
select FulfilledBy
, count(*)
from (
select R_FulfilledBy as FulfilledBy
from Requests
union all
select SM_FulfilledBy
from SIMs
) as SubQueryAlias
group by
FulfilledBy
Use union all instead of union because the second eliminates duplicates; which would give everyone a maximum count of 1.
I'd go with this:
SELECT R_FulfilledBy, COUNT(*) +
( SELECT COUNT(*) FROM SIMs WHERE R_FulfilledBy = SM_FulfilledBy )
FROM Requests GROUP BY R_FulfilledBy