如何从数据库条目创建永久可访问的URL?

时间:2016-11-22 18:08:06

标签: php mysql url

我正在尝试为我自己的国家制作this site的镜像,但我很难理解如何从我的数据库条目中创建新的网址。

例如,在我链接的网站上,它有和id / name来识别那个确切的网站,我想达到同样的目的,使单独的问题可以共享。

我的当前代码仅用于在localhost网址上测试1个问题。

这是我目前得到的一小段内容:

    <?php
    $id = 1;
    $result = performQuery("SELECT * FROM dilemmas WHERE id = ".$id);
    while($row = mysqli_fetch_array($result)){
        $blue_question = utf8_encode($row['blue_question']);
        $blue_votes =  utf8_encode($row['blue_votes']);
        $red_question = utf8_encode($row['red_question']);
        $red_votes = utf8_encode($row['red_votes']);
    }
    ?>

<div class="answer-peek">
    <div class="peek-text">Vil du helst...</div>
    <div class="peek-buttons">
        <div class="peek-bluebutton"><?php echo $blue_question ?></div>
        <div class="peek-redbutton"><?php echo $red_question ?></div>
    </div>
</div>
<div class="row text-center" id="textrow">
    <h3 style="color:white;">Vil du helst...</h3>
</div>
<div class="row" id="buttonrow">
<div class="col-md-1">
    <div class="button-left"></div>
</div>
<div class="col-md-10"><
    <div class="col-md-5 col-md-offset-1 button" id="bluebutton">
    <div class="blueCheckDiv"></div>
        <div id="text-container">
            <table cellpadding="0">
                <tbody>
                    <tr>
                        <td valign="middle">
                            <div class="result">
                                <div class="percentage">
                                    <?php
                                    $percent = $blue_votes/($blue_votes + $red_votes);
                                    $percent_friendly = number_format( $percent * 100, 0 ) . '%';
                                    echo $percent_friendly;
                                    ?>
                                </div>
                                <div class="total-votes">
                                <span class="count">
                                <?php echo $blue_votes; ?>
                                </span>
                                <span class="word">
                                    enige
                                </span>
                                </div>
                                <div class="question-text">
                                <?php echo $blue_question ?>
                                </div>
                            </div>
                            <p class="question">
                            <?php echo $blue_question ?>
                            </p>
                        </td>
                    </tr>
                </tbody>
            </table>

        </div>
    </div>
    <div class="col-md-5 button" id="redbutton">
    <div class="redCheckDiv"></div>
        <div id="text-container">
            <table cellpadding="0">
                <tbody>
                    <tr>
                        <td valign="middle">
                            <div class="result">
                                <div class="percentage">
                                    <?php
                                    $percent = $blue_votes/($blue_votes + $red_votes);
                                    $percent_friendly = number_format( $percent * 100, 2 ) . '%';
                                    echo $percent_friendly;
                                    ?>
                                </div>
                                <div class="total-votes">
                                <span class="count">
                                    <?php echo $red_votes ?>
                                </span>
                                <span class="word">
                                    uenige
                                </span>
                                </div>
                                <div class="question-text">
                                    <?php echo $red_question ?>
                                </div>
                            </div>
                            <p class="question">
                            <?php echo $red_question ?>
                            </p>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    </div>
    <div class="col-md-1">
        <div class="button-right"></div>
    </div>
</div>

<div class="row" id="dividerrow">
    <div class="col-md-8" id="divider-left"></div>
    <div class="col-md-4" id="divider-right"></div>
</div>

<div class="row" id="socialrow">
    <div class="col-md-8 socialcol" id="col-left"></div>
    <div class="col-md-4 socialcol" id="col-right"></div>
</div>

1 个答案:

答案 0 :(得分:0)

我建议你在评论的评论中使用一个框架。

无论如何,要做你正在尝试的事情,你可以尝试这样的事情:

<?php
$id = 1;
if (true === isset($_GET['id'])) {
    $id = $_GET['id'];
}

$result = performQuery("SELECT * FROM dilemmas WHERE id = ".$id);
while($row = mysqli_fetch_array($result)){
    $blue_question = utf8_encode($row['blue_question']);
    $blue_votes =  utf8_encode($row['blue_votes']);
    $red_question = utf8_encode($row['red_question']);
    $red_votes = utf8_encode($row['red_votes']);
}

您可以使用example.com/page.php?id=1234

之类的内容来调用同一页面

因此,您可以为每个问题构建链接:

<a href="/page.php?id=<?php echo $row['id']; ?>">Share</a>

注意:这是一个非常脏的代码,它会导致许多安全问题。我写它只是为了让你能够理解动态,但是,我建议你在第一次开始时使用一个框架:make all in&#34; plain&#34; PHP将使您处理许多复杂的问题,如安全性或路由或服务的处理等...使用框架找到非常好的指导,并更快更好地学习您选择的语言(PHP在此情况)。