Page 1 of 1

linking to data in MySQL DB

Posted: Wed Mar 03, 2004 12:24 am
by seeker2921
I'm trying to make an archive page for my simple blog I wrote, What I would like is to have the archive page consist of two pages..

Page one:
should just display the links and I have this part figuared out..

Code: Select all

<ul>
<?
//Get Settings
include("config.inc.php");

 
 /* Connecting to MySQL*/
    $connection = mysql_connect($db_host,$db_user,$db_pass) 
        or die("Could not connect to mysql. Fix me!?!");
      
        /* Select Database. */
     $db = mysql_select_db($database_name,$connection) or die("ERROR: I couldn't select the database requested!");

 //Time choices 
if ($UStime==us) {
//date format 
    $sql = "SELECT DATE_FORMAT(date, '%M %e, %Y @ %h:%i %p') AS readable_date,topic,entry,ID FROM $tablename order by date desc";
   $sql_result = mysql_query($sql, $connection) or die("Query failed. Please contact the site administrator, and bitch at him."); 

/*setting up array and defining result values */
     while ($row = mysql_fetch_array($sql_result)) {  

//defining variables, readable_date is temp field  
$date = $row["readable_date"]; //date
$topic = $row["topic"]; //Title of blog entry
$id = $row["ID"]; //ID of blog entry

//Display links
echo ="<li><a href='archive2.php'>$date - $topic</a></li>";
}

            
    /* Free resultset */
    mysql_free_result($sql_result);
    /* Closing connection */
    mysql_close($connection);
}
?>
Where I run into my problem is with page two

Page two:
Should just display the single blog entry based on the link that was clicked I know how to pass data from one page to another but I can't figuare out how to make the page two just show the blog entry that was clicked.. Any ideas?

I hope that this post is clear I'm not good at explaining myself.. Sorry, Also thanks for any help you provide me with..

Posted: Wed Mar 03, 2004 4:07 am
by twigletmac
The first thing you need to do is pass the ID value of the blog entry to be displayed in the link to that entry, so instead of:

Code: Select all

echo ="<li><a href='archive2.php'>$date - $topic</a></li>";
you would have

Code: Select all

echo '<li><a href="archive2.php?id='.$id.'">'.$date.' - '.$topic.'</a></li>';
Then on the archive2.php page you would get the ID from the URL:

Code: Select all

$article_id = (!empty($_GET['id']) && is_numeric($_GET['id'])) ? $_GET['id'] : 0;
Then you can write an SQL statement to find the article's data in the database, so you'd have something along the lines of:

Code: Select all

SELECT field1, field2, field3, field4, ... , fieldn FROM your_table WHERE ID=$article_id
Once you've got the data all you've got to do is display it.

HTH,
Mac