0

I am using a PHP / MySQL pagination script which works fine, however I have trouble sorting the output accordingly.

Here parts of my script:

        $link=mysql_connect("localhost","x","x");
        mysql_select_db("x",$link);
        $q="select count(*) \"total\"  from entries";
        $ros=mysql_query($q,$link);
        $row=(mysql_fetch_array($ros));
        $total=$row['total'];
        $dis=3;
        $total_page=ceil($total/$dis);
        $page_cur=(isset($_GET['page']))?$_GET['page']:1;
        $k=($page_cur-1)*$dis;

        $q="select * from entries limit $k,$dis";
        $ros=mysql_query($q,$link);
        while($row=mysql_fetch_array($ros))
        {

I've tried to change the line

    $q="select count(*) \"total\"  from entries";

to

    $q="select count(*) \"total\"  from entries id DESC";

however it does not seem to be working correctly.

Anyone has an idea how I can get this fixed and have the items sorted by ID?

Some expert help would be greatly appreciated - thank you very much.

Please find the original script I have in place here: http://allitstuff.com/php-mysql-pagination-script-download/

1
  • Did you mean: $q="select * from entries limit $k,$dis ORDER BY id DESC;" ? Commented Jan 25, 2014 at 7:24

3 Answers 3

1

The coury

select count(*) \"total\"  from entries

only returns the count. You can get only the total number of rows.

If you want to retrieve data please change your query.

If you want to manipulate with the count only, then try

$row=mysql_fetch_row($ros);
$total=$row[0];

Edit:

The OP want to sort the entries in a reverse order. So try

select column_name from entries order by column_name desc;
Sign up to request clarification or add additional context in comments.

4 Comments

thank you very much for your comment. how exactly would I need to change this? As mentioned it's a pagination script and works fine, however there seems to be no sort order in place.
Basically what I need is this script to work and just sort the entries in a reverse order (as they currently are) - I have updated the first post and added a link to the original script
@David select column_name from entries order by column_name desc;
Thank you very much for your input - as soon though as I apply any changes to the $q="select count(*) \"total\" from entries"; part the pagination stops working
0

try it

$q="select count(*) as total  from entries ORDER BY id DESC;";

1 Comment

still wouldnt matter as its just doing a count
0

Try this:

count(*) returns only count. FOR EXAMPLE

SELECT column_name1,column_name2,count(*)
FROM table_name
ORDER BY column_name1,column_name2 ASC|DESC;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.