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
Related
I'm trying to order a list of items based on the amount of comments for each topic as shown below:
$page = $_GET['page'];
$query = mysql_query("SELECT * FROM topic WHERE cat_id='$page' LIMIT $start, $per_page");
if (mysql_num_rows($query)>=1)
{
while($rows = mysql_fetch_array($query))
{
$number = $rows['topic_id'];
$title = $rows['topic_title'];
$description = $rows['topic_description'];
//get topic total
$sqlcomment = mysql_query("SELECT * FROM comments WHERE topic_id='$number'");
$commentnumber = mysql_num_rows($sqlcomment);
// TRYING TO ORDER OUTPUT ECHO BY TOPIC TOTAL ASC OR DESC
echo "
<ul>
<li><h4>$number. $title</h4>
<p>$description</p>
<p>$topictime</p>
<p>$commentnumber</p>
</li>
</ul>
";
}
}
else
{
echo "<p>no records available.</p><br>";
}
What would be the best way to order each echo by $num_rows (ASC/DESC values)? NOTE: I've updated with the full code - I am trying to order the output by $commentnumber
The first query should be:
SELECT t.*, COUNT(c.topic_id) AS count
FROM topic AS t
LEFT JOIN comments AS c ON c.topic_id = t.topic_id
WHERE t.cat_id = '$page'
GROUP BY t.topic_id
ORDER BY count
LIMIT $start, $per_page
You can get $commentnumber with:
$commentnumber = $rows['count'];
You don't need the second query at all.
First of all you have error here
echo "divs in order from least to greatest "number = $num_rows"";
It should be
echo "divs in order from least to greatest number = " . $num_rows . "";
And about the most commented try with
$sql = "SELECT * FROM `table` WHERE `id` = '$id' ORDER BY column DESC/ASC";
Or if there is not count column try with
$sql = "SELECT * FROM `table` WHERE `id` = '$id' ORDER BY COUNT(column) DESC/ASC";
Hi I have a system where I only want to display the last 300 records from MYSQL, normally i would just write the query like this LIMIT 300
the problem i have is i am using a pagination system which writes the query like this.
$tableName="masterip_details";
$targetpage ="raw_data.php";
$limit = 30;
$query = "SELECT COUNT(*) as num FROM $tableName where type='6' AND country_code='GB'";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
// Get page data
$query1 = "SELECT * FROM $tableName where type='6' AND country_code='GB' LIMIT $start, $limit";
$result = mysql_query($query1);
The problem is because it uses the limit to calculate the start and finish page numbers i am not sure if i can limit the number of rows to return whilst using the pagination.
select * from (SELECT * FROM $tableName where type='6' AND country_code='GB' order by AUTO_INCERMENT_ID DESC LIMIT 300) as a order by AUTO_INCERMENT_ID ASC LIMIT $start, $limit
I created a pagination by roughly following this link:
http://www.awcore.com/dev/1/3/Create-Awesome-PHPMYSQL-Pagination_en#toggle
quite cool. Although I have an issue with my query.
It works fine like this:
require 'includes/function.php';
$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
$limit = 8;
$startpoint = ($page * $limit) - $limit;
$statement = "cars WHERE deleted = 'no'";
$query = mysql_query("SELECT * FROM {$statement} LIMIT {$startpoint}, {$limit} ");
while ($row = mysql_fetch_assoc($query)) {
However when I try to add an ORDER BY to this, like so:
require 'includes/function.php';
$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
$limit = 8;
$startpoint = ($page * $limit) - $limit;
$statement = "cars WHERE deleted = 'no'";
$query = mysql_query("SELECT * FROM {$statement} LIMIT {$startpoint}, {$limit} ORDER BY model DESC");
while ($row = mysql_fetch_assoc($query)) {
or just change the statement like this:
$statement = "rcr_cars WHERE deleted = 'no' ORDER BY model DESC";
I get this error:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in filepath/myfile.php on line 79.
Line 79 is this line:
while ($row = mysql_fetch_assoc($query)) {
Can anyone tell me how I am not using the ORDER BY correctly, its got me puzzled. :/
Try the query as below
$query = mysql_query("SELECT * FROM {$statement} ORDER BY model DESC LIMIT {$startpoint}, {$limit} ");
Where you have gone wrong is LIMIT should come after ORDER BY. Read more
Change the query:
$query = mysql_query("SELECT * FROM {$statement} ORDER BY model DESC LIMIT {$startpoint}, {$limit}") ;
$query = mysql_query("SELECT * FROM {$statement} ORDER BY model DESC LIMIT {$startpoint}, {$limit} ");
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);
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)