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.
Related
$sqlList ="select title from podcast order by date_pd desc limit 5";
$rslt2 = mysqli_query($conn, $sqlList) or die ("Fail".mysqli_error($conn));
$record2 = mysqli_fetch_array($rslt2);
$nRows = mysqli_num_rows($rslt2);
for ($i=0; $i<$nRows; $i++) {
$record2 = mysqli_fetch_array($rslt2);
echo $record2["title"];
}
the echo result is skipping the first row of the query, what am I doing wrong?
You are loosing the first row of your result set because you read the first row and throw it ways/ignore it
$sqlList ="select title from podcast order by date_pd desc limit 5";
$rslt2 = mysqli_query($conn, $sqlList) or die ("Fail".mysqli_error($conn));
// this line gets the first row of your result set
// and you just ignore it therefore throw it away
$record2 = mysqli_fetch_array($rslt2);
$nRows = mysqli_num_rows($rslt2);
for ($i=0; $i<$nRows; $i++) {
$record2 = mysqli_fetch_array($rslt2);
echo $record2["title"];
}
As #Anant says, you are better advised to use a while as if the result set is empty, it just does nothing. Admittedly not always an advantage.
$sqlList ="select title from podcast order by date_pd desc limit 5";
$rslt2 = mysqli_query($conn, $sqlList) or die ("Fail".mysqli_error($conn));
while ( $record2 = mysqli_fetch_array($rslt2) ) {
echo $record2["title"];
}
remove the line $record2 = mysqli_fetch_array($rslt2);, which is placed before the for loop.
new code would be:
$sqlList ="select title from podcast order by date_pd desc limit 5";
$rslt2 = mysqli_query($conn, $sqlList) or die("Fail".mysqli_error($conn));
$nRows = mysqli_num_rows($rslt2);
for ($i=0; $i<$nRows; $i++) {
$record2 = mysqli_fetch_array($rslt2);
echo $record2["title"];
}
I have two queries to a single table in a database, the only difference being the limit. I like the output but I know there must be more practical ways of doing this. Can anyone suggest something?
$sql1 = mysql_query("SELECT * FROM products ORDER BY id DESC LIMIT 20");
$count = mysql_num_rows($sql1);
if ($count > 0) {
while($row = mysql_fetch_array($sql1)){
}
}
$sql2 = mysql_query("SELECT * FROM products ORDER BY id DESC LIMIT 1");
$count = mysql_num_rows($sql2);
if ($count > 0) {
while($row = mysql_fetch_array($sql2)){
}
}
Create a function and pass the limit as a parameter perhaps:
function doQuery($limit = 1) {
$sql = mysql_query("SELECT * FROM products ORDER BY id DESC LIMIT $limit");
$count = mysql_num_rows($sql);
if ($count > 0) {
while($row = mysql_fetch_array($sql)){
}
}
};
doQuery(1);
doQuery(20);
$sql1 = mysql_query("SELECT * FROM products ORDER BY id DESC LIMIT 20");
$count = mysql_num_rows($sql1);
if ($count > 0) {
while($row = mysql_fetch_array($sql1)){
//loop over all records...
}
}
$count = mysql_num_rows($sql1);
if ($count > 0) {
if($row = mysql_fetch_array($sql1)){
//retrieve one record
}
}
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
I'm trying to store a number of percentages that I've worked out into an array so that I can call it later on to create a pie chart but don't have a clue how to do it. Here is my PHP code so far for working out the percentages: Thanks for any help in advance!
//execute the SQL query and return records
$result = mysql_query("SELECT book_id, COUNT(*) FROM loan GROUP BY book_id ASC ORDER BY count(*) DESC LIMIT 10");
$book_count = mysql_num_rows($result);
$step = 1;
while($row = mysql_fetch_array($result))
{
$total = $total + $row['COUNT(*)'];
$i[$step] = $row['COUNT(*)'];
$step++;
}
for($index = 1; $index <= 10; $index++)
{
$percentage[$index] = (($i[$index]/$total)*100);
#$degrees = (($percentage/100)*360);
}
This should do what you're asking, just substitute the variables for real ones:
array_push($array, $value);
// This will add "$value" on to the end of $array, i.e the last element
It can also be done much simpler:
$array[] = $value;
Updated code:
//execute the SQL query and return records
$result = mysql_query("SELECT book_id, COUNT(*) FROM loan GROUP BY book_id ASC ORDER BY count(*) DESC LIMIT 10");
$book_count = mysql_num_rows($result);
$step = 1;
while($row = mysql_fetch_array($result))
{
$total = $total + $row['COUNT(*)'];
$i[$step] = $row['COUNT(*)'];
$step++;
}
$percentage_array = array();
for($index = 1; $index <= 10; $index++)
{
array_push($percentage_array, (($i[$index]/$total)*100));
#$degrees = (($percentage/100)*360);
}
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)