Search
Left Quote    Black holes are where God divided by zero.
- Steven Wright    
Right Quote
 
[login] | [Register]
Forum Index » PHP and mySQL » Viewing Topic and Replies
Post Reply


        Viewing Topic: PHP Pagination tutorial question [RESOLVED]
  This user is offline  AdRock
  Subject: "PHP Pagination tutorial question [R..." Posted: @ 12:43 pm on Jul 21 2006   
Member #: 186
Rank: User - (31)
Since: 07/21/06
Posts: 31
From: Devon

I have used the code in the PHP pagination tutorial

http://www.alls yntax.com/tutor ials/PHP/23/Pag ination-with-PH P/2.php

and have noticed soemthing that doesn't make sense to me.

I have edited the code so I can connect to the database but when i run it, I get an error saying "Unknown column 'cat' in 'where' clause" and it points to this line

Code:
$t = mysql_query(&qu ot;SELECT * FROM `news` WHERE `cat` = '".addslas hes($_GET['cat' ])."'" ;
if(!$t) die(mysql_error ());[


I know there is no 'cat' column in my database so what do i need to put there in it's place or do i have to create a new column?Link Text Here

Edited at 01:50:05 pm on 08/14/06


    Viewed: 37,953 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 5:21 pm on Jul 21 2006    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

cat is short for category. cat is if your news goes like this:

mysite.com/news .php?cat=sports

$_GET['cat'] in the WHERE clause will select all (*) tutorials in the news database where the category = sports.




-bs0d | AllSyntax.com

    Viewed: 36,587 Times | Reply to This | To top
  This user is offline  AdRock
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 7:10 pm on Jul 21 2006    
Member #: 186
Rank: User - (31)
Since: 07/21/06
Posts: 31
From: Devon

  bs0d said...

< i> cat is short for category. cat is if your news goes like this:

mysite.com/news .php?cat=sports

$_GET['cat'] in the WHERE clause will select all (*) tutorials in the news database where the category = sports.


What if I haven't got a category. All i have in my database is an ID, title, content and timestamp.




    Viewed: 36,585 Times | Reply to This | To top
  This user is offline  AdRock
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 9:12 pm on Jul 21 2006    
Member #: 186
Rank: User - (31)
Since: 07/21/06
Posts: 31
From: Devon

Right....I have figureed out that but I have come across a new problem

I use a switch statement to display each of my pages (only some of them are shown to save space)

Code:


<?php
switch ($_GET['page' ])
{
case "contact ":
include('conta ct.php');
break;

default:
include('home. php');
}
?>


and i have a pagination script to display a set number of record per page. The first page displays ok but when i click on the next page link it defaults back to the default page (obviuosly because the switch statement isn't set up to handle it)
The link that appears in the browser window is http://www.mysi te.co.uk/index. php?page=newsit em?limit=2& page=2 but no page is displayed

How do I sort this problem out?




    Viewed: 36,581 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 11:35 pm on Jul 21 2006    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA




-bs0d | AllSyntax.com

    Viewed: 36,577 Times | Reply to This | To top
  This user is offline  AdRock
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 10:00 am on Jul 23 2006    
Member #: 186
Rank: User - (31)
Since: 07/21/06
Posts: 31
From: Devon

Can anyone please help me insert this line of html and javascript into my php code?

I have a radio button which when clicked will open delete_news.php
I want to put that in the php code so every record will have the radio button next to it so it can be deleted.


Code:

<input style="bo rder:none;&quo t; type="rad io" name="loc " onClick=" go('index.php? page=delete_new s');"> ;


I would like the HTML to replace the <h3> but when I escape the quotes(\" i get a parse error saying unexpected > on line 47 (the echo line below)

Code:
while($code = mysql_fetch_obj ect($q)) {
echo(" <h3> ".$code-&g t;title." </h3>< BR>";}




    Viewed: 36,568 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 11:40 pm on Jul 24 2006    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

heres javascript code I found right off google:


Code:


<script language=" ;JavaScript&qu ot;>
function radio_input(url )
{
// Re-direct the browser to the url value
window.location .href = "http:// " + url
}
</script>
<form>
<input type="rad io" name="sel ectname"
onClick=" JavaScript:radi o_input('www.y ahoo.com')&qu ot;>



and what do you mean you would like HTML to replace the H3?




-bs0d | AllSyntax.com

    Viewed: 36,549 Times | Reply to This | To top
  This user is offline  AdRock
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 10:05 pm on Jul 30 2006    
Member #: 186
Rank: User - (31)
Since: 07/21/06
Posts: 31
From: Devon

I have a new question about the tutorial.

What I would like to do is something similar to using tables and alternating the row colours. i don't want to use tables as I would like to position an image and would like to use a div instead.

Is there a way I can edit the code so I can alternate 2 div classes instead of 2 table rows becuase I would like to align an image differently in each class.

I have tried using this that i got from another pagination script that uses table rows ( but i have tried to change it to divs)


Code:

$code = mysql_fetch_arr ay($q);
if($i % 2) { //this means if there is a remainder
echo "<div class=\" news\" id=\"top \">\ n";
} else { //if there isn't a remainder we will do the else
echo "<div class=\" news\" id=\"bot tom\"> ;\n";
}
echo "<b> ;".$code[ 'title'].&qu ot;</b>&l t;br>" .$code['time' ]."<br >".$co de['content'] ."<img src=\"im ages/jack1.jpg \">< ;br><br&g t;\n";
echo "</div >\n";



The above code doesn't work very well becuase it only displays one record per page even if there are other records on the page



Edited at 10:17:06 pm on 07/30/06


    Viewed: 36,527 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 11:18 pm on Jul 30 2006    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

yea, its only going to show one, you've gotta put it in a while loop and say:

Code:


$count = 1
while($code = mysql_fetch_obj ect($query) {
// echo code
if($count % 2 == 1) {
echo('<div class="co lor1"> code sample stuff...</di v>');
} else {
echo('<div class="co lor2"> code sample stuff...</di v>');
}
$count++;
} //end while


something like that i believe



Edited at 11:22:21 pm on 07/30/06


-bs0d | AllSyntax.com

    Viewed: 36,523 Times | Reply to This | To top
  This user is offline  AdRock
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 2:38 pm on Aug 01 2006    
Member #: 186
Rank: User - (31)
Since: 07/21/06
Posts: 31
From: Devon

I managed to sort that problem but I have come across another problem that i can't see what is going wrong.

In my table I have a fieldname called photo which is used to store the name of an image e.g. "hello.jp g"

I edited this line so it reads

Code:

while($code = mysql_fetch_arr ay($q)) {
echo "<b> ;".$code[ 'title'].&qu ot;</b>&l t;br><i&g t;Date added: ".$code[ 'time']." ;</i>< br><br> ;".nl2br( $code['content '])."< ;br><img src=http://www. mysite.com/imag es/".$cod e['photo'].& quot;> <br>\n& quot;;
}



When I run the code it doesn't display an image, just where the image should be.

I am tearing my hair out with this becuase I tried a simple query on it's own in a seperate page and it displays teh picture. There must be something wrong in this code. Here is the full code i use

Code:
<h2>Fort hcoming Events</h2&g t;
<?
//REMEMBER TO CONNECT TO DATABASE!

include_once(& quot;includes/c onnection.php& quot;
@mysql_connect( $host, $user, $password) or die("ERRO R--CAN'T CONNECT TO SERVER";
@mysql_select_d b($database) or die("ERRO R--CAN'T CONNECT TO DB";
//**EDIT TO YOUR TABLE NAME, ECT.

$t = mysql_query(&q uot;SELECT * FROM `news`";
if(!$t) die(mysql_error ());

$a = mysql_fetch_obj ect($t);
$total_items = mysql_num_rows( $t);
$limit = $_GET['limit' ];
$type = $_GET['type'] ;
$page = $_GET['pagenum '];

//set default if: $limit is empty, non numerical, less than 1, greater than 50
if((!$limit) || (is_numeric($li mit) == false) || ($limit < 1) || ($limit > 50)) {
$limit = 1; //default
}
//set default if: $page is empty, non numerical, less than zero, greater than total available
if((!$page) || (is_numeric($pa ge) == false) || ($page < 0) || ($page > $total_items)) {
$page = 1; //default
}

//calcuate total pages
$total_pages = ceil($total_ite ms / $limit);
$set_limit = $page * $limit - ($limit);

//query: **EDIT TO YOUR TABLE NAME, ECT.

$q = mysql_query(&q uot;SELECT * FROM `news` LIMIT $set_limit, $limit";
if(!$q) die(mysql_error ());
$err = mysql_num_rows( $q);
if($err == 0) die("No matches met your criteria." ;

//Results per page: **EDIT LINK PATH**
echo("
<a href=?page=news &limit=10&a mp;amp;pagenum= 1></a>
<a href=?page=news &limit=25&a mp;amp;pagenum= 1></a>
<a href=?page=news &limit=50&a mp;amp;pagenum= 1></a> ";

//show data matching query:
while($code = mysql_fetch_arr ay($q)) {
echo "<b> ;".$code[ 'title'].&qu ot;</b>&l t;br><i&g t;Date added: ".$code[ 'time']." ;</i>< br><br> ;".nl2br( $code['content '])."< ;br><img src=http://www. mysite.com/imag es/".$cod e['photo'].& quot;> <br>\n& quot;;
}

$id = urlencode($id); //makes browser friendly

//prev. page: **EDIT LINK PATH**

$prev_page = $page - 1;

if($prev_page >= 1) {
echo("< ;b>&lt;& amp;lt;</b&g t; <a href=?page=news &limit=$lim it&amp;page num=$prev_page& gt;<b>Pre v.</b>< ;/a>";
}

//Display middle pages: **EDIT LINK PATH**

for($a = 1; $a <= $total_pages; $a++)
{
if($a == $page) {
echo("< ;b> $a</b> | "; //no link
} else {
echo(" <a href=?page=news &limit=$lim it&amp;page num=$a> $a </a> | ";
}
}

//next page: **EDIT THIS LINK PATH**

$next_page = $page + 1;
if($next_page <= $total_pages) {
echo("< ;a href=?page=news &limit=$lim it&amp;page num=$next_page& gt;<b>Nex t</b>< /a> &gt; &gt;"< img src=http://www. allsyntax.com/f orums/images/sm ilies/wink.gif border=0>;
}

//all done
?>




    Viewed: 36,510 Times | Reply to This | To top
  This user is offline  AdRock
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 5:36 pm on Aug 01 2006    
Member #: 186
Rank: User - (31)
Since: 07/21/06
Posts: 31
From: Devon

I think i sorted it now......I don't think i changed anything but it works now




    Viewed: 36,503 Times | Reply to This | To top
  This user is offline  AdRock
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 10:08 pm on Aug 02 2006    
Member #: 186
Rank: User - (31)
Since: 07/21/06
Posts: 31
From: Devon

I have had a thought about something I want to do.

somewhere on the home page I want to have a list of the 5 newst entries in the news table. That isn't a problem.

How would I create a link to the position in the table so that it directs to the right page within the pagination? Say it was ID number 15 and it was in about the page number 3....how would i link to that so a viewer can move next/previous records after reading that one?

I was thinking of something like this but better

Code:

while ($row = mysql_fetch_arr ay($result)){?& gt;
<?echo $row['title'] ;?><a href="new s.php?id=<? echo $row['id']; ?>"> ;</a>
<a href="edi t.php?id=<? echo $row['id']; ?>"> ;</a>
<?}




    Viewed: 36,491 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 10:40 pm on Aug 02 2006    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

Not exactly sure if im understanding your question 100% or not, but if you want to send them to the 3rd page of the article id 15, then that link would be like this:

www.yoursite.co m/articles/view .php?id=15& page=3

if you have pagination done properly, they can click "next page" at the bottom to go to the next and so on. So I would send them to the 1st page, and they can read from there.




-bs0d | AllSyntax.com

    Viewed: 36,487 Times | Reply to This | To top
  This user is offline  AdRock
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 9:03 am on Aug 03 2006    
Member #: 186
Rank: User - (31)
Since: 07/21/06
Posts: 31
From: Devon

Yeah...i thought something like that

I put www.yoursite.co m/articles/view .php?id=15 (one that is not on the first page) and it jumps to the first page. If news are updating quite regularly I'm not sure what page number it's going to be on, so is there a way around this? If not I'll have to scrap the idea

The idea was to have a list of the 5 latest news entries so the viewer can click [read] and go straight to the article




    Viewed: 36,480 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 12:15 am on Aug 04 2006    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

I designed the database to have 2 tables for articles.
1 table holds all of the info about the article (id, title, type, author, date... so on). The next table holds the article infomation (id, article id & page #, page data, ect.).

So in that case, I just SELECT * from `news_data` WHERE `article_id` = ".$_GET[ 'id']." , use mysql_num_rows to get the total # of pages.

So in the end, what looks like pagination is just really reflecting the pages associated with that article in the database. Use a while loop to display the page #'s. Understand where im coming from?




-bs0d | AllSyntax.com

    Viewed: 36,469 Times | Reply to This | To top
  This user is offline  AdRock
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 12:49 am on Aug 05 2006    
Member #: 186
Rank: User - (31)
Since: 07/21/06
Posts: 31
From: Devon

A little......I have got the pagination working fine so if I set the limit to 1 or 2 (becuase the news articles can be quite long) it displays each of the articles in order. I have only used one table with field names id, title, time (timestamp), content.

What i would like to know is....I am using a while loop and set the limit in the SELECT to 5 so it picks the 5 newsest entries and lists them. What I want it to do is list each of the entries with a link to that article on that page number like you said earlier www.yoursite.co m/articles/view .php?id=15& page=3

Doing the while loop isn't the problem...it's the link to the actual article that I'm having problems with. Would it work if i created another table like you mentioned. If so what field names/types would i need




    Viewed: 36,455 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 2:49 am on Aug 05 2006    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

Well, if you want to display the 5 newest articles, you would do something like:

Code:


$q = mysql_query(&q uot;SELECT id, title FROM articles ORDER BY time DESC LIMIT 5";
echo("< ;OL>";
while($articles = mysql_fetch_obj ect($q)) {
echo("< ;LI><a href='http://w ww.yoursite.com /articles/view. php?id=". $articles->i d."'> ".$articl es->title.& quot;</a> </LI>&qu ot;
}
echo("< ;/OL>"< img src=http://www. allsyntax.com/f orums/images/sm ilies/wink.gif border=0>;



That would list the 5 newest articles sorted by time, descending.

Now, I think it would be important for you to normalize your tables, expecially if you plan on having alot of articles. Split it up into 2 tables like I mentined in the post above.



Edited at 02:57:03 am on 08/05/06


-bs0d | AllSyntax.com

    Viewed: 36,449 Times | Reply to This | To top
  This user is offline  AdRock
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 1:43 pm on Aug 07 2006    
Member #: 186
Rank: User - (31)
Since: 07/21/06
Posts: 31
From: Devon

I have got the 5 latest news articles listed but I am still confused about the second table.

You said that you have (id, article id & page #, page data, ect.), how would you insert into the second table. I suppose if you're entering data into the databse at the same time then the article_id and the id of the other table will be the same, but how does you insert the page #? What are the fields that you would use for the second table?

Would by doing this move to the correct page witht the selected article on it so the viewer can move back and forward through the articles?

This is the code i use for the query

Code:

$q = mysql_query(&q uot;SELECT id,title,conten t, DATE_FORMAT(tim e, '%W %D %M %Y') as date, photo FROM news ORDER BY time DESC LIMIT $set_limit, $limit";



Edited at 02:07:16 pm on 08/07/06


    Viewed: 36,397 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 11:41 pm on Aug 07 2006    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

Right underneath the query that you just inserted the article information in for (title, type ,description, and so on..) use this code to insert each page as a seperate row for the article added:

Code:


//get ID of article you just inserted...
$id = mysql_insert_id ();

foreach ($_POST['page '] as $key => $body) {
$page = $key + 1;

//add article data (pages/content) to article_data table...
$q = "INSERT INTO `article_data` (article_id, body, page) VALUES ('$id', '$body', '$page')&quo t;;
$q2 = mysql_query($q) ;
if(!$q2) die(mysql_error ());
}




-bs0d | AllSyntax.com

    Viewed: 36,387 Times | Reply to This | To top
  This user is offline  AdRock
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 12:32 am on Aug 08 2006    
Member #: 186
Rank: User - (31)
Since: 07/21/06
Posts: 31
From: Devon

I created a new table called article_data and I created the following fields

article id - INT (auto_increment )
body - VARCHAR
page - VARCHAR

This is the code I use on the form to insert the news record into the news table

Code:

<?php
// This is displayed if all the fields are not filled in
$empty_fields_m essage = "<p> ;Please go back and complete all the fields in the form.</p> Click <a class=\" two\" href=\"j avascript:histo ry.go(-1)\&qu ot;>here< /a> to go back";
// This is displayed when the email has been sent
$thankyou_messa ge = "<p class=\" style3\" >Thankyou. News successfully added.</p> ;<p>Click <a class=\" two\" href=\"i ndex.php\&quo t;>here</ a> to go back to Jack Godfrey Honeylands Support Fund Admin Area main menu.</p> ";
//This is the directory where images will be saved
$target = "../image s/";
$target = $target . basename( $_FILES['photo ']['name']);
//This gets all the other information from the form
$name = stripslashes($_ POST['txtName ']);
$event = stripslashes($_ POST['txtEvent ']);
$message = stripslashes($_ POST['txtMessa ge']);
$pic=($_FILES[ 'photo']['nam e']);
if (!isset($_POST[ 'txtName'])) {
?>
<h3>Jack Godfrey Honeylands Support Fund Admin Area.</h3> ;
<h3 style="co lor:#060B69;&q uot;>Add a forthcoming event</h3> ;
<form enctype=" multipart/form- data" method="p ost" action="& lt;?php echo $_SERVER['REQU EST_URI']; ?>"> ;
<p class="st yle1"> Please enter a title for the event.
<input style="wi dth:400px;&quo t; type="tex t" title="Pl ease enter a title for the event" name="txt Name" size="30 "/>< /p>
<p class="st yle1"> Please enter a date for the event.
<input id="calen dar" style="wi dth:400px;&quo t; type="tex t" title="Pl ease enter the event date" size="30 " name="txt Event"> ;<a href="jav ascript:NewCal( 'calendar',' ddmmyyyy')&qu ot;><img src="../i mages/cal.gif& quot; width="20 " height="2 0" border="0 " alt="Pick a date"> </a></ p>
<p class="st yle1"> Please enter the content for the event.
<textarea style="wi dth:400px;&quo t; title="Pl ease enter the content for the event" name="txt Message" rows="10 " cols="30 "></ textarea>< ;/p>
<p class="st yle1"> Please select an image for the event.
<input style="wi dth:400px;&quo t; type="fil e" name="pho to" title="Pl ease select an image for the event" size="30 "></ p>
<p class="st yle3">
<input class="su bmit-button&qu ot; style="ma rgin-left:0&qu ot; type="Sub mit" value="Su bmit"> <input class="su bmit-button&qu ot; style="ma rgin-left:2px& quot; type="res et" value="Re set">< br /> </form> <p>Click <a class="tw o" href="ind ex.php"&g t;here</a> ; to go back to Jack Godfrey Honeylands Support Area admin area</p>< br /> <?php
}
elseif (empty($name) || empty($event) || empty($message) ) {
echo $empty_fields_m essage;
}
else {
// Stop the form being used from an external URL
// Get the referring URL
$referer = $_SERVER['HTTP _REFERER'];
// Get the URL of this page
$this_url = "http:// ".$_SERVER ['HTTP_HOST'] .$_SERVER[&quo t;REQUEST_URI& quot;];
// If the referring URL and the URL of this page don't match then
// display a message and don't send the email.
if ($referer != $this_url) {
echo "You do not have permission to use this script from another URL.";
exit;
}
include_once(& quot;../include s/connection.ph p";
// The URLs matched so send the email
mysql_connect($ host,$user,$pas sword);
@mysql_select_d b($database) or die( "Unable to select database"< img src=http://www. allsyntax.com/f orums/images/sm ilies/wink.gif border=0>;
$query = "INSERT INTO events VALUES ('','$name' ,'$event','$ message','$pi c')";
mysql_query($qu ery);

//Writes the photo to the server
if(move_uploade d_file($_FILES[ 'photo']['tm p_name'], $target))
{
//Tells you if its all ok
echo "The file ".basenam e( $_FILES['uploa dedfile']['na me']). " has been uploaded, and your information has been added to the directory" ;;
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
mysql_close();< br /> // Display the thankyou message
echo $thankyou_messa ge;
}
?>


This is the code from you're tutorial. i edited it to make it work for me but it may need editing further to make it work how it's supposed to

Code:
<?
//REMEMBER TO CONNECT TO DATABASE!

include_once(& quot;includes/c onnection.php& quot;
@mysql_connect( $host, $user, $password) or die("ERRO R--CAN'T CONNECT TO SERVER";
@mysql_select_d b($database) or die("ERRO R--CAN'T CONNECT TO DB";
//**EDIT TO YOUR TABLE NAME, ECT.

$t = mysql_query(&q uot;SELECT * FROM `news`";
if(!$t) die(mysql_error ());

$a = mysql_fetch_obj ect($t);
$total_items = mysql_num_rows( $t);
$limit = $_GET['limit' ];
$type = $_GET['type'] ;
$page = $_GET['pagenum '];

//set default if: $limit is empty, non numerical, less than 1, greater than 50
if((!$limit) || (is_numeric($li mit) == false) || ($limit < 2) || ($limit > 50)) {
$limit = 1; //default
}
//set default if: $page is empty, non numerical, less than zero, greater than total available
if((!$page) || (is_numeric($pa ge) == false) || ($page < 0) || ($page > $total_items)) {
$page = 1; //default
}

//calcuate total pages
$total_pages = ceil($total_ite ms / $limit);
$set_limit = $page * $limit - ($limit);

//query: **EDIT TO YOUR TABLE NAME, ECT.

$q = mysql_query(&q uot;SELECT id,title,conten t, DATE_FORMAT(tim e, '%W %D %M %Y') as date, photo FROM news ORDER BY time DESC LIMIT $set_limit, $limit";
if(!$q) die(mysql_error ());
$err = mysql_num_rows( $q);
if($err == 0) die("No matches met your criteria." ;
$numofrows = mysql_num_rows( $q);
//Results per page: **EDIT LINK PATH**
echo("
<a href=?page=news &limit=10&a mp;amp;pagenum= 1></a>
<a href=?page=news &limit=25&a mp;amp;pagenum= 1></a>
<a href=?page=news &limit=50&a mp;amp;pagenum= 1></a> ";

//show data matching query:
while($code = mysql_fetch_arr ay($q)) {

echo "<div id=\"rig ht\"> <h3>&quo t;.$code['titl e']."< ;/h3><i&g t;Date added: ".$code[ 'date']." ;</i>< p class=\" style3\" ><img src=http://www. project-sw.co.u k/jack/images/ ".$code[' photo']." ;>".nl 2br($code['con tent'])." ;</p>< /div>\n&qu ot;;
}

$id = urlencode($id); //makes browser friendly

//prev. page: **EDIT LINK PATH**

$prev_page = $page - 1;

if($prev_page >= 1) {
echo("< ;b>&lt;& amp;lt;</b&g t; <a href=?page=news &limit=$lim it&amp;page num=$prev_page& gt;<b>Pre v.</b>< ;/a>";
}

//Display middle pages: **EDIT LINK PATH**

for($a = 1; $a <= $total_pages; $a++)
{
if($a == $page) {
echo("< ;b> $a</b> | "; //no link
} else {
echo(" <a href=?page=news &limit=$lim it&amp;page num=$a> $a </a> | ";
}
}

//next page: **EDIT THIS LINK PATH**

$next_page = $page + 1;
if($next_page <= $total_pages) {
echo("< ;a href=?page=news &limit=$lim it&amp;page num=$next_page& gt;<b>Nex t</b>< /a> &gt; &gt;"< img src=http://www. allsyntax.com/f orums/images/sm ilies/wink.gif border=0>;
}

//all done
?>


I tried what you said about insertting that code but i get an error message Invalid argument supplied for foreach().

Can you have a look at my code and tell me where i'm going wrong please. I really appreciate all your help and this pagination has to be far the best that I have seen Once I know how to use it properly I'll use it again....Many thanks




    Viewed: 36,383 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 1:44 am on Aug 08 2006    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

What i've been describing is a mock-pagination , for articles only.

if you have your articles stored in 2 tables as described, you can simply echo the pages available (from article_data table, where article_id = $_GET['id']) in pagination form, like this:


Code:


$p = mysql_query(&q uot;SELECT `page` FROM `article_data` WHERE `article_id` = '".$_GET ['id']." ;'";
while($results = mysql_fetch_obj ect($p)) {
echo("< ;a href='www.your site.com/articl es/view.php?id= ".$_GET[ 'id']."& amp;amp;page=& quot;.$results- >page." ;'> ".$result s->page.&qu ot;</a>& quot;
}



Something like that. Basically you already paginated the material by normalizing your tables, so just grab the page #'s for the article and list them out as links. Cuts down on the actual PHP processing, which should be more efficient.




-bs0d | AllSyntax.com

    Viewed: 36,380 Times | Reply to This | To top
Viewing Page: 1 of 2


1 | 2 |
Next »





"AllSyntax.com" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank