prev next link headache

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Braendan
Forum Newbie
Posts: 7
Joined: Tue Jun 25, 2002 2:34 am

prev next link headache

Post by Braendan »

HI list,

I am trying to do a prev next link using php. But i seem to be having a problem and i dont know where went wrong. It would be nice if you guys could comment about it. Any help would be greatly appreciated.

Please bear with the code below cause i am very new at this.

TIA
Braendan



<?php

//
// Connects to sql server and logs in with username and password and select the required database
//

$connect=@mysql_connect("localhost","username","password") or die ("couldnt connect to sql server");

$db= @mysql_select_db("test") or die ("couldnt select database");

//
// This is where you choose the ammount you want to display in one page
//

$per_page = 5;

//
// Selects all of the data from database
//

$sql_text = ("SELECT * from pdf ORDER BY id DESC");

//
// Sets page number, if no page is specified, it will create page 1
//

if(!$page) {
$page = 1;
}
$prev_page = $page - 1;
$next_page = $page + 1;


$query = @mysql_query($sql_text);

// Sets up specified page

$page_start = ($per_page * $page) - $per_page;

$num_rows = @mysql_num_rows($query);

if($num_rows <= $per_page) {
$num_pages = 1;
}
else if (($num_rows % $per_page) == 0) {
$num_pages = ($num_rows / $per_page);
}
else {
$num_pages = ($num_rows / $per_page) + 1;
}
$num_pages = (int) $num_pages;

if (($page > $num_pages) || ($page < 0)) {
error("You have specified an invalid page number");
}
$sql_text = $sql_text . " LIMIT $page_start, $per_page";
$query = mysql_query($sql_text);

?>


<title>Output here</title>
<font face="Tahoma" size="2"> There are currently<font color="red">
<?php echo "$num_rows"; ?> </font> files in this section<p></font>


<?php

while ($result = mysql_fetch_array($query)) {

echo "<p><font face=\"Tahoma\" size=\"2\">Document ID:".$result['id']."</font>
\n";
echo "<font face=\"Tahoma\" size=\"2\">Title:".$result['title']."</font>
\n";
echo "<font face=\"Tahoma\" size=\"2\">File:</font><font face=\"Tahoma\" size=\"2\">".$result['file']."</font></p>\n";

}

// This displays the "Previous" link

if ($prev_page) {
echo "< Prev";
}

// This loops the Pages and displays individual links =

for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $page) {
echo " $i";
} else {
echo " $i ";
}
}

// This displays the "Next" link.

if ($page != $num_pages) {
echo " | Next >" ;
}

?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It would help if you explained exactly what the problem is and gave any error messages you might be recieving.

Mac
Braendan
Forum Newbie
Posts: 7
Joined: Tue Jun 25, 2002 2:34 am

problem

Post by Braendan »

Hi,

The problem is it diplays the value order by id DESC and limit by 5 but the next prev links all dont work. When i click on it, the link displays the same value. I have like 40 rows in the database.

TIA,
Braendan
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Err, where exactly are you setting the anchor (<a href="..."></a>) tags because it's not in the code above.

Mac
Braendan
Forum Newbie
Posts: 7
Joined: Tue Jun 25, 2002 2:34 am

Post by Braendan »

hrmm its in there when i pasted it, but when i submit this form all the <a href=""</a> went missing

Code: Select all

<?php 

//
// Connects to sql server and logs in with username and password and select the required database
//

$connect=@mysql_connect("localhost","username","password") or die ("couldnt connect to sql server"); 
       
$db= @mysql_select_db("test") or die ("couldnt select database"); 

//
// This is where you choose the ammount you want to display in one page
//

$per_page = 5;  

//
// Selects all of the data from database 
//

$sql_text = ("SELECT * from pdf ORDER BY id DESC");   

//
// Sets page number, if no page is specified, it will create page 1   
//

if(!$page) &#123;   
	$page = 1;   
	&#125;   
$prev_page = $page - 1; 
$next_page = $page + 1; 


$query = @mysql_query($sql_text);  

// Sets up specified page 
     
$page_start = ($per_page * $page) - $per_page;  

$num_rows = @mysql_num_rows($query);   

    if($num_rows <= $per_page) &#123;   
        $num_pages = 1;   
    &#125; 
    else if (($num_rows % $per_page) == 0) &#123;   
        $num_pages = ($num_rows / $per_page);   
    &#125; 
    else &#123;   
        $num_pages = ($num_rows / $per_page) + 1;   
    &#125;   
    $num_pages = (int) $num_pages;   

    if (($page > $num_pages) || ($page < 0)) &#123;   
       error("You have specified an invalid page number");   
    &#125;   
$sql_text = $sql_text . " LIMIT $page_start, $per_page";   
$query = mysql_query($sql_text);  

    ?> 


<title>Output here</title> 
<font face="Tahoma" size="2"> There are currently<font color="red"> 
<?php echo "$num_rows"; ?> </font> &nbsp;files in this section<p></font>


<?php 

    while ($result = mysql_fetch_array($query)) &#123;       

        echo "<p><font face="Tahoma" size="2">Document ID:".$result&#1111;'id']."</font><br>\n";
	echo "<font face="Tahoma" size="2">Title:".$result&#1111;'title']."</font><br>\n";
	echo "<font face="Tahoma" size="2">File:</font><a href="$file" target="_blank"><font face="Tahoma" size="2">".$result&#1111;'file']."</font></a></p>\n";
	
    &#125; 

    // This displays the "Previous" link   

    if ($prev_page)  &#123;  
        echo "<a href="$PHP_SELF?page=$prev_page"><&nbsp;Prev</a>";   
    &#125;  

    // This loops the Pages and displays individual links =

    for ($i = 1; $i <= $num_pages; $i++) &#123;   
        if ($i != $page) &#123;   
            echo " <a href=$PHP_SELF?page=$i>$i</a>";   
        &#125; else &#123;   
            echo " $i ";   
        &#125;   
    &#125;   

    // This displays the "Next" link. 

    if ($page != $num_pages) &#123;   
       echo "&nbsp;|<a href="$PHP_SELF?page=$next_page">&nbsp;Next&nbsp;></a>" ;  
    &#125;  

?> 
 
</body> 
</html>
Last edited by Braendan on Tue Jun 25, 2002 3:25 am, edited 1 time in total.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try editing your orginal post and enclose all of the code in code tags.

i.e. [syntax=php]your code here[/syntax]

Mac
Braendan
Forum Newbie
Posts: 7
Joined: Tue Jun 25, 2002 2:34 am

just did

Post by Braendan »

please see code above

TIA
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Had a play with your code and the following worked fine for one of my databases:

Code: Select all

<?php 
// Connects to sql server and logs in with username and password and select the required database 
$connect = @mysql_connect('localhost', 'user', 'pass') or die ('couldnt connect to sql server'); 
@mysql_select_db('db') or die ('couldnt select database');

// This is where you choose the amount you want to display in one page 
$per_page = 5; 

// Selects all of the data from database 
$sql_text = "SELECT * FROM table ORDER BY ID DESC"; 
 
// Sets page number, if no page is specified, it will create page 1
if(!isset($_GET&#1111;'page'])) &#123; 
	$page = 1; 
&#125; else &#123;
	$page = $_GET&#1111;'page'];
&#125;
$prev_page = $page - 1; 
$next_page = $page + 1; 

$query = @mysql_query($sql_text); 

// Sets up specified page 

$page_start = ($per_page * $page) - $per_page; 

$num_rows = @mysql_num_rows($query); 

if($num_rows <= $per_page) &#123; 
	$num_pages = 1; 
&#125; elseif (($num_rows % $per_page) == 0) &#123; 
	$num_pages = ($num_rows / $per_page); 
&#125; else &#123; 
	$num_pages = ($num_rows / $per_page) + 1; 
&#125; 
$num_pages = (int) $num_pages; 

if ($page > $num_pages || $page < 0) &#123; 
	echo '<p>You have specified an invalid page number.</p>'; 
&#125; 
echo $sql_text = $sql_text." LIMIT $page_start, $per_page"; 
$query = mysql_query($sql_text); 
?> 

<title>Output here</title> 
<font face="Tahoma" size="2"> There are currently<font color="red"> 
<?php echo "$num_rows"; ?> </font> files in this section<p></font> 

<?php 
while ($info = mysql_fetch_assoc($query)) &#123; 
	echo <<<END
\n<p><font face="Tahoma" size="2">Document ID: &#123;$info&#1111;'ID']&#125;</font> 
<font face="Tahoma" size="2">Title: &#123;$info&#1111;'title']&#125;</font> 
<font face="Tahoma" size="2">File: &#123;$info&#1111;'file']&#125;</font></p>\n
END;
&#125; 

// This displays the "Previous" link 
if ($prev_page != 0) &#123; 
	echo '<a href="test.php?page='.$prev_page.'"><&nbsp;Prev</a>'; 
&#125; 

// This loops the Pages and displays individual links = 
for ($i = 1; $i <= $num_pages; $i++) &#123; 
	if ($i != $page) &#123; 
		echo '&nbsp;<a href="test.php?page='.$i.'">'.$i.'</a>&nbsp;';     
	&#125; else &#123; 
		echo '&nbsp;'.$i.'&nbsp;'; 
	&#125; 
&#125; 

// This displays the "Next" link. 
if ($page != $num_pages) &#123; 
	echo '&nbsp;|<a href="test.php?page='.$next_page.'">&nbsp;Next&nbsp;></a>';
&#125;
?>
Mac
Braendan
Forum Newbie
Posts: 7
Joined: Tue Jun 25, 2002 2:34 am

moan

Post by Braendan »

sigh..must be something wrong with the php / mysql setup on my comp then. Thanks alot for your time.

Best Regards,
Braendan
Post Reply