[RESOLVED] Link title to get table data and display by ID.
Code:
<?php
mysql_connect ('localhost', 'root', 'password') ;
mysql_select_db ('manchesternews');
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
die("Invalid ID specified.");
}
$id = (int)$_GET['id'];
$sql = "SELECT * FROM newsreel WHERE id='$id' LIMIT 1";
$result = mysql_query($sql) or print ("Error has incurred. Please look at your code.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
$articletitle = ($row['articletitle']);
$article = ($row['article']);
?>
<p><strong><?php echo $articletitle; ?></strong><br /><br />
<?php echo $article; ?><br /><br />
<hr /></p>
<?php
}
Basically the above code works but only if the user then puts "http://example.com/feed.php?id=3"
They have to type ?id=3 in.
Is it possible on the feed.php to have the $articletitle displayed and when that is clicked it gets the "id=3" which loads the "fee.php?id=3" Thanks.
$mystring="";
$pre = "id=";
$id = (int)$_GET['id'];
CONCAT "{$pre}{$id}" = $mystring
etc;.....
Thanks for the reply.
Code:
<?php
mysql_connect ('localhost', 'root', 'password') ;
mysql_select_db ('manchesternews');
$sql = "SELECT * FROM newsreel ORDER BY id DESC LIMIT 5";
$result = mysql_query($sql) or print ("Something went wrong.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
$article = ($row['article']);
$title = ($row['title']);
$id = ($row['id']);
$article = "$article... <br /><br /><a href=\"newsroom.php?id=" . $id . "\">read more</a>";
}
?>
<p><strong><?php echo $title; ?></strong><br /><br />
<?php echo $article; ?><br /><br />
<hr /></p>
The following code actually does the job for me. But where I tried limiting the db entries to 5 per page for some reason only one of them shows up on the webpage.
Try changing your while loop to and see if it helps
PHP Code:
while($row = mysql_fetch_array($result)) { ?> <p><strong><?php echo $row [ 'title' ]; ?> </strong><p> <p><?php echo $row [ 'article' ]; ?> <br> <a href="newsroom.php?id=" . $row['id'] . ">read more</a></p> <hr><?php } ?>
If not then echo out your $sql and then copy and paste into phpmyadmin and see what is returned there (i.e. check your query is working as expected)
Thanks for the help pixie. Your method seemed like it would work but when clicking read more it never inserted the ID so e.g. newsroom.php?id= would never be giving an ID meaning nothing would change. I echo'd the query in phpMyAdmin and it ran like I wanted to load up the last 5 news items. I want it so if they click read more it only loads the information associated with that id in the table. Thanks for the help.
Is the name ['id'] the correct column name from your table (i.e. is your id in the table actually called id)? Also I forgot some quote marks in the code I posted before
PHP Code:
< a href = "newsroom.php?id='" . $row [ 'id' ]. "'" > read more </ a ></ p >
Hello pixie. Im sure the name of the row is id.
Code:
<a href="tech.php?id='".$row['id']."'">read more</a></p>
When clicking read more I know just get
Pixie thanks for the help. Until now I've only just noticed i've been pasting your code in the wrong file.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks