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.
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]
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. ^^;
  • 4 years later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.