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.
Related
I have a table with columns name and amount, i want to query data that sum(amount) upto lets say 1000 in each group, in cases where a row value is greater than 1000 it can be still be displayed in its own group. i have tried this using a php loop of sum that range between 500 to 1000 but this way produces so many groups that i wish could be a voided. is there any way that we can do this in a single mysql query. regards
this is the way i crooked my way!
//this query helped me in maneuvering in provision of limits each time
$totalLines=mysql_query("select COUNT(*) as totalRows from chasis where chasis.BL_No =xxxx order by chasis.BL_No ")or die(mysql_error());
$fetch_totalLines=mysql_fetch_array($totalLines);
$found_rows=$fetch_totalLines['totalRows'];
$total=0;
$Toplimit=0;
$z=0;
a:
if($Toplimit>0){
$Toplimit=$Toplimit;
//$found_rows=$found_rows-$Toplimit;
$total=0;
}
$query=mysql_query("select * from chasis where chasis.BL_No =xxxx order by chasis.duty_amount limit ".$Toplimit.",".$found_rows." ")or die(mysql_error());
$i=1;
while($row=mysql_fetch_array($query)){
$total=round($row['duty_amount'],0)+$total;
echo'<tr>
<td>'.$i.'</td>
<td>'.$row['BL_No'].'</td>
<td>'. mb_substr($row["make"], 0, 1,"UTF8")."-";
echo $row["model"];
echo'</td>
<td>'.$row['chasisNo'].'</td>
<td>'.$row['cc'].'</td>
<td>'.date("Y F", strtotime($row['year'])).'</td>
<td>'.$row['EntryNumber'].'</td>
<td>'.round($row['duty_amount'],0).'</td>
<td> </td>
</tr>';
if($total<=1000 and $total>=500 ){
echo"<td colspan='8' align='right'><b>Duty Cheque for ".$i." units</b></td>
<td><b>".$total."</b></td>
";
$Toplimit=$Toplimit+$i;
$z=$z+$i;
goto a;
}
$i++;
}
I have decided to show everything that i did may to reflect the usage of goTo statement to hold the column id
I know this is a poor way to handle this but am really stuck.
Could you please try this way
select * from chasis where chasis.BL_No =xxxx AND chasis.duty_amount>=500 AND chasis.duty_amount<=1000 order by chasis.duty_amount
$query=mssql_query ('SELECT USER_INDEX_ID FROM T_o2jam_login');
echo "<table border =\"0\" style=\"color: gray;\" cellspacing=\"0\" cellpadding=\"0\" CLASS='boldtable'><tr><th colspan=\"9\">Online Players</th></tr><tr><td>Level </td> <td> Nick </td> </tr>";
if (mssql_num_rows($query)) {
while ($row = mssql_fetch_array($query)) {
$q2 = mssql_query ("select * from t_o2jam_charinfo where USER_INDEX_ID=$row[USER_INDEX_ID] ORDER BY Level DESC");
$nt=mssql_fetch_array($q2);
echo "<tr><td>Lv. $nt[Level] </td><td> $nt[USER_NICKNAME] </td></tr>" ;
I am trying to sort online users by Level on Descending order. From 99 to Level 1. It displays the data but they are not sorted. What's the problem right there? Thank you!
You need to have the order by in your first query. Your second query is within a loop, so it's going to first go by the order of the first one.
This really should be one query with a JOIN.
SELECT charinfo.*, FROM t_o2jam_charinfo charinfo
INNER JOIN T_o2jam_login login
ON charinfo.USER_INDEX_ID = login.USER_INDEX_ID
ORDER BY `Level` DESC
But since you never use any of the info from the "login" table, one wonders why it's even used at all.
Anyway, never run queries in loops.
I have one table in database -> applicants_detail which has two column
applied_personid and applied_courseid.
College provides number of courses.
I have the given id to applied_courseid, that is written like this:
arts--1
science--2
maths--3...
And applied_personid which contains applied person id.
I need to count how many people applied for the course, like in the page it should show:
maths------15 people_applied
science------20 people_applied
I tried this query:
$query="select * from people where people_id in(select applied_personid from applicants where applied_courseid =".$_GET['postcourseid']." )";
And the code to find count is not able to show count in the table.
<?php
$count=0;
while($row=mysql_fetch_array($res))
{
echo '<tr> <td width="10%">'.$count.'
<td width="50%">'.$row['student_fnm'].'
<td width="50%">'.$row['applied_courseid'].'
<td width="30%">course name
';
$count++;
}
?>
You just need to group by applied_courseid. This should do it.
select applied_courseid, count(applied_personid) as `count`
from
applicants_detail
group by applied_courseid
Then if needed use the results here to join to the other tables
which you probably have (that would give the you course name e.g.).
Try this to get all data:
SELECT course_name, applied_courseid as course_id, count(applied_personid) as `student_number`
FROM applicants_detail
INNER JOIN course_detail ON course_detail.course_id = applicants_detail.course_id
GROUP BY applied_courseid
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.
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.