Forum Moderators: coopster

Message Too Old, No Replies

Query Strings - Page ID

         

Mark_129

3:48 am on Feb 11, 2010 (gmt 0)

10+ Year Member


I've been trying for about an hour or so to allow a query string for different articles on a website I've been working on. It has a News section and basically I want the URL to be something like http:/[smilestopper]/www.example.com/dir1/dir2/article.php?id=42. I don't want any content other than that in the specific article to appear, and is it possible to create a unique page for article.php (without the query string) as well?

Links to these pages would be accessible from an index page and archive. I'm not entirely sure how to go about it though, so I'd appreciate a lot if anybody could help me.

Would article.php be ever-growing, since the news is constantly updated with new articles? I'm assuming each ID won't be a separate file since the extension is already there with article.php

Is it possible to add simple HTML tags in the article?

What information would I need to include in the URL which links to that page? For example, a link from http:/[smilestopper]/www.example.com/ straight over to http:/[smilestopper]/www.example.com/dir1/dir2/article.php?id=42. Would a PHP script need to go in the link, or could it just be typed as I just did?

I've tried something like:

<?php

$id = $_GET['42'];
echo $Content here, but _GET is recommended as short as possible - below 100 characters - or is that just the variable above?;

?>

I'm on a Go Daddy Windows hosting plan, which supports PHP 5.x, so not sure if that would be a disadvantage and whether I'd still be able to use page IDs.

Thanks a lot in advance.

Readie

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

WebmasterWorld Senior Member 10+ Year Member



Hey Mark_129, welcome to Webmaster World :)

Happily I have a working example of what you're after on one of my sites. So I'll just copy and paste it over with a few changes:

<?php

$n_id = mysql_real_escape_string($_GET['id']);

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

if($result = mysql_fetch_array($news)) {
/* THIS IS THE ACTUAL NEWS STORY
GET THE CONTENT BY DOING:
$subject = mysql_result($news,0,"subject");
$content = mysql_result($news,0,"content");
ET CETERA... */
} else {
/* THIS IS YOUR ERROR PAGE FOR INCORRECT ID'S */
header('HTTP/1.1 404 Not Found');
echo 'Error: There is no news post with that ID.';
}
echo '<br /><a href="article.php">Back to Archive</a>';
} else {
/* THIS IS THE PAGE FOR NO ID DECLARED - THE CODE BELOW WILL GENERATE A LIST OF LINKS FOR YOUR NEWS POSTS */
$sql = 'SELECT * FROM news ORDER BY date DESC';
$news = mysql_query($sql);
$news_rows = mysql_num_rows($news);
for($i = 0; $i < $news_rows; $i++) {
$id = mysql_result($news,$i,"id");
$date = mysql_result($news,$i,"date");
$subject = mysql_result($news,$i,"subject");
echo '<a href="article.php?id=' . $id . '">' . $date . ' - ' . $subject . '</a><br />';
}
}
}

?>


So, this has your archive taken care of too :) simples.

Mark_129

9:51 am on Feb 11, 2010 (gmt 0)

10+ Year Member



Thanks a lot for your help. I've implemented that code but now getting the following errors in the section I placed it:

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Can't connect to MySQL server on 'localhost' (10061) in D:\MY HOSTING PATH\article.php on line 30

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in D:\MY HOSTING PATH\article.php on line 30

Warning: mysql_query() [function.mysql-query]: Can't connect to MySQL server on 'localhost' (10061) in D:\MY HOSTING PATH\article.php on line 51

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in D:\MY HOSTING PATH\article.php on line 51

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in D:\MY HOSTING PATH\article.php on line 52


Do I need to create a MySQL database to do this or am I missing something else here? If so how would I go about connecting it to the page? Also, please could you highlight parts I need to change (if any)?

Many thanks.

Readie

4:30 pm on Feb 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, you do need a MySQL database; in this case you need a table named "news", with columns: "id" "date" "subject" and "content".

You'd also need the following lines of code (change them to match your database name/login details):

mysql_connect('localhost','USERNAME','PASSWORD') or die("Could not connect to server");
mysql_select_db('DATABASE');

Mark_129

10:19 pm on Feb 11, 2010 (gmt 0)

10+ Year Member



Thanks again. Just one more question now me-think and me-hopes. Think I'm very close to get this working.

I've managed to make the table and create inserts, which automatically created links with IDs on the article.php page, according to what I entered. They show the date and subject, however when I click on them the content isn't on the requested page. The 'Back to archive' link is there but that's about it apart from my own PHP require() codes, CSS, etc.

I've tried changing:


$subject = mysql_result($news,0,"subject");
$content = mysql_result($news,0,"content");


and adding more, however whenever I do I receive errors such as 'Warning: mysql_result() [function.mysql-result]: Unable to jump to row 1 on MySQL result index 6', so I'm assuming they shouldn't be modified.

Anything I can do to make the content appear on the ID pages? Is there something in the MySQL database I need to look at?

Thanks a lot for your help.

Readie

10:47 pm on Feb 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$subject = mysql_result($news,0,"subject");
$content = mysql_result($news,0,"content");

Assuming you removed the comment tags ( /* */ ) and my comment from around these, then what you are doing is declaring $subject and $content as the data inside the cell, where the header is equal to "content" or "subject", and the row's ID (under column "id") is equal to $n_id.

so you'd have something like this:

if($result = mysql_fetch_array($news)) {
$date = mysql_result($news,0,"date");
$subject = mysql_result($news,0,"subject");
$content = mysql_result($news,0,"content");
echo '<p><b>[' . $date . ']: ' . $subject . '</b></p><p>' . $content . '</p>';
}


Although I didn't think about this properly earlier, you shouldn't really name a MySQL column "date" as it's a data type - name it something like "n_date" and change the mysql requests to compensate

Mark_129

11:48 am on Feb 16, 2010 (gmt 0)

10+ Year Member


Thanks a lot. :)

I've been using this script for a few of my pages the past few days and all is working well, but I'm now wondering about something else.

My news archive is in a separate file (archive.php), and I'm able to filter down categories in the URL using the current script. The URL would be similar to this:

http://news.example.com/archive.php?f=0

(f=1, f=2, f=3, etc.)

My archive shows as a table to the public.

Is there a way I can query from multiple elements, so I can set page numbers for the archive where older records are pushed down onto the next page? So the URL would look like this:

http://news.example.com/archive.php?f=1&p=1

(&p=2, &p=3, etc.)

Or perhaps a code where I'd have to change each page manually and push them down myself by adding a new table row.

Thanks again for your help, it's much appreciated. :D

Readie

5:06 pm on Feb 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$sql = 'SELECT * FROM news ORDER BY date DESC'; 
$news = mysql_query($sql);
$news_rows = mysql_num_rows($news);
for($i = 0; $i < $news_rows; $i++) {
$id = mysql_result($news,$i,"id");
$date = mysql_result($news,$i,"date");
$subject = mysql_result($news,$i,"subject");
echo '<a href="article.php?id=' . $id . '">' . $date . ' - ' . $subject . '</a><br />';
}


that part of the code I already wrote should take care of that for you - just modify the items accordingly