abyssal Posted June 12, 2011 Share Posted June 12, 2011 Hello. As I've said, I'm a newbie trying to solve some issues with a script. I want to design my own script but I've come along with this issue of linking. I will give you an example of what I want. E.g. here when I want to see a topic, I go at viewtopic.php?id=31328741. This is the exact thing I'm trying to do on my script, to link like this. For example, my database has Post ID, Post Title and Post content. when I go to view.php?=1 to show me the title and the content from there. I would be very satisfied if you could guide me through the creation of the certain view.php page. Thanks ! Quote Link to comment Share on other sites More sharing options...
cssfreakie Posted June 12, 2011 Share Posted June 12, 2011 well that should be doable. Can you show us the script you say you are struggling with? If you don't have one. I recommend you first have a look at the database handling tutorial and move from there on. Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted June 13, 2011 Share Posted June 13, 2011 Here is a simple little demo I made using a single page to display posts as you described. http://get.blogdns.com/dynaindex/post-variable.php Another way is to make a separate php file made exclusively to display the single posts. As an example, lets call this view.php <?php //At the top would use a $_GET parameter $id = $_GET['id']; //make mysql connection, use your connection info $con = mysql_connect("localhost","username","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } //select your database mysql_select_db("my_db", $con); //query what you want, insert your table name $result = mysql_query("SELECT * FROM table WHERE id='$id'"); //post loop while($row = mysql_fetch_array($result)) { //use your values echo $row['id'] ."<br />"; echo $row['title'] ."<br />"; echo $row['description'] ."<br />"; } //close the connection mysql_close($con); ?> From the page you want to link to your single post, make a hyperlink leading to view.php, get the id from each post echo "<a href='view.php?id=$id'>View Post</a>"; The above is in it's simplest form, you can use other unique values versus id's. Quote Link to comment Share on other sites More sharing options...
abyssal Posted June 13, 2011 Author Share Posted June 13, 2011 Ok thanks. But you get the ID via $_GET function. What if I want to get the id from the database? Let's say I have the username stored in a cookie and then I get the user's last post from a database. How to link to it then ? Quote Link to comment Share on other sites More sharing options...
monkeytooth Posted June 13, 2011 Share Posted June 13, 2011 $_GET is retrieving the value of the id= from the URL view.php?id=xxx xxx being the value of id. You get the data from the database by comparing that $_GET value (or $_POST, or $_COOKIE, or $_SESSION, or even $_REQUEST.. all depending on what your doing and how.) to that of whats stored in your DB like QuickOldCar's example shows. Now on to QuickOldCar's example might I suggest doing a little light reading on mysql_real_escape_string() as well so you can attempt to prevent SQL injection attacks. As for the question of the users last post, well thats all in the query.. you query for the user, then you query I believe in Asending order to get that users last post first, then you display that post.. Quote Link to comment Share on other sites More sharing options...
abyssal Posted June 13, 2011 Author Share Posted June 13, 2011 OK I did it afterall. I was doing all backwards, got the point now. Thanks. BTW, is there any function or command that can store the link from the page where it's executed into a variable? Quote Link to comment Share on other sites More sharing options...
monkeytooth Posted June 13, 2011 Share Posted June 13, 2011 $myNewVariable = $_GET['id']; echo $myNewVariable; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.