从数据库获取ID计数并生成新闻的动态链接

Yesterday I asked this question: https://stackoverflow.com/questions/15117767/add-dynamic-content-to-otherwise-static-php-page I´m sorry for the not very precise question. I accomplished a lot more now by myself but I still have a few questions.

I managed to put my news into a Database Table with the following columns: "id", "news_headline", "news_date", "news_teaser", "news_content", "news_external_link"

On my news/index.php I have the following code now:

<?php
     $con=mysqli_connect("localhost", "XXX", "oizQvdjh", "usr_web3_1");
     // Check connection
     if (mysqli_connect_errno())
     {
           echo "Failed to connect to MySQL: " . mysqli_connect_error();
     }
     $result = mysqli_query($con,"SELECT * FROM tbl_news ORDER BY id DESC");

     while($row = mysqli_fetch_array($result))
     {
       echo "<li class='clearfix'>";
       echo "<a class='news_pic'><img src='/assets/images/news/news_".$row['id']
                  ."_thumb.jpg'></a>";
       echo "<h2 class='info_headline'>" . $row['news_headline'] . "</h2>";
       echo "<p class='date'>" . $row['news_date'] 
                  . " &ndash; <a href='#disqus_thread'></a></p>";
       echo "<p class='news_text'>" . $row['news_teaser'] 
                  . "<br><a class='more-link' href='WHERE_TO'>Read more</a></p>";
       echo "</li>";
     }

     mysqli_close($con);
?>

Everything looks great there and it fetches all my news from the database (five so far) and gets the image which I uploaded by myself on the server with names according to the id.

The single News side has the same code except it has a "WHERE" statement and grabs the content text and not the teaser:

$result = mysqli_query($con,"SELECT * FROM tbl_news WHERE id='1' ORDER BY id DESC");

while($row = mysqli_fetch_array($result))
{
   echo "<h2>" . $row['news_headline'] . "</h2>";
   echo "<p class='date'>" . $row['news_date'] 
         . " &ndash; <a href='#disqus_thread'></a></p>";
   echo "<a class='news_pic'><img src='/assets/images/news/news_".$row['id']."
         .jpg'></a>";
   echo "<p class='news_text'>" . $row['news_content'] . "</p>";
}

mysqli_close($con);

?>

Now to my question. How do I have to save the file of the single news to get it dynamically updatet. In my Index where the href says "WHERE TO" I want a link like <a href="<?php echo $row['news_date'] + slug (I would add a column slug in my table).

The news single php should then be generated and should also say the date and slug in the URL. Like XXX/news/2013-02-18_this-is-a-news-site.php I know that for that I also have to put a variable into the WHERE command: Instead of WHERE id=1 --> id="GET AUTOMATICALLY". I also don´t know what to put here.

Your question is not very clear, by the way:

You need to enable mod_rewrite and choose an appropriate rewrite rule.

Create an .htaccess file like this:

RewriteEngine On
RewriteBase / 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php?path=$1 [L,QSA]

An url like

http://your.site.com/news/2012/10/22

Will be converted to

http://your.site.com/index.php?path=/news/2012/10/22

Get 'path' from querystring and create your query. Be sure to sanitize input properly.

2012/10/22 is almost a mysql valid date: just replace '/' with '-'.

$dt=str_replace('/','-',substr($path,6));

Ensure it is a valid date:

$dt=date('Y-m-d',strtotime($dt));

Now your query could be

$query="SELECT * FROM tbl_news WHERE news_date='$dt'";

This query will give you all news for a specific day.