please correct this navigation script

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
User avatar
harsha
Forum Contributor
Posts: 103
Joined: Thu Jul 11, 2002 1:35 am
Location: Bengaluru (Bangalore) > Karnataka > India

please correct this navigation script

Post by harsha »

<html>
<head>
<?php

// if a page isn't defined, we're on page one
if($page <= 0)
{
$page = 1;
}

$i=0;

// create an array of data
$myArray = file("data.txt");
$s=sizeof($myarray);
// reverse the order of the data
$myArray = array_reverse($myArray);

// how many lines of data to display
$display = 10;

// where to start depending on what page we're viewing
$start = ($page * $display) - $display;

// the actual news we're going to print
$news = array_slice($myArray, $start, $display);
?>

</head>
<body bgcolor="Gainsboro" text="#000000">
<?php
if ($page=='' or !$page) { $page=1; }


$end=$display*$page;
$start=$end-$display;

if ($start<>'0') {
$new_page=$page-1;
$prev="<a href='view.php?page=$new_page'><---Previous page</a>";
}
else {
$prev="";
}

if ($end<$s) {
$new_page1=$page+1;
$next="<a href='view.php?page=$new_page1'>Next page---></a>";
}
else {
$next="";
}

echo "$prev";
echo "$next";

?>
<p>
<table width="90%" align="center" bgcolor="gainsboro" border="0" cellpadding="0" cellspacing="0">
<?php
// printing the data
foreach($news as $key=>$value)
{
$new=explode("#","$value");
echo "<tr><td width=\"50%\"><b>Name:</b>".$new[1]."</td><td width=\"50%\"><b>E-mail:</b>".$new[2]."</td></tr>";
echo "<tr><td colspan=2 align=justify><b>Message:</b><br>".$new[3]."</td></tr>";
echo "<tr><td><b>Posted on:</b>".$new[0]."</td><td><b>smileys:</b>"."</td></tr>";
echo "<tr><td width=\"50%\" height=\"16\"></td><td width=\"50%\" height=\"16\"></td></tr>";
}
?>
</table>
</body>
</html>
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Code: Select all

<html> 
<head> 
<?php 

// if a page isn't defined, we're on page one 
if(!$page || empty($page)) 
&#123; 
$page = 1; 
&#125; 


// create an array of data 
$myArray = file("data.txt"); 
$s=sizeof($myarray); 
// reverse the order of the data 
$myArray = array_reverse($myArray); 

// how many lines of data to display 
$display = 10; 

// where to start depending on what page we're viewing 
$start = ($page * $display) - $display; 

// the actual news we're going to print 
$news = array_slice($myArray, $start, $display); 
?> 

</head> 
<body bgcolor="Gainsboro" text="#000000"> 
<?php 


if ($start != '0') &#123; 
$new_page=$page-1; 
$prev="<a href='view.php?page=$new_page'><---Previous page</a>"; 
&#125; 
else &#123; 
$prev=""; 
&#125; 

if ($end < $s) &#123; 
$new_page1=$page+1; 
$next="<a href='view.php?page=$new_page1'>Next page---></a>"; 
&#125; 
else &#123; 
$next=""; 
&#125; 

echo "$prev"; 
echo "$next"; 

?> 
<p> 
<table width="90%" align="center" bgcolor="gainsboro" border="0" cellpadding="0" cellspacing="0"> 
<?php 
// printing the data 
foreach($news as $value) 
&#123; 
$new=explode("#","$value"); 
echo "<tr><td width="50%"><b>Name:</b>".$new&#1111;1]."</td><td width="50%"><b>E-mail:</b>".$new&#1111;2]."</td></tr>"; 
echo "<tr><td colspan=2 align=justify><b>Message:</b><br>".$new&#1111;3]."</td></tr>"; 
echo "<tr><td><b>Posted on:</b>".$new&#1111;0]."</td><td><b>smileys:</b>"."</td></tr>"; 
echo "<tr><td width="50%" height="16"></td><td width="50%" height="16"></td></tr>"; 
&#125; 
?> 
</table> 
</body> 
</html>
should work ;)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Have you read this:
http://www.devnetwork.net/forums/viewtopic.php?t=511

Mac

PS. Exactly what is the problem that you're having?
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post by gnu2php »

The problem could also be on line 15:

$s=sizeof($myarray);

Should be:

$s=sizeof($myArray); (with capital A on myArray)
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

ok, an improvement

Code: Select all

<html> 
<head> 
<?php 

// if a page isn't defined, we're on page one 
if(!isset($HTTP_GET_VARS&#1111;'page'])) 
&#123; 
$page = 1; 
&#125; 


// create an array of data 
$myArray = file("data.txt"); 
$s=sizeof($myArray); 
// reverse the order of the data 
$myArray = array_reverse($myArray); 

// how many lines of data to display 
$display = 10; 

// where to start depending on what page we're viewing 
$start = ($page * $display) - $display; 

// the actual news we're going to print 
$news = array_slice($myArray, $start, $display); 
?> 

</head> 
<body bgcolor="Gainsboro" text="#000000"> 
<?php 


if ($start != '0') &#123; 
$new_page=$page-1; 
$prev="<a href='view.php?page=$new_page'><---Previous page</a>"; 
&#125; 
else &#123; 
$prev=""; 
&#125; 

if ($end < $s) &#123; 
$new_page1=$page+1; 
$next="<a href='view.php?page=$new_page1'>Next page---></a>"; 
&#125; 
else &#123; 
$next=""; 
&#125; 

echo "$prev"; 
echo "$next"; 

?> 
<p> 
<table width="90%" align="center" bgcolor="gainsboro" border="0" cellpadding="0" cellspacing="0"> 
<?php 
// printing the data 
foreach($news as $value) 
&#123; 
$new=explode("#","$value"); 
echo "<tr><td width="50%"><b>Name:</b>".$new&#1111;1]."</td><td width="50%"><b>E-mail:</b>".$new&#1111;2]."</td></tr>"; 
echo "<tr><td colspan=2 align=justify><b>Message:</b><br>".$new&#1111;3]."</td></tr>"; 
echo "<tr><td><b>Posted on:</b>".$new&#1111;0]."</td><td><b>smileys:</b>"."</td></tr>"; 
echo "<tr><td width="50%" height="16"></td><td width="50%" height="16"></td></tr>"; 
&#125; 
?> 
</table> 
</body> 
</html>

the problem was probably some parse errors, and/or the way variables were re-stated and it's general sloppyness
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Tiny improvement on hob_goblin's code, replace:

Code: Select all

if(!isset($HTTP_GET_VARS&#1111;'page'])) 
&#123; 
$page = 1; 
&#125;
with

Code: Select all

if (!isset($_GET&#1111;'page']) &#123;
    $page = 1;
&#125; else &#123;
    $page = $_GET&#1111;'page'];
&#125;
Use $_GET['page'] if you have PHP version 4.1 or above and $HTTP_GET_VARS['page'] if you have a version less than 4.1.

You're very lucky, harsha, not everybody gets their code rewritten for them like this, it's very generous of hob_goblin to do this. For future reference (and for anyone else), please tell us exactly what the problem is and give any error messages you might be getting because it makes it easier for us to help. Posting a piece of code and just saying 'fix this' is a bit rude.

Mac
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

ignore me.
Last edited by protokol on Thu Jul 11, 2002 10:17 am, edited 1 time in total.
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post by RandomEngy »

Errr.. they ARE case sensitive.... I just did a little test:

Code: Select all

$ham = "yay";

echo $HaM;
produced no results, while

Code: Select all

$ham = "yay";

echo $ham;
did.

Maybe you have a funky version or something?
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

i just think that i posted to this forum way too early in the morning .. my brain is not awake, yet my eyes are
fatalcure
Forum Contributor
Posts: 141
Joined: Thu Jul 04, 2002 12:57 pm
Contact:

Post by fatalcure »

Copy and paste this code, should work good for you :)

Code: Select all

<html>
<head><title></title></head>

<body bgcolor="Gainsboro" text="#000000"> 

<?php
     $perpage = 10;
     if (!$page || !is_numeric($page)) $page=1;
     
     // create an array of data 
     $myArray = file("data.txt"); 
     $total=sizeof($myarray); 

     $numpages = ceil($total / $perpage);

     $prev = $page - 1;
     $next = $page + 1;

     if ($numpages > 1) &#123;
          $pagesvar = "<table width=99%><tr><td align=right>Pages: ($numpages) <b>&#1111; &nbsp;";

          if ($page - 3 > 0) $pagesvar .= "<a href='view.php?page=1'><u><<</u></a> &nbsp;&nbsp;";	
          if ($prev > 0 && $prev <= $numpages) $pagesvar .= "<a href='view.php?page=$prev'><u><</u></a> &nbsp;&nbsp;";

          for ($i=$page-2; $i < $page; $i++) &#123;
               if ($i > 0) $pagesvar .= "<a href='view.php?page=$i'><u>$i</u></a> &nbsp;&nbsp;";
          &#125;

          $pagesvar .= "$page &nbsp;&nbsp;";

          for ($i=$page+1; $i <= $page+2; $i++) &#123;
               if ($i <= $numpages) $pagesvar .= "<a href='view.php?page=$i'><u>$i</u></a> &nbsp;&nbsp;";
          &#125;

          if ($next <= $numpages && $next > 0) $pagesvar .= "<a href='view.php?page=$next'><u>></u></a> &nbsp;&nbsp;";
          if ($page + 3 <= $numpages) $pagesvar .= "<a href='view.php?&page=$numpages'><u>>></u></a> ";

          $pagesvar .= "]</b></td></tr></table>";
     &#125;

// reverse the order of the data 
     $myArray = array_reverse($myArray); 

//set starting position
     $start = ($page - 1) * $perpage;

// the actual news we're going to print 
     $news = array_slice($myArray, $start, $perpage); 

//PRINTING START
     echo "
          <table width='90%' align='center' bgcolor='gainsboro' border='0' cellpadding='0' cellspacing='0'>
            <tr><td align='right' colspan='2'>$pagesvar</td></tr>
     ";

foreach($news as $key=>$value) &#123; 
     $new=explode("#","$value"); 
     echo "<tr><td width='50%'><b>Name:</b>".$new&#1111;1]."</td><td width='50%'><b>E-mail:</b>".$new&#1111;2]."</td></tr>"; 
     echo "<tr><td colspan=2 align=justify><b>Message:</b><br>".$new&#1111;3]."</td></tr>"; 
     echo "<tr><td><b>Posted on:</b>".$new&#1111;0]."</td><td><b>smileys:</b>"."</td></tr>"; 
     echo "<tr><td width='50%' height='16'></td><td width='50%' height='16'></td></tr>"; 
&#125; 

     echo "
               <tr><td align='right' colspan='2'>$pagesvar</td></tr>
          </table>
     ";
?>

</body>
</html>
hope this helps 8)
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

you just had to go out-do me eh? :(

oh well, yeah about $_GET, I always like to use HTTP_GET_VARS when im helping people out.. because i try to make sure my method works no matter what. i know my host hasn't updated to 4.2 yet... so i bet there are others
User avatar
harsha
Forum Contributor
Posts: 103
Joined: Thu Jul 11, 2002 1:35 am
Location: Bengaluru (Bangalore) > Karnataka > India

hi thank you every body

Post by harsha »

hello

:) i you people are really gr8 i thanx for your effort in helping me. i am a beginner so i am enjoying lot with you people .
User avatar
harsha
Forum Contributor
Posts: 103
Joined: Thu Jul 11, 2002 1:35 am
Location: Bengaluru (Bangalore) > Karnataka > India

hi i got a doubt

Post by harsha »

mr hob_goblin

you haven't assigned any value to $end [/u ]in your script
Post Reply