Search
Left Quote    A clever person solves a problem. A wise person avoids it.
- Einstein    
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: "re: PHP Pagination tutorial questio..." Posted: @ 11:50 pm on Aug 09 2006    
Member #: 186
Rank: User - (31)
Since: 07/21/06
Posts: 31
From: Devon

I tried inserting a new record and I get this error message

Warning: Invalid argument supplied for foreach() in /home/adrock/pu blic_html/jack/ admin/addnews.p hp

It successfully adds the news to the first table but no the article_data table

This is where the error lies

Code:

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




    Viewed: 1,417 Times | Reply to This | To top
  This user is offline  misterhaan
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 6:50 pm on Aug 10 2006    

Member #: 5
Rank: User - (213)
Since: 02/11/05
Posts: 148
From: chair

that should mean that $_POST['page' ] is not an array -- you might want to do print_r($_POST[ 'page']); right before the foreach to check what's in there.




please note that the above post is likely made up in its entirety.

    Viewed: 1,412 Times | Reply to This | To top
  This user is online  Guest
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 6:57 pm on Aug 10 2006    
Unregistered

also, check if you're using "pg" ; or "page&qu ot; as page; $_POST['page' ] may not exist, if you're using something else (like pg).



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

Here is the code that is supposed to insert into the 2 tables

Code:

include_once( "../includ es/connection.p hp";
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 news VALUES ('','$name' ,'$message',n ow(),'$pic') ";
mysql_query($qu ery);

//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 ());
}


Where is the $_POST['page' ] coming from?



Edited at 09:04:21 pm on 08/10/06


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

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

well, your form to add an article would need to be broken up into pages, and $_POST['page' ] would be the page data for that page #. Loop through the pages using FOR EACH.




-bs0d | AllSyntax.com

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

That makes more sense but on the form itself how am i inserting the page numbers?

Would I need

$page = $_POST['page' ];
<input type="hid den" name="pag e">

becuase there is nothing posting the page number




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

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

Well, take a look at my tutorial contribute form. I have a field that asks how many pages and what type, then hit continue. So on the next part, i have the # of pages with the tutorial type. I believe I simply used a while loop to loop a textarea until loop count <= $_POST['pages '].

Gather your thoughts, think about this logically and bring it back down to basics. None of it is complex or rocket science. Everything you need to know has been mentioned in this thread.




-bs0d | AllSyntax.com

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

I have been looking at the pagination tutorial and the comments script tutorial and I think it's becoming clearer.

On my form that i use to insert the news do i put this (like in your comments script)

Code:

<input type="hid den" name="pag e" value="&l t;? echo($_SERVER[ 'REQUEST_URI'] ); ?>"> ;


then in the insert SQL use this?

Code:
$q ="INSERT INTO `news` (article_id, page) VALUES ('".$_GE T['id'].&quo t;', '".$_POS T['page'];&q uot;

< br /> This in theory makes sense to me

Also I did ask this before but on the pagination tutorial you have $cat which is the category. Can I replace the cat with id?
I think i may be missing a bit from my code becuase I saw no use for it or i couldn't get it to work. Also i noticed that $type doesn't seem to do anything




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

I haven't made any changes to the pagaination code but I have something that nearly works. On the page where I list the 5 newest articles i have made some changes

Code:

include_once( "includes/ connection.php ";
$q = mysql_query(&q uot;SELECT id, title FROM news ORDER BY id DESC LIMIT 5";
echo "<OL id=\"bla h\"> ";
while($articles = mysql_fetch_obj ect($q)) {
echo "<LI&g t;<a class=\" one\" href=\"h ttp://www.proje ct-sw.co.uk/jac k/index.php?pag e=news&limi t=1&pagenum =".$artic les->id.&qu ot;\"> ;".$artic les->title. "</a> ;</LI>&q uot;;
}
echo"< /OL>";
?>



I have set the limit to 1 because all I want is 1 article per page. What i have noticed by doing it this way is that the pagenum and the id of the field are reversed. If I click on article #1 it directs to the id #1 which should be the last article listed. What it should do is direct to page #1 where the current id would be for the last post.

Any ideas how to get around this....it's probably simple but I can't work it out




    Viewed: 1,385 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 5:38 pm on Aug 12 2006    

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

I think we're talking about four different things:

  1. pagination
  2. displaying articles from 2 tables
  3. inserting articles w/ page numbers
  4. displaying a list of 5 newest articles.
  5. < /ol>Im confused, how about we see a URL of the problem and what its not doing so we can get a visual and go from there.




-bs0d | AllSyntax.com

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

http://www.proj ect-sw.co.uk/ja ck/

Click on the latest news and you'll see what I mean



Edited at 09:33:27 pm on 08/12/06


    Viewed: 1,379 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 11:03 pm on Aug 12 2006    

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

Ok, you need in the url the id of the article, and use that as the page #. 1 article per page.

http://www.proj ect-sw.co.uk/ja ck/news/view.ph p?id=1 (page 1)
http://www.proj ect-sw.co.uk/ja ck/news/view.ph p?id=2 (page 2)
http://www.proj ect-sw.co.uk/ja ck/news/view.ph p?id=3 (page 3)




-bs0d | AllSyntax.com

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

Have you got an example of what i can put in this code


Code:

include_once( "includes/ connection.php ";
$q = mysql_query(&q uot;SELECT id, title FROM news ORDER BY id DESC LIMIT 5";
echo "<OL id=\"bla h\"> ";
while($articles = mysql_fetch_obj ect($q)) {
echo "<LI&g t;<a class=\" one\" href=\"h ttp://www.proje ct-sw.co.uk/jac k/index.php?pag e=news&id= ".$article s->id." ;\"> ".$article s->title.&q uot;</a>& lt;/LI>&quo t;;
}
echo"< /OL>";



This currently only displays the first page

Also I was looking at the mod rewrite tutorial
On this line rewrite_base /tutorials/< br> can the root folder be put here as my news page is in this directory?




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

I managed to sort it and it was quite easy all along.

I just added a counter for the page number starting at 1 and add 1 when it reaches the end of the loop

Code:

$counter = 1;
$q = mysql_query(&q uot;SELECT id, title FROM news ORDER BY id DESC LIMIT 5";
echo "<OL id=\"bla h\"> ";
while($articles = mysql_fetch_obj ect($q)) {
echo "<LI&g t;<a class=\" one\" href=\"h ttp://www.proje ct-sw.co.uk/jac k/index.php?pag e=news&limi t=1&pagenum =".$count er."\&q uot;>" .$articles-> title."&l t;/a></LI >";
$counter = $counter + 1;
}
echo"< /OL>";




    Viewed: 1,365 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: PHP Pagination tutorial questio..." Posted: @ 2:35 am on Aug 15 2006    

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

great, glad you got your answer!




-bs0d | AllSyntax.com

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


« Prev.
1 | 2 |

This Topic is Locked






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