简体   繁体   English

来自DB,PHP和.htaccess的URL友好

[英]URL friendly from DB, php and .htaccess

can help me? 可以帮我? I've a site url like: 我有一个网站网址,例如:

http://www.example.com/episodio/ID http://www.example.com/episodio/ID

ID = row number from Database. ID =数据库中的行号。

I want to display the page title. 我想显示页面标题。

I've this: 我有这个:

.htaccess file: .htaccess文件:

RewriteEngine on
RewriteRule ^episodio/(\w+)/?$ episodio.php?id=$1

php file: php文件:

$sql = "SELECT * FROM "episodios" WHERE serie = ".$id." AND temporada = ".$i." ORDER BY "episodio" ASC";
            $result = $conn->query($sql);
                if ($result->num_rows > 0) {
                while($row = $result->fetch_assoc()) {
                    if ($row['episodio'] == 0) {
                        echo '<a href="episodio/'.$row["id"].'"><li><strong>Episodio doble:</strong> '.$row["nombre"].'<img src="http://www.example.net/img/play.png" class="img-play"></li></a>';
                    }else{
                        echo '<a href="episodio/'.$row["id"].'"><li><strong>Episodio '.$row['episodio'].':</strong> '.$row["nombre"].'<img src="http://www.example.net/img/play.png" class="img-play"></li></a>';
                    }
            ?>
            <?php } ?>

You're using $_GET in your ID anywhere before this code? 您在此代码之前的任何地方都在ID中使用$ _GET吗?

I mean, $_GET['id'] . 我的意思是$_GET['id']

Most PHP versions (since 4...) doesn't catch the var just for its name. 大多数PHP版本(从4开始...)并不能仅仅因为其名称而捕获var。

I'd recommend getting all your variables from the DB first, and then printing out the HTML (perhaps from a template file). 我建议先从数据库中获取所有变量,然后再打印出HTML(也许从模板文件中)。 We assume that $row contains more than just an ID? 我们假设$ row包含的不仅仅是ID? Perhaps an "título del episodio", also? 也许还有“títulodel episodio”?

if ($result->num_rows > 0) {
    $episodios = array();
    $x = 0;
    while($row = $result->fetch_assoc()) {
        $episodios[$x]['id'] = $row['id'];
        if ($x == 0) {
            $PageTitle = $row['title'];
        }
        $x++;
    }

Then your template can use $PageTitle (from the first episode), and you can loop through the $episodios array to construct the links for the other episodes. 然后,您的模板可以使用$ PageTitle(从第一个情节开始),然后可以遍历$ episodios数组来构造其他情节的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM