Jump to content

Php side from mod_rewrite


russe11

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.