Jump to content

How would I make a script make multiple pages


Shadow Hatake

Recommended Posts

Okay so my news script is set to view only 10 pieces of news. But I want it so that it starts a new page once I have more than 10 pieces of news.

[code]<?php
require("functions.php");
include("dbconnect.php");

session_start();

head1();
body1();
new_temp();
sotw();
navbar();
    
$start = 0;
$display = 10;
$query = "SELECT * FROM news ORDER BY id DESC LIMIT $start, $display";
$result = mysql_query( $query );

if ($result) {
    while( $row = @mysql_fetch_array( $result, MYSQL_ASSOC ) ) {
        news_box( $row['news'], $row['title'], $row['user'], $row['date'], $row['id'] );
    }
    mysql_free_result($result);
} else {
    news_box( 'Could not retrieve news entries!', 'Error', 'Error', 'Error');
}
                  
footer();

mysql_close($link);
?>[/code]

I tried a few things but they failed....miserably.
Link to comment
Share on other sites

This should work. You'll probably have to adjust it some to make it display where you want.

Is there a reason you are putting all of your html into functions?

[code]<?php

require("functions.php");
include("dbconnect.php");

session_start();

head1();
body1();
new_temp();
sotw();
navbar();
    
$start = $_GET['start'];
if ($start == "" || $start < 0) {
    $start = 0;
}
$display = 10;

$query = "SELECT * FROM news ORDER BY id DESC LIMIT $start, $display";
$result = mysql_query($query);

if ($result) {
    while( $row = @mysql_fetch_array( $result, MYSQL_ASSOC ) ) {
        news_box( $row['news'], $row['title'], $row['user'], $row['date'], $row['id'] );
    }
    mysql_free_result($result);
} else {
    news_box( 'Could not retrieve news entries!', 'Error', 'Error', 'Error');
}

echo '<div style="text-align: center">';
if ($start > 0) {
    echo "<a href=\"" . $_SERVER['PHP_SELF'] . "&start=" . $start - 10 . "\">Prev page</a>";
}
echo "&nbsp;&nbsp;";
if (($start + 10) < mysql_num_rows($result);) {
    echo "<a href=\"" . $_SERVER['PHP_SELF'] . "&start=" . $start + 10 . "\">Next page</a>";
}
echo '</div>';
          
footer();

mysql_close($link);
?>[/code]
Link to comment
Share on other sites

well i tested it by replacing the 10's with 3's and it won't show the next/prev page at bottom, and it seems to be adding them above the last news box.

EDIT: Well after palying with a little, it shows the next/prev page thing. but now how do I get it to show after last piece of news? :/

ANOTHER EDIT: I created 2 footer functions. And that fixed the placement problem. Thanks for the code. ^^;
Link to comment
Share on other sites

  • 4 years later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.