Jump to content

Linking in PHP


abyssal

Recommended Posts

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 !

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

$_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..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.