i tried the following PHP code to create permalinks to my blog posts. The result is a blank page which obviously means there are bugs in the code. Please help me debugging it or a whole new coding to create permalinks. I posted the same post in MYSQL section but received no reply. :-( $result = mysql_query("SELECT post_title, post_body From posts ORDER BY post_no DESC $limit"); while($row = mysql_fetch_array($result)) { $title = $row[0]; $content = $row[1]; $perma_link = mysql_query("SELECT post_title, post_body FROM posts WHERE post_title = $row[0]"); echo("<h3>"'<a href="{$_SERVER['PHP_SELF']}?$perma_link">$title</a> ."</h3><hr style='color: brown'><br/> .$content" ."<hr style='width: 80%; align: center; color: brown'><br/><br/><br/>"); }; $result = mysql_query($query, $con_mysql_db) or trigger_error("SQL", E_USER_ERROR); PHP:
Hey, Without solving the whole thing for you :-P there's a couple of things you'll want to change.. For starters, $perma_link = mysql_query -- this is just holding the MySQL resource id and is not being turned into usable data.. Secondly the permalink is just created from the title which can cause headaches later down the track if you ever have two of the same titles. My suggestion is to retrieve the column id as well (providing you have an id column) $result = mysql_query("SELECT post_title, post_body, id From posts ORDER BY post_no DESC $limit"); while($row = mysql_fetch_array($result)) { $title = $row[0]; $content = $row[1]; $postid = $row[2]; $permalink = sprintf('%s/postid%d-$s', $_SERVER['PHP_SELF'], $postid, $title); echo("<h3>"'<a href="$perma_link">$title</a> ."</h3><hr style='color: brown'><br/> .$content" ."<hr style='width: 80%; align: center; color: brown'><br/><br/><br/>"); }; Code (markup): Then you would need to setup htaccess to somehow handle this new URL RewriteEngine On RewriteRule postid([0-9]*)-.+ showpost.php?postid=$1 Code (markup): This isn't necessarily how you would do it exactly but it should give you a step in the right direction
Thanks for posting, but the code doesn't seem to work. You sure that the PHP code is correct? Secondly the htaccess file isn't being edited. When I insert the above code in the file and try to save it. The message appears that the file could not be saved. What could be the possible reason(s)?
Is it not supposed to be mysql_fetch_row PHP: when your using $row[0]; $row[1]; // etc... PHP: Can you show us what it does echo out as a link ?
_fetch_array also works fine. Actually the echo does not show any link but display a whole blank page. So any suggestions? Actually the htaccess file is also not saving. So really some help needed here...
I was presuming this was your custom written software and was just trying to lead you in the right direction rather than give you the full answer which I couldn't anyway as I don't know your database structure or anything about your website. Since I'm guessing you might be using wordpress, there are built in functions for you to use. http://codex.wordpress.org/Function_Reference/get_permalink
the echo looks a bit messy, could be a source of some problems. Cleaned to working hopefully: echo "<h3><a href=\"$_SERVER[PHP_SELF]?$perma_link\">$title</a></h3><hr style=\"color: brown\"><br/>$content<hr style=\"width: 80%; align: center; color: brown\"><br/><br/><br/>"; PHP:
No I am not using wordpress. Actually I am not using any blogging software. It may sound silly, but I wanted to learn PHP and the best way to do is create something through php. I think you understand what I am trying to say. Any way thanks for helping.
You know what!!!? It actually worked. However the link is not referring to the respective individual post but to the main page and the URL changes from saadi.000a.biz to saadi.000a.biz/index.php? So may be I am missing something.. Any idea???
looks like perm link isn't grabbing anything, needs a mysql_fetch_array in there in there. Atm you are just re querying retrieving the same as in result (post_title and post_body). I'm guessing you have a field in posts with perm_link? If not let me know what you want in your link $result = mysql_query("SELECT post_title, post_body,perm_link From posts ORDER BY post_no DESC $limit") or die(mysql_error()); while($row = mysql_fetch_array($result)) { $title = $row["post_title"]; $content = $row["post_body"]; $perma_link = $row["perm_link"]; echo "<h3><a href=\"$_SERVER[PHP_SELF]?$perma_link\">$title</a></h3><hr style=\"color: brown\"><br/>$content<hr style=\"width: 80%; align: center; color: brown\"><br/><br/><br/>"; PHP:
No there isn' any field named as perma link. What I actually want is that the link is generated behind every post_title where upon clicking, the link leads to its respective post. Just like any blog. Where the post title is also a link to its respective post.
aahh ok righty that's list: $result = mysql_query("SELECT post_title, post_body From posts ORDER BY post_no DESC $limit") or die(mysql_error()); while($row = mysql_fetch_array($result)) { $title = $row["post_title"]; $content = $row["post_body"]; echo "<h3><a href=\"$_SERVER[PHP_SELF]?title=$title\">$title</a></h3><hr style=\"color: brown\"><br/>$content<hr style=\"width: 80%; align: center; color: brown\"><br/><br/><br/>"; PHP: for showing the post $title = mysql_escape_string($_GET['title']); $postquery = mysql_query("SELECT post_title, post_body From posts WHERE post_title ='$title' "); if (mysql_num_rows($postquery)>0) { $post = mysql_fetch_array($postquery); echo "$post[post_title] - $post[post_body]"; } else { echo "not found"; } PHP:
Yeah this code worked. But there's still a problem. The URL changes according to the title but the clicked post shows 6 or 7 times along with the other posts. In fact the clicked post reappears after each other post present on the page and the clicked post doesn't shows up exclusively. I will try to figure it out. In fact using the _GET[variable] on the other page would be much appropriate where every click on the post title will be redirected to this page which will show the clicked post exclusively.
just need a bit of code to tell if it's the list or if it's the post. if (isset($_GET['title']) { //code for showing the post } else { //code for list } PHP:
It's post. Actually I will tell you the structure of my database. I think that will be helpful. post_id post_title post_content Date Post_id is a unique identifier. post_title is of course title of each post. Post_content is the whole article and date is the date on which this article was submitted to the database. I think this info will be helpful.