Jump to content

Recommended Posts

I have successfully and hardly made my url from
[b]articles2.php?title=Low%20calorie%20diet%20plan&id=1[/b]
to
[b]/articles2/1-low-calorie-diet-plan.htm[/b]

After many days figuring out how to do the mod_rewrite, now i can not figure out how to fix my links(script) to point to the newly created mod_rewrite url.

My links still points to : [b]articles2.php?title=low calorie diet plan&id=1[/b]

Question: How can i dinamicaly point my articles database links to the new friendly url made with mod_rewrite?

My code:
[code]<?php
include '../database.php';


// if no id is specified, list the available articles
if(!isset($_GET['title']))
{
  $self = $_SERVER['PHP_SELF'];

  $query = "SELECT title, id FROM news ORDER BY id";
  $result = mysql_query($query) or die('Error : ' . mysql_error());

  // create the article list

  $newstext = '<ol>';
  while($row = mysql_fetch_array($result, MYSQL_NUM))
  {
      list($title, $id) = $row;

      $newstext .= "<li><a href=\"$self?title=$title&id=$id\">$title</a></li>\r\n";
  }

  $newstext .= '</ol>';

  $title = 'Diet Plan Articles';
} else {
  // get the article info from database
  $query = "SELECT title, newstext FROM news WHERE id=".$_GET['id'];
  $result = mysql_query($query) or die('Error : ' . mysql_error());
  $row = mysql_fetch_array($result, MYSQL_ASSOC);

  $title = $row['title'];
  $newstext = $row['newstext'];
}

mysql_close();[/code]

Thank you in advance for any insight on this solution that i am after!  :)
Link to comment
https://forums.phpfreaks.com/topic/33146-php-side-from-mod_rewrite/
Share on other sites

After this:
[code=php:0]list($title, $id) = $row;[/code]


Add this:
[code=php:0]$titleURL = str_replace(' ', '-', $title);[/code]


Then change this code:
[code=php:0]$newstext .= "<li><a href=\"$self?title=$title&id=$id\">$title</a></li>\r\n";[/code]

to this:
[code=php:0]$newstext .= "<li><a href=\"{$self}{$id}-{$titleURL}.htm\">$title</a></li>\r\n";[/code]


That should be right. Test it and see
Thank you very much for your help. I have been working and suffering with mod rewrite and php linking structure- i am an official newbie! - , but your reply did give me lots of light in the tunnel, and i was able to figure out how to do it! Thank you once again for your help.

Phpfreaks rules! :o
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.