

![]() ![]() |
| scrambled23 |
Nov 11 2006, 10:14 PM
Post
#1
|
|
Rank #1: Newbie ![]() Group: Members Posts: 16 Joined: 11-November 06 Member No.: 5,455 |
I'm trying to edit your Build A Blog script into a photoblog, but am having troubles with the Previous / Next links. Whenever I add a post, it shows up for every page. I think the example will speak better than this description. Check it out... http://fauxx.org/photo
I assume you'll have to see some part of the PHP code I'm using... But I'll wait until someone replies. |
| Alis Dee |
Nov 11 2006, 10:59 PM
Post
#2
|
![]() Rank #4: Advanced ![]() ![]() ![]() ![]() Group: Members Posts: 127 Joined: 22-November 05 Member No.: 3,379 |
I will have to see some part of the PHP code you're using.
I'm betting ten to one it's a $_POST['id'] problem. -------------------- |
| scrambled23 |
Nov 11 2006, 11:11 PM
Post
#3
|
|
Rank #1: Newbie ![]() Group: Members Posts: 16 Joined: 11-November 06 Member No.: 5,455 |
Wow, fast reply
This is my index.php CODE <?php mysql_connect ('localhost', 'OMITTED', 'OMITTED'); mysql_select_db ('OMITTED'); $sql = "SELECT * FROM php_blog ORDER BY timestamp DESC LIMIT 1"; $result = mysql_query($sql) or print ("Can't select entries from table php_blog.<br />" . $sql . "<br />" . mysql_error()); while($row = mysql_fetch_array($result)) { $date = date("l F d Y", $row['timestamp']); $title = stripslashes($row['title']); $entry = stripslashes($row['entry']); ?> <p><strong><?php echo $title; ?></strong><br /><br /> <?php echo $entry; ?><br /><br /> Posted on <?php echo $date; ?> <hr /></p> <?php $sql_prev = "SELECT * FROM php_blog WHERE id < '$id' ORDER BY id DESC LIMIT 1"; $result_prev = mysql_query ($sql_prev) or print ("Can't select previous entry id table php_blog.<br />" . $sql_prev . "<br />" . mysql_error()); while ($row = mysql_fetch_array($result_prev)) { $prev = $row['id']; } if (isset($prev)) { // print a previous link printf("<a href=\"index.php?id=%s\">Previous</a> -- ", $prev); } else { // just print the word "previous" print "Previous -- "; } ?> <?php $sql_next = "SELECT * FROM php_blog WHERE id > '$id' ORDER BY id LIMIT 1"; $result_next = mysql_query ($sql_next) or print ("Can't select next entry id table php_blog.<br />" . $sql_next . "<br />" . mysql_error()); while ($row = mysql_fetch_array($result_next)) { $next = $row['id']; } if (isset($next)) { // print a next link printf("<a href=\"index.php?id=%s\">Next</a>", $next); } else { // just print the word "next" print "Next"; } } ?> |
| Alis Dee |
Nov 12 2006, 09:06 PM
Post
#4
|
![]() Rank #4: Advanced ![]() ![]() ![]() ![]() Group: Members Posts: 127 Joined: 22-November 05 Member No.: 3,379 |
Try changing the following lines:
CODE $sql_prev = "SELECT * FROM php_blog WHERE id < '$_POST[id]' ORDER BY id DESC LIMIT 1"; CODE $sql_next = "SELECT * FROM php_blog WHERE id < '$_POST[id]' ORDER BY id DESC LIMIT 1"; In other words, you're changing $id to $_POST['id']. This is the number one most common error in this script, and has been gone over in pretty much every second post in this forum. One of those will have an explanation of why it's breaking your page. This post has been edited by Loqia Dee: Nov 12 2006, 09:08 PM -------------------- |
| scrambled23 |
Nov 12 2006, 09:44 PM
Post
#5
|
|
Rank #1: Newbie ![]() Group: Members Posts: 16 Joined: 11-November 06 Member No.: 5,455 |
Ok so now I have this
CODE <?php $sql_prev = "SELECT * FROM php_blog WHERE id < '$_POST[id]' ORDER BY id DESC LIMIT 1"; $result_prev = mysql_query ($sql_prev) or print ("Can't select previous entry id table php_blog.<br />" . $sql_prev . "<br />" . mysql_error()); while ($row = mysql_fetch_array($result_prev)) { $prev = $row['id']; } if (isset($prev)) { // print a previous link printf("<div class=\"previous\"><a href=\"index.php?id=%s\">Previous</a></div> ", $prev); } else { // just print the word "previous" print ""; } $sql_next = "SELECT * FROM php_blog WHERE id < '$_POST[id]' ORDER BY id DESC LIMIT 1"; $result_next = mysql_query ($sql_next) or print ("Can't select next entry id table php_blog.<br />" . $sql_next . "<br />" . mysql_error()); while ($row = mysql_fetch_array($result_next)) { $next = $row['id']; } if (isset($next)) { // print a next link printf("<div class=\"next\"><a href=\"index.php?id=%s\">Next</a></div>", $next); } else { // just print the word "next" print ""; } } ?> but the links don't show up anymore |
| Amelie |
Nov 12 2006, 10:43 PM
Post
#6
|
![]() (Not the film) Group: CodeGrrl Staff Posts: 5,296 Joined: 14-January 05 From: UK Member No.: 2,051 |
You need $_GET['id'] not $_POST['id'].
-------------------- -- Amelie
» N-N.net | RNM.com | NS.net » Scripts | PHPAskIt fan? "This is my signature. There are many others like it but only this one is mine." |
| scrambled23 |
Nov 12 2006, 11:14 PM
Post
#7
|
|
Rank #1: Newbie ![]() Group: Members Posts: 16 Joined: 11-November 06 Member No.: 5,455 |
No change
CODE <?php $sql_prev = "SELECT * FROM php_blog WHERE id < '$_GET[id]' ORDER BY id DESC LIMIT 1"; $result_prev = mysql_query ($sql_prev) or print ("Can't select previous entry id table php_blog.<br />" . $sql_prev . "<br />" . mysql_error()); while ($row = mysql_fetch_array($result_prev)) { $prev = $row['id']; } if (isset($prev)) { // print a previous link printf("<div class=\"previous\"><a href=\"index.php?id=%s\">Previous</a></div> ", $prev); } else { // just print the word "previous" print ""; } $sql_next = "SELECT * FROM php_blog WHERE id < '$_GET[id]' ORDER BY id DESC LIMIT 1"; $result_next = mysql_query ($sql_next) or print ("Can't select next entry id table php_blog.<br />" . $sql_next . "<br />" . mysql_error()); while ($row = mysql_fetch_array($result_next)) { $next = $row['id']; } if (isset($next)) { // print a next link printf("<div class=\"next\"><a href=\"index.php?id=%s\">Next</a></div>", $next); } else { // just print the word "next" print ""; } } ?> |
| Alis Dee |
Nov 13 2006, 01:54 AM
Post
#8
|
![]() Rank #4: Advanced ![]() ![]() ![]() ![]() Group: Members Posts: 127 Joined: 22-November 05 Member No.: 3,379 |
You need $_GET['id'] not $_POST['id']. D'oh. Of course you do. Try $_REQUEST['id'] as well. Try putting the line: CODE print_r( $_REQUEST ); Somewhere and seeing what it prints out, looking for id or the number value you are expecting. Learning to debug and to chase down 'lost' variables is Coding 101; you can follow all the tutorials you like, but you won't get anywhere without it. -------------------- |
| scrambled23 |
Nov 13 2006, 02:41 AM
Post
#9
|
|
Rank #1: Newbie ![]() Group: Members Posts: 16 Joined: 11-November 06 Member No.: 5,455 |
When I add that code it just prints Array ( )
|
| Alis Dee |
Nov 13 2006, 04:56 AM
Post
#10
|
![]() Rank #4: Advanced ![]() ![]() ![]() ![]() Group: Members Posts: 127 Joined: 22-November 05 Member No.: 3,379 |
That means you are not only are you not getting a value for $id, but the variable isn't even defined. Which is correct. If you visit index.php?id=2 it will change the output; so it hasn't 'lost' the value.
Okay, so now you know your variable is being 'received', you have to ask why it's not being used. So track down where you think you want to use it. In this case, it's the first SQL query: CODE $sql = "SELECT * FROM php_blog ORDER BY timestamp DESC LIMIT 1"; Notice how you haven't told the script to ever select anything other than the newest row in the database? That's your problem. There are lots of different ways you could try to fix this, but how about we start with: CODE if( !empty( $_REQUEST['id'] ) ) $sql = "SELECT * FROM php_blog ORDER BY timestamp DESC LIMIT 1"; else $sql = "SELECT * FROM php_blog WHERE id = '$_REQUEST[id]'"; There are two other places you will need to make some edits; can you find them? I also notice I'm getting the following errors: CODE Warning: main() [function.main]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/fauxxor/public_html/photo/index.php on line 1 Warning: main(http://fauxx.org/x/header.inc) [function.main]: failed to open stream: Permission denied in /home/fauxxor/public_html/photo/index.php on line 1 -------------------- |
| Amelie |
Nov 13 2006, 08:59 AM
Post
#11
|
![]() (Not the film) Group: CodeGrrl Staff Posts: 5,296 Joined: 14-January 05 From: UK Member No.: 2,051 |
I think I see where you're going wrong - you need to change $_GET[id] to $row[id].
-------------------- -- Amelie
» N-N.net | RNM.com | NS.net » Scripts | PHPAskIt fan? "This is my signature. There are many others like it but only this one is mine." |
| scrambled23 |
Nov 13 2006, 10:46 PM
Post
#12
|
|
Rank #1: Newbie ![]() Group: Members Posts: 16 Joined: 11-November 06 Member No.: 5,455 |
I know, my header/footer includes are somewhat messed up, but I can fix that later.
I'm guessing I'll have to replace CODE $sql_prev = "SELECT * FROM php_blog WHERE id < '$_GET[id]' ORDER BY id DESC LIMIT 1"; With some variation of that code as well, I just don't know what. Sorry, I'm a total n00b to coding anything more complex than a countdown. |
| Alis Dee |
Nov 13 2006, 11:39 PM
Post
#13
|
![]() Rank #4: Advanced ![]() ![]() ![]() ![]() Group: Members Posts: 127 Joined: 22-November 05 Member No.: 3,379 |
Yup, that's the place; the SQL for the previous/next links.
First step is to ask yourself what it is currently doing. In this case, you're fetching everything (SELECT *) from php_blog where the id field is less than $_GET['id'], which is the number passed in your query string. You only want to get the very next smallest number (LIMIT 1), as ordered numerically by id. Next you have to ask what am I actually trying to achieve? In this case, you want to fetch the previous log post. How is that represented in the database? Well, the previous post would be the row in the database that had the next smallest id after the current post/row's. Just getting the next smallest number (ie. $id--) isn't good enough because what if you've missed a post ID (ie. deleted a post)? You have no way of knowing 'beforehand' what the next smallest valid ID is, so you need to find a way to get it out of the database. If you're currently viewing a post other than the most recent, it's okay to just use $_GET['id'] (or $_REQUEST['id']; one of the links in my sig goes to an explanation of metaglobal arrays) because you actually have a $_GET['id'] set. But what if you are just viewing from the index page? $_GET['id'] would not be set, in that case, and your query will error. Is there a better place to get this information? As usual with programming we're skinning cats here and there is more than one way to do it, but in this case there's an easy solution. Because this is a photolog, you are only ever going to be displaying one post at a time. You get the information for this post out of the first SQL query, and it gets put into the $row array. $row['id'] is going to be set wheter $_GET['id'] is or not (that's the previous thing we fixed), so it makes a good candidate in this instance to be used to construct our previous/next links. (I should also note at this point that because $row is only ever going to need to hold one value, it doesn't need to be put inside a while() loop. The same goes for the previous/next SQL while()s; beware of unnecessary useage of while() loops...) The second thing you need to ask yourself at this point is what fields exactly do you need to pull out of this SQL query. It's generally bad practice to pull out more information from an SQL query that you absolutley need. In this case, we're only actually using the id field, so we really should be putting that instead of *. So, all up what we've got looks like: CODE <?php // establish the db connection mysql_connect ('localhost', 'OMITTED', 'OMITTED'); mysql_select_db ('OMITTED'); // query to pull the post info out // this is either going to be specified by the query string, or else the most recent post if( !empty( $_GET['id'] ) ) $sql = "SELECT * FROM `php_blog` ORDER BY timestamp DESC LIMIT 1"; else $sql = "SELECT * FROM `php_blog` WHERE id = '$_GET[id]'" $result = mysql_query( $sql ) or print( "Can't select entries from table php_blog.<br />" . $sql . "<br />" . mysql_error() ); // only ever going to be returning one row, so we don't need a while() loop $row = mysql_fetch_array( $result ); // format the data $date = date( "l F d Y", $row['timestamp'] ); $title = stripslashes( $row['title'] ); $entry = stripslashes( $row['entry'] ); // now print the entry ?> <p><strong><?=$title?></strong><br /><br /> <?=$entry?><br /><br /> Posted on <?=$date?> <hr /></p> <?php // write the 'previous' link $sql_prev = "SELECT id FROM `php_blog` WHERE id < '$row[id]' ORDER BY id DESC LIMIT 1"; $result_prev = mysql_query( $sql_prev ) or print( "Can't select previous entry id table php_blog.<br />" . $sql_prev . "<br />" . mysql_error() ); $prev = mysql_fetch_array( $result_prev ); // if 'id' is set, print a link // else just print the word previous if( isset( $prev['id'] ) ) print "<a href=\"index.php?id=". $prev['id'] ."\">Previous</a> -- "; else print "Previous -- "; // now do the same with the 'next' link $sql_next = "SELECT id FROM php_blog WHERE id > '$row[id]' ORDER BY id LIMIT 1"; $result_next = mysql_query( $sql_next ) or print( "Can't select next entry id table php_blog.<br />" . $sql_next . "<br />" . mysql_error() ); $next = mysql_fetch_array( $result_next ); if( isset( $next['id'] ) ) print "<a href=\"index.php?id=". $next['id'] ."\">Next</a>"; else print "Next"; ?> There's one further thing I can see in here that might possibly be a problem; see if you can pick it up. -------------------- |
| scrambled23 |
Nov 14 2006, 12:53 AM
Post
#14
|
|
Rank #1: Newbie ![]() Group: Members Posts: 16 Joined: 11-November 06 Member No.: 5,455 |
It says there is an error on line 13, so I'm guessing it has something to do with this:
CODE $result = mysql_query( $sql ) or print( "Can't select entries from table php_blog.<br />" . $sql . "<br />" . mysql_error() ); |
| Alis Dee |
Nov 14 2006, 02:00 AM
Post
#15
|
![]() Rank #4: Advanced ![]() ![]() ![]() ![]() Group: Members Posts: 127 Joined: 22-November 05 Member No.: 3,379 |
QUOTE Parse error: syntax error, unexpected T_VARIABLE in /home/site/public_html/photo/index.php on line 13 That error usually meants that there's a missing semicolon or unclosed quotation marks. Generally it's not on the line the error says; it's on the line with the statement immediatley preceeding. A statement is any line in PHP that would normally end in a semicolon. Contrast to control structures -- if(), while() and so on -- which are usually delimited by curly braces. -------------------- |
| scrambled23 |
Nov 14 2006, 03:58 AM
Post
#16
|
|
Rank #1: Newbie ![]() Group: Members Posts: 16 Joined: 11-November 06 Member No.: 5,455 |
Ok, I forgot the semicolon...
It still doesn't seem to work, though. Sorry, I'm just really lost. |
| Alis Dee |
Nov 14 2006, 04:40 AM
Post
#17
|
![]() Rank #4: Advanced ![]() ![]() ![]() ![]() Group: Members Posts: 127 Joined: 22-November 05 Member No.: 3,379 |
Okay, first up instead of just posting "it doesn't work", post the exact error message you are receiving. If you're not receiving an error message, describe the problem.
Work out in your head what you think might be going wrong after giving the description. What data is missing? Where might it have gone missing? Before asking for help, see if you can track down what is going wrong. If you seem to have 'lost' variables, put in some print() statements at various points in the code to see if you can 'find' them again. Are you getting data out of the MySQL query? If so, are you performing any manipulations on it that may be changing its value? Are these going wrong? These are absolutley essential skills you will need to learn if you want to code. Yes, it's painful and frustrating; tough, everyone goes through it, and the learning curve is steep. If you do not develop these skills you might as well give up now and go back to using a pre-fab script because let me tell you, what you're doing now -- hacking basic data out of a database and displaying it on a page -- is the easy part. If you can't get through this bit, you'll never develop the skill to maintain, improve and secure your script. -------------------- |
| scrambled23 |
Nov 18 2006, 05:27 AM
Post
#18
|
|
Rank #1: Newbie ![]() Group: Members Posts: 16 Joined: 11-November 06 Member No.: 5,455 |
Ok, sorry for not replying sooner... I didn't give up yet. Just had to catch up on some school work.
The problem is index.php?id=# (request?) doesn't lead to the proper queries... it all goes to the same post. I played with the select, _GET, request and row things but the next/previous post links seem to become static and the requested URL still doesn't work. |
| Alis Dee |
Nov 19 2006, 09:15 PM
Post
#19
|
![]() Rank #4: Advanced ![]() ![]() ![]() ![]() Group: Members Posts: 127 Joined: 22-November 05 Member No.: 3,379 |
Did you put in some print() statements?
Put one on $_GET['id'] right at the top of the script: Is the value there? Use a print_r( $result ) after the SQL query: Are the correct values there? I see what's going on; it's a dumb logic error on my part. See if you can find where it is. -------------------- |
| scrambled23 |
Nov 19 2006, 09:26 PM
Post
#20
|
|
Rank #1: Newbie ![]() Group: Members Posts: 16 Joined: 11-November 06 Member No.: 5,455 |
Like this, right?
CODE if( !empty( $_GET['id']; print_r( $_REQUEST );) ) It says there is a parse error... syntax error, unexpected ';', expecting ')' |
![]() ![]() |
|
Lo-Fi Version | Time is now: 29th July 2010 - 11:21 AM |

