Forum Moderators: coopster

Message Too Old, No Replies

website.com/subdomain.php?id=

         

jason_m

9:16 pm on Feb 26, 2010 (gmt 0)

10+ Year Member



thank you in advance for any pointers.

i am a new php coder who comes from a background in non web oriented languages.

i am trying to do the following:
allow my visitor to cycle through blog posts by way of an id tag.

i thought i had the problem solved. i created a variable which remembered your number of page views, and then cycled through posts by way of that.

but, i forgot one thing: what if they want to go backwards?

i have a database set up, i have the display set up, but i want to limit it such that only 3 posts are displayed at a time, and allow the user to click "next" or "previous".

all the groundwork in html and php for all db connections as well as formatting. i just need to crack this one last issue.

thank you!

jason_m

9:20 pm on Feb 26, 2010 (gmt 0)

10+ Year Member



one last thing:
i know how to limit the number of entries viewed. the only thing i need help with is moving forward and backward as my current method is flawed.

Readie

9:47 pm on Feb 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World Jason_m

For your problem, in the database, have an id column (auto incrementing, unique)

Then, each blog post is:

www.example.com/blog.php?id=8

(or whatever number)

Then:

<?php

$id = $_GET['id'];

if(isset($id) && $id != "") {
$sql = 'SELECT * FROM blog_posts WHERE id="' . $id . '"';
$result = mysql_query($sql);

if(mysql_fetch_array($result)) {
$subject = mysql_result($result, 0, "subject");
$body = mysql_result($result, 0, "content");
// Any more columns you want

$sql = 'SELECT id FROM blogposts WHERE id>"' . $id . '" ORDER BY id DESC';
$result = mysql_query($sql);
if(mysql_fetch_array($result)) {
$l_id = mysql_result($result, 0, "id");
$prev = '<a href="/blog.php?id=' . $l_id . '">Previous</a>';
} else {
$prev = '&nbsp;';
}

$sql = 'SELECT id FROM blogposts WHERE id<"' . $id . '" ORDER BY id ASC';
$result = mysql_query($sql);
if(mysql_fetch_array($result)) {
$l_id = mysql_result($result, 0, "id");
$next = '<a href="/blog.php?id=' . $l_id . '">Next</a>';
} else {
$next = '&nbsp;';
}

if($prev == "&nbsp;" || $next == "&nbsp;") {
$break = '';
} else {
$break = ' | ';
}

echo '<p>' . $subject . '</p>
<p>' . $body . '</p>
<p>' . $prev . $break . $next . '</p>';

} else {
header('HTTP/1.1 404 Not Found');
// Echo some error message about invalid ID
}

} else {
// Page for no ID declared
}

?>

Code hasn't been error checked etc, might be a typo or some other error. Should be enough to give you the basic principle of the idea though

Readie

5:11 am on Feb 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops, I miss-read your post there

Taking what I said, most of it can be used as a framework, simply adding in the following

for($i = 0; $i < 3; $i++) {
$subject[$i] = mysql_result($result, $i, "subject");
// Other stuff
}

....

echo $subject[0];
// Rest of post, divider of some sort
echo $subject[1];
// etc...

Or you could just echo each post in the for loop.

EDIT:

Also need to change this:
$sql = 'SELECT * FROM blog_posts WHERE id="' . $id . '"';


To this:
$sql = 'SELECT * FROM blog_posts WHERE id>="' . $id . '" AND WHERE id<="' . ($id + 2) . '"';


EDIT 2:

And this sql statement:
$sql = 'SELECT id FROM blogposts WHERE id<"' . $id . '" ORDER BY id ASC';


To this:
$sql = 'SELECT id FROM blogposts WHERE id<"' . ($id + 2) . '" ORDER BY id ASC';

jason_m

9:55 pm on Feb 27, 2010 (gmt 0)

10+ Year Member



Beautiul. I will give this a shot when I get back to my machine

Readie

12:27 am on Feb 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looking over the code, for the next/prev links I think I have the less than and greater than symbols the wrong way around. The SORT BY id ASC/DESC seems fine though.

jason_m

5:19 pm on Mar 2, 2010 (gmt 0)

10+ Year Member



i am getting the following error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/henomel6/public_html/blog/index.php on line 39




<?php
$id = $_GET['id'];

$host="localhost"; // Host name
$username="myuser"; // Mysql username
$password="mypw"; // Mysql password
$db_name="mydb"; // Database name
$tbl_name="blog"; // Table name
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

if ($id==1){
$sql = 'SELECT * FROM $tbl_name WHERE id>=1 AND id<=3;';
}else{
$sql = 'SELECT * FROM $tbl_name WHERE id>="' . (($id * 3)-2) . '" AND id<="' . ($id * 3) . '";"';
}

$result=mysql_query($sql);
echo $result;
echo '<br>';
echo $id;
echo '<br>';
echo $sql;
while ($row = mysql_fetch_array($result)) /*error line*/
{
$view_db .='<tr><th colspan="2" align="left"><b>';
$view_db .= $row['title'];
}

$view_db .='<a href="/blog.php?id=".($id-1)."/>&#9668;Previous</a></th><th colspan="2" align="right">';
if (id > 1){
$View_db .='</blog.php?id=".($id+1)."/>Next&#9658;</a></th></tr>';
}
$view_db .='</table>';
echo ($view_db);

?>

jason_m

5:22 pm on Mar 2, 2010 (gmt 0)

10+ Year Member



note: output of while loop truncated for anonymity etc.

jason_m

6:07 pm on Mar 2, 2010 (gmt 0)

10+ Year Member



hello,
it was a silly SQL error.

this is not in critique of Readie, for those who come across this post in the future.

Correct SQL should be coded as:
if ($id==1){
$sql = 'SELECT * FROM ' .$tbl_name. ' WHERE id>=1 AND id<=3 order by id desc;';
}else{
$sql = 'SELECT * FROM ' .$tbl_name. ' WHERE id>=' .(($id * 3)-2). ' AND id<=' .($id * 3). ' order by id desc;';
}

the quotes seemed ot be producing the issue. order is overkill.

jason_m

6:13 pm on Mar 2, 2010 (gmt 0)

10+ Year Member



edit: the second sql works for the case of id=1 as well.
derp.

$sql = 'SELECT * FROM ' .$tbl_name. ' WHERE id>=' .(($id * 3)-2). ' AND id<=' .($id * 3). ' order by id desc;';

Readie

7:29 pm on Mar 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Was typed on the fly so there's bound to be some errors :)

$sql = 'SELECT * FROM ' .$tbl_name. ' WHERE id>=' .(($id * 3)-2). ' AND id<=' .($id * 3). ' order by id desc;';


The problem you have with this is if the id is set as, say 3

Then you'd be selecting all posts between 7 and 9, when what the user is trying to select is all posts between 3 and 5

Furthermore, if the id is 1, then you're selecting all posts between 1 and 1