我在让我的 php 脚本显示一个 url slug 时遇到问题,一旦显示文章页面,该 url slug 就会由文章标题表示。我将其设置为使用我从自己生成的表单发布的所有文章自动更新文章页面,然后 article.php 页面将根据我的数据库中的内容填充每篇文章的不同内容。
我唯一想在这里更改的是它不显示 /article.php?id=23,我希望它显示一个根据加载文章的标题创建的 slug。我已经在我的数据库中为 slug 创建了一个新列,并将其设置为自动以 slugified 形式将新条目放置在数据库中(www.example.com/article-title-here)但是我在错误之后遇到错误当我尝试填充现有文章的 slug 列时。
这是显示所有文章的页面的 php 脚本:
<?php
$conn = new mysqli();
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM articles ORDER BY published_date DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<div class="postcard">';
if (!empty($row['image'])) {
echo '<div class="postcard-image" style="background-image: url(' . $row['image'] . ')"></div>';
}
echo '<div class="postcard-content">';
echo '<h3>' . $row['title'] . '</h3>';
echo '<p class="published-date">' . date('F j, Y', strtotime($row['published_date'])) . '</p>';
echo '<p>' . substr(html_entity_decode(strip_tags($row['content'])), 0, 150) . '...</p>';
echo '<a href="article.php?id=' . $row['id'] . '">Read More</a>';
echo '</div>'; // postcard-content
echo '</div>'; // postcard
}
} else {
echo 'No articles found.';
}
$conn->close();
?>
这里是我的 article.php 页面的 php 脚本,它显示点击了哪篇文章的“阅读更多”链接:
<?php
if (!isset($_GET['id'])) {
header('Location: articles.php');
}
$conn = new mysqli();
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$id = $_GET['id'];
$sql = "SELECT * FROM articles WHERE id = $id";
$result = $conn->query($sql);
if ($result->num_rows == 1) {
$row = $result->fetch_assoc();
$article_title = $row['title'];
$meta_description = $row['meta_description'];
echo '<head>';
echo '<meta name="description" content="' . $meta_description . '">';
echo '<title>Green Life Resource - ' . $article_title . '</title>'; // Add the title tag
echo '</head>';
echo '<body>';
echo '<div class="breadcrumbs">';
echo '<a href="index.php">Home</a> > ';
echo '<a href="articles.php">Articles</a> > ';
echo '<span>' . $article_title . '</span>';
echo '</div>';
// Generate table of contents
$content = html_entity_decode($row['content'], ENT_QUOTES, 'UTF-8');
$toc = '';
$pattern = '/<h(2|3)>(.*?)<\/h(2|3)>/';
preg_match_all($pattern, $content, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$tag = $match[1];
$title = $match[2];
$id = preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', $title));
$toc .= '<li><a href="#' . $id . '">' . $title . '</a></li>';
$content = str_replace('<h' . $tag . '>' . $title . '</h' . $tag . '>', '<h' . $tag . ' id="' . $id . '">' . $title . '</h' . $tag . '>', $content);
}
echo '<div class="toc-container">';
echo '<h2>Table of Contents</h2>';
echo '<ul class="toc">' . $toc . '</ul>';
echo '</div>';
echo '<section id="article-container">';
echo '<h1>' . $article_title . '</h1>';
echo '<p class="published-date">Published - ' . date('F j, Y', strtotime($row['published_date'])) . '</p>';
if (!empty($row['image'])) {
echo '<div class="article-image" style="background-image: url(' . $row['image'] . ')"></div>';
}
echo '<div class="article-content">' . $content . '</div>';
echo '</section>';
// Set the page title to the article title
echo '<script>document.title = "Green Life Resource - ' . $article_title . '";</script>';
// Get the current page URL
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
// Social media sharing links
$facebook_link = "https://www.facebook.com/sharer.php?u=" . $url . "&picture=" . $row['image'];
$twitter_link = "https://twitter.com/share?url=" . $url . "&text=Check out this blog post&image=" . $row['image'];
$linkedin_link = "https://www.linkedin.com/shareArticle?mini=true&url=" . $url . "&title=" . $article_title . "&summary=" . $meta_description . "&source=Green Life Resource&thumbnailUrl=" . $row['image'];
$pinterest_link = "https://www.pinterest.com/pin/create/button/?url=" . $url . "&media=" . $row['image'] . "&description=" . $article_title;
echo '<hr>';
echo '<div class="social-icons">';
echo '<h3>Share This Article</h3>';
echo '<a href="' . $facebook_link . '" target="_blank"><i class="fab fa-facebook fa-xl"></i></a>';
echo '<a href="' . $twitter_link . '" target="_blank"><i class="fab fa-twitter fa-xl"></i></a>';
echo '<a href="' . $linkedin_link . '" target="_blank"><i class="fab fa-linkedin fa-xl"></i></a>';
echo '<a href="' . $pinterest_link . '" target="_blank"><i class="fab fa-pinterest fa-xl"></i></a>';
echo '</div>';
} else {
echo '<p>Sorry, the article you are looking for cannot be found.</p>';
}
$conn->close();
?>
我尝试更改链接以显示 slug 而不是 articles.php 脚本上的 id 以如下所示阅读:
echo '<a href="article.php?slug=' . $row['slug'] . '">Read More</a>';
然后我更改了我的 article.php 脚本以获取 slug 而不是 id,如下所示:
if (!isset($_GET['slug'])) {
header('Location: articles.php');
}
$conn = new mysqli();
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$slug = $_GET['slug'];
$sql = "SELECT * FROM articles WHERE slug = '$slug'";
$result = $conn->query($sql);