select similar value from MySQL and order the result - php

how do I order this result??
$range = 5; // you'll be selecting around this range.
$min = $rank - $range;
$max = $rank + $range;
$limit = 10; // max number of results you want.
$result = mysql_query("select * from table where rank between $min and $max limit $limit");
while($row = mysql_fetch_array($result))
{
echo $row['name']." - ".$row['rank']."<br>";
}

$result = mysql_query(
"select * from table where rank between $min and $max " .
"order by rank asc limit $limit"
);

Use the "order by"- clause:
mysql_query("select * from table where rank between $min and $max order by rank limit $limit");
This will order your result from little to big values.
Use "order by rank desc" to order in descendent direction. (big -> little)

Related

MySql ORDER By rand() one row

I have got this php code
$coma_count=substr_count($Hob,',');
if ($coma_count==0) {
$query="SELECT * FROM user_opt WHERE Interests='$Hob' LIMIT 80";
}else{
$expl=explode(',',$Hob);
for ($i=0; $i <=$coma_count; $i++) {
$query.=" UNION SELECT * FROM users WHERE Interests LIKE '%{$expl[$i]}%'";
}
}
$sql=$con->query($query." ORDER BY RAND()") or die($con->error);
Number of $coma_count varies from 0 to 112 and the problem is when $coma_count is 0 (means only country was selected) my query is going to look like this SELECT * FROM user_opt WHERE Country='$Countr' LIMIT 80 ORDER BY RAND() which is not accepted by SQL.What can i do?
Create a $limit variable, set it if need be, then append it:
$limit = '';
$coma_count=substr_count($Hob,',');
if ($coma_count==0) {
$query="SELECT * FROM user_opt WHERE Interests='$Hob'";
$limit = " LIMIT 80";
}else{
$expl=explode(',',$Hob);
for ($i=0; $i <=$coma_count; $i++) {
$query.=" UNION SELECT * FROM users WHERE Interests LIKE '%{$expl[$i]}%'";
}
}
$sql=$con->query($query." ORDER BY RAND()".$limit) or die($con->error);

Show members random and not in alphabetical order

On the memberpage its shows the first 16 members, they are all in alphabetical order.
How to change that into random.
<?php
$limit=48;
$stages ='none';
$page = isset($_GET['page'])?mysql_escape_string($_GET['page']):0;
$start = ($page !== 0)?($page - 1) * $limit:0;
$queryl ="select *, DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(birthdate)), '%Y')+0 as age from user_profiles LIMIT ".$start.",".$limit;
$query = "SELECT count(*) as num FROM `user_profiles` ";
$targetpage = 'zoeken.php?'.rtrim($_SERVER['QUERY_STRING'],'&');
$dis = display($queryl);
$paginate = paginate2($page, $stages, $limit, $targetpage, $query, $start);//current page, stages, limit, query
echo $dis['thumbnail'];
?>
Select a random row with MySQL:
SELECT column FROM table
ORDER BY RAND()
LIMIT 1
select *, DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(birthdate)), '%Y')+0 as age from user_profiles ORDER BY RAND() LIMIT ".$start.",".$limit;

mysql random entries from the top200

is there a better way to return random entries from the TOP 200 bestselling tshirts (tshirt_sales from shop_tshirts) that could just query just 6 entries instead of 200 ?
$SQL = "SELECT * FROM shop_tshirts WHERE shop = 'nidieunimaitre' AND online='1' ORDER BY tshirt_sales DESC LIMIT 200";
$Result = mysql_query($SQL)
or die('A error occured: ' . mysql_error());
$Rows = array();
while ($Row = mysql_fetch_assoc($Result))
$Rows[] = $Row;
shuffle($Rows);
$i = 0;
foreach($Rows as $Data){
$i++;
if($i >= 6) { break; }
}
Try this:
SELECT *
FROM
(SELECT *
FROM shop_tshirts
WHERE shop = 'nidieunimaitre' AND online='1'
ORDER BY tshirt_sales DESC
LIMIT 200) as tableAlis
ORDER BY RAND()
LIMIT 6;
Return first 200 results and after order by rand and return only six results.

PHP MySQL Results num total rows and split for pages

Is there any way to count and split results without doing 2 query,
im using a query something like this:
$result = mysqli_query($con,"SELECT * from articles WHERE category = '$category'");
$row = mysqli_fetch_row($result);
$rows = $row[0];
$page_rows = 20;
$last = ceil($rows/$page_rows);
$pagenum = 1;
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$result2 = mysqli_query($con,"SELECT * FROM articles order by id desc $limit");
while($row = mysqli_fetch_array($result2)) {
$id = $row['id'];
}
this is working but i dont like that it has 2 queries, any better idea? thank you
$pagenum = 1;
$rows_on_page = 20;
$start = (($pagenum - 1) * $rows_on_page);
$end = ($pagenum * $rows_on_page);
$result = mysqli_query($con, "SELECT * from articles WHERE category = '$category' ORDER BY id DESC LIMIT $start, $end");
while ($row = mysqli_fetch_array($con,$result) {
... do stuff with articles ...
$pagenum++;
}
The while loop will protect you from going past the end of the records.
try this query,it will return count of records and pagination (title in query is a field name, change it based on your table):
SELECT aa.countt, title FROM articles , (SELECT COUNT(*) AS countt FROM articles WHERE category = '$category' ) AS aa ORDER BY id LIMIT 5,10

combining two While & sql statments

I need to combine the two queries if possible or make them process one after the other. I'm assuming the $Record_Count = $Record_Count + 1; doesn't need to be there twice since that's just for the pagination script. (thanks in advance)
$results = mysql_query("SELECT * load_test WHERE language = '".$lang."' ORDER BY Id DESC, creationdate DESC LIMIT $start, 5");
while ($data = mysql_fetch_array($results)) {
$Record_Count = $Record_Count + 1;
$rec_res = mysql_query("SELECT * FROM names WHERE com_id = '".$data[Id]."'");
while ($recdata = mysql_fetch_array($rec_res)) {
$Record_Count = $Record_Count + 1;
IF $Record_Count is just counting the number of returned rows you could always use mysql_num_rows()
$results = mysql_query("SELECT * FROM load_test WHERE language = '".$lang."' ORDER BY Id DESC, creationdate DESC LIMIT $start, 5");
$rec_res = mysql_query("SELECT * FROM names WHERE com_id = '".$data[Id]."'");
$Record_Count += mysql_num_rows($result) + mysql_num_rows($rec_res);

Categories