Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8432921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:15:02+00:00 2026-06-10T06:15:02+00:00

I am trying to paginate an array made of MySQL results. if (!$db_server) die(Unable

  • 0

I am trying to paginate an array made of MySQL results.

if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());

mysql_select_db($db_database, $db_server)
    or die("Unable to select database: " . mysql_error());

if (isset($_GET['page']))
{
    $page = $_GET['page'];
}
else
{
    $page = 1;
}

$query = "SELECT * FROM blog";
$result = mysql_query($query);
$totalrows = mysql_num_rows($result);
$rowsperpage = 4;
$pages = ceil($totalrows / $rowsperpage);

$limit = 'LIMIT ' . ($page - 1) * $rowsperpage . ',' . $rowsperpage;
$query = "SELECT * FROM blog";
$result = mysql_query($query);

for ($j=0; $j < $totalrows; ++$j)
{
    $results[] = mysql_fetch_array($result);
}

foreach($results as $id)
    $sortAux[] = $id['id'];

array_multisort($sortAux, SORT_DESC, $results);

for ($j=0; $j < $rowsperpage; ++$j)
{
    echo "<h1>" . $results[$j][0] . "</h1>";
    echo "<hr />";
    echo "<p>" . stripslashes(emoticons($results[$j][2])) . "</p>";
    echo "<p style=\"font-size:12px;color:red;\">Posted at " . $results[$j][3] . " by " . $results[$j][1] . "</p>";
}

if ($page != $pages)
{
    $nextpage = $page+1;
    echo " <a href='{$_SERVER['PHP_SELF']}?page=$nextpage'>Older</a> ";
}
if ($page != 1)
{
    $prevpage = $page-1;
    echo " <a href='{$_SERVER['PHP_SELF']}?page=$prevpage'>Newer</a> ";
}

function emoticons ($text)  
{ 
   $emoticons = array(  
    ":)" => "<img src='emoticons/12.png'>",
    ":(" => "<img src='emoticons/13.png'>",
    ";)" => "<img src='emoticons/4.png'>",
    ":D" => "<img src='emoticons/7.png'>",
    ":P" => "<img src='emoticons/10.png'>",
    "<3" => "<img src='emoticons/3.png'>",
); 
    return str_replace(  
    array_keys($emoticons),  
    $emoticons,  
    $text);  
}
?>

The problem is the for loop. I can’t figure out on how to get 4 different results from each page. By the way it needs to be sorted from newest to oldest. That’s why I used array_multisort. Thanks.

Note: I know I need to upgrade to mysqli but for now I’m using the old mysql_*.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:15:03+00:00Added an answer on June 10, 2026 at 6:15 am

    c’mon! you have fragments of the correct SQL-Code in there already…

    you have FIRST change the first query to ‘SELECT COUNT(*) FROM blog’ in combination with $x = mysql_fetch_array($result); $totalrows = $x[0];

    and then fire up the paginated SQL-query:

    $limit = 'LIMIT ' . ($page - 1) * $rowsperpage . ',' . $rowsperpage;
    $query = "SELECT * FROM blog ORDER BY id DESC {$limit}";
    

    remove the multisort-thingy etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm am trying to paginate a set of results from a database query using
I'm trying to make Cakephp paginate take advantage of the SQL_CALC_FOUND_ROWS feature in mysql
I'm trying to make will_paginate's :order to work with this array: @posts = current_user.subscribed_tags.map(&:posts).flatten.paginate(:page
I am trying to paginate the results of an SQL query for use on
i'm trying to paginate my results, the limit is working, the number of pages
I'm trying to use custom query in Cake then paginate the results with the
I am trying to paginate the results using LIMIT keyword for 30 RECORDS in
I am new to the whole php, sql, jquery. I am trying to paginate
Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
I'm trying to make a paginate mechanism for our product documents stored in MongoDB.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.