0

我在将变量设置为 echo<<<_END 构造中的链接时遇到问题

这是我的代码:

<?php //submit_build.php

include_once 'header.php';
require_once 'login_builds.php';
include_once 'functions.php';

$db_server = mysql_connect($db_hostname, $db_username, $db_password);

mysql_select_db($db_database)
    or die("Unable to select database: " . mysql_error());

?>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="en-us" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Weapon Build Creator</title>

<link href="styles/main.css" rel="stylesheet" type="text/css" />

<style>

.bannerdiv
{
    display: block;
    width: 620px;
    height;
    position: static;
    background-image:url('images/news_banner_072813.jpg');
    text-align: center; 
    border-radius: 15px;
    moz-border-radius: 15px;
    moz-box-shadow: 0 0 30px 5px #999;
    box-shadow: 0 0 30px 5px #999;
    height: 300px;
    margin-left: 25px;

}
.form
{
    display: block;
    width: 670px;
    left: 50%;
    margin-left: -335px;
    margin-top: 100px;
    float: left;
    position: absolute;
    background-image:url('images/greystripe_bg.jpg');
    text-align: center; 
    border-radius: 15px;
    moz-border-radius: 15px;
    moz-box-shadow: 0 0 30px 5px #999;
    box-shadow: 0 0 30px 5px #999;
    height: 1000px;
}


</style>

</head>

<body style="background-image: url('images/bg.jpg')">

<div id="form" class="form">
    <div class="newsdiv"><p class="title">Search / Browse Builds</p>
        <p class="newsbody">&nbsp;Build your search query by inputting at least 1 of 
        the fields below.</p>
    <form method="post" action="search_query.php"><p class="timestamp">
        <input name="buildname" type="text" style="vertical-align:middle">&nbsp;&nbsp; Build Name</p>
        <p class="timestamp">
        <select name="weapon" class="dropdown" style="vertical-align:middle">
        <option></option>
        <option>Acrid</option>
        <option>Afuris</option>
        <option>Akbolto</option>
        <option>Aklato</option>
        <option>Amphis</option>
        <option>Ankyros</option>
        <option>Bo</option>
        <option>Boar</option>
        <option>Bolto</option>
        <option>Boltor</option>
        <option>Braton</option>
        <option>Braton Prime</option>
        <option>Braton Vandal </option>
        <option>Bronco</option>
        <option>Bronco Prime</option>
        <option>Burston </option>
        <option>Ceramic Dagger</option>
        <option>Cronus</option>
        <option>Dark Dagger</option>
        <option>Dark Sword</option>
        <option>Dera</option>
        <option>Despair</option>
        <option>Dread</option>
        <option>Dual Broncos</option>
        <option>Dual Cleavers</option>
        <option>Dual Ether</option>
        <option>Dual Heat Swords</option>
        <option>Dual Skana</option>
        <option>Dual Zoren</option>
        <option>Ether Daggers</option>
        <option>Ether Sword</option>
        <option>Fang</option>
        <option>Fang Prime</option>
        <option>Flux Rifle</option>
        <option>Fragor</option>
        <option>Furax</option>
        <option>Furis</option>
        <option>Glaive</option>
        <option>Gorgon </option>
        <option>Grakata</option>
        <option>Gram</option>
        <option>Hate</option>
        <option>Heat Dagger</option>
        <option>Heat Sword</option>
        <option>Hek</option>
        <option>Hikou</option>
        <option>Ignis</option>
        <option>Jaw Sword</option>
        <option>Kestrel</option>
        <option>Kogake</option>
        <option>Kraken</option>
        <option>Kunai</option>
        <option>Lanka</option>
        <option>Lato</option>
        <option>Lato Prime</option>
        <option>Lato Vandal</option>
        <option>Latron</option>
        <option>Latron Prime</option>
        <option>Lex</option>
        <option>Machete</option>
        <option>Mire</option>
        <option>MK1-Braton</option>
        <option>Ogris</option>
        <option>Orthos</option>
        <option>Orthos Prime</option>
        <option>Pangolin Sword</option>
        <option>Paris</option>
        <option>Paris Prime</option>
        <option>Plasma Sword</option>
        <option>Prova</option>
        <option>Reaper Prime</option>
        <option>Scindo</option>
        <option>Seer</option>
        <option>Sicarus</option>
        <option>Skana</option>
        <option>Skana Prime</option>
        <option>Snipetron</option>
        <option>Snipetron Vandal</option>
        <option>Sobek</option>
        <option>Spectra</option>
        <option>Strun</option>
        <option>Supra</option>
        <option>Torid</option>
        <option>Twin Vipers</option>
        <option>Vasto</option>
        <option>Viper</option>
        <option>Vulkar</option>
        </select>&nbsp;&nbsp; Weapon</p>
        <p class="timestamp">
        <select class ="dropdown" name="category" style="vertical-align:middle">
        <option></option>
        <option>Defensive</option>
        <option>Balanced</option>
        <option>High DPS</option>
        </select>&nbsp;&nbsp; Category</p>
        <p class="timestamp">
        <input name="id" type="text" style="vertical-align:middle">&nbsp;&nbsp; 
        Weapon ID Number</p>
        <p class="timestamp">
        <input name="search" type="submit" value="Search Builds">&nbsp;</p>
        <p class="timestamp">



        <p class="title">SEARCH RESULTS</p>

<?php //search result table

if (isset($_POST['buildname']) ||
    isset($_POST['weapon']) ||
    isset($_POST['category']) ||
    isset($_POST['id']))

{

$sqlArray = array();

    if(!empty($_POST['buildname'])){
        $sqlArray[] = "buildname='" . mysql_real_escape_string($_POST['buildname']) . "'";
    }

    if(!empty($_POST['weapon'])){
        $sqlArray[] = "weapon='" . mysql_real_escape_string($_POST['weapon']) . "'";
    }

    if(!empty($_POST['category'])){
        $sqlArray[] = "category='" . mysql_real_escape_string($_POST['category']) . "'";
    }

    if(!empty($_POST['id'])){
        $sqlArray[] = "id='" . mysql_real_escape_string($_POST['id']) . "'";
    }

    $searchstring = "SELECT buildname,weapon,category,id,author,buildname FROM weapons " .
                    "WHERE " . implode(' AND ', $sqlArray);

$result = mysql_query($searchstring);

if (!$result) die ("Database access failed: " . mysql_error());

$rows = mysql_num_rows($result);

echo<<<_END

        <table class="searchresult">
        <tr style="width:100%">
        <td class="newsbody" style="width:5%">ID</td>
        <td class="newsbody" style="width:40%">Build Name</td>
        <td class="newsbody" style="width:20%">Weapon</td>
        <td class="newsbody" style="width:20%">Category</td>
        <td class="newsbody" style="width:15%">Author</td>
        </tr>
_END;

for ($j = 0; $j < $rows; ++$j)
{
$row = mysql_fetch_row($result);
echo<<<_END

        <tr style="width:100%">
        <td class="newsbody" style="width:5%">$row[3]</td>
        <td class="newsbody" style="width:40%"><a href="view_build.php?buildname=$row[5]">$row[0]</a></td>
        <td class="newsbody" style="width:20%">$row[1]</td>
        <td class="newsbody" style="width:20%">$row[2]</td>
        <td class="newsbody" style="width:15%">$row[4]</td>
        </tr>

_END;
}

echo<<<_END

        </table>

_END;

}

?>

</form>
<p><br /><br /><br /><br /><br /></p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p><br /></p>
    </div>

</body>
</html>

我遇到问题的代码行是第 236 行,在代码底部附近以粗体显示。

这个:

<td class="newsbody" style="width:40%"><a href="view_build.php?buildname=$row[5]">$row[0]</a></td>

我基本上希望搜索结果显示一个值,例如“Dual Broncos”,然后在链接到 view_build.php?buildurl=Dual+Broncos 的文本周围有一个超链接。

我不知道如何正确插入 urlencode 函数,因为当我放置标签时,它说我的 HTML 错误(它说表格列结束标签无效/不必要)。

所以我尝试在数据库输入时让 urlencode 工作,这样每个对象都有一个“buildname”和一个“buildurl”,其中 buildurl = urlencode($buildname)。此代码有效,并且我的数据库输入了值“Dual+Broncos”,但是当我检索该行以将其放入位置 view_build.php?buildurl=$row[5] 的链接中时,它不显示'+' 应该在编码的 URL 中。

这个有什么帮助吗?或者关于如何以另一种方式执行此功能的建议?

非常感谢!

PS。我包含了我的整个代码,以便您可以看到 HTML 标记和 PHP 标记的开始和结束位置。

4

2 回答 2

0

只需在heredoc之前使用一个变量来存储编码值,然后将其插入。

$value = urlencode($row[5]);
...
<td class="newsbody" style="width:40%"><a href="view_build.php?buildname=$value">$row[0]</a></td>
于 2013-07-29T15:18:32.853 回答
0

只需在循环外进行 urlencoding:

for($j = 0; ...) {
   $encoded = urlencode($row[0]);
   echo <<<EOL

....?buildname=$encoded
EOL;
}

您可以使用变量函数并“内联”编码,例如

$enc = 'urlencode';
echo <<<EOL
 ... ?buildname={$enc($row[0])}
EOL;

但是这样的代码很快就变得完全不可维护并且非常难以调试。

于 2013-07-29T15:18:55.843 回答