0

下面显示来自数据库字段“从不”的 HTML 结果。我正在尝试将 CSS 样式应用于输出。

这就是我所拥有的...

echo "<p><strong>Never:</strong>&nbsp;".$results['Never']."".$results['text']."</p><br />";

这是我尝试过的...

echo "<p><strong>Never:</strong>&nbsp;".$results['<div id="nevermsg">'Never'</div>]."".$results['text']."</p><br />";

这是我的CSS...

#nevermsg { color: red; }

...但它没有正确应用。我收到语法错误和头痛。我把这个放在错误的地方了吗?

$results变量没有被填充。

编辑:添加代码

这是我的连接...

<?php
    mysql_connect("localhost", "jpcso_compliance", "abc*123") or die("Error connecting to database: ".mysql_error());
    /*
        localhost - it's location of the mysql server
        root - username
        third is your password

        if connection fails it will stop loading the page and display an error
    */

    mysql_select_db("jpcsolut_compliance") or die(mysql_error());
    /* jpcsolut_webfro_HS is the name of database we've created */
?>

输出没有其他 HTML 格式,除了这里的...

<div id="title">
<p><h1>Database Search Results</h1></p></div>
<br />
<div id="main_inner">


<?php
    $query = $_GET['query']; 
    // gets value sent over search form

    $min_length = 2;
    // you can set minimum length of the query if you want

    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then

        $query = htmlspecialchars($query); 
        // changes characters used in html to their equivalents, for example: < to &gt;

        $query = mysql_real_escape_string($query);
        // makes sure nobody uses SQL injection

        $raw_results = mysql_query("SELECT * FROM Compliance
            WHERE (`Question` LIKE '%".$query."%') OR (`Sample Response / Must` LIKE '%".$query."%') OR (`Must` LIKE '%".$query."%') OR (`Can` LIKE '%".$query."%') OR (`Never` LIKE '%".$query."%') OR (`Tags` LIKE '%".$query."%')") or die(mysql_error());

        // * means that it selects all fields, you can also write: `id`, `title`, `text`
        // IllegitimateHighSchools is the name of our table

        // '%$query%' is what we're looking for, % means anything, for example if $query is Hello
        // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
        // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'

        if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

            while($results = mysql_fetch_array($raw_results)){
            // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop

                echo "<p><strong><u><h2>Question:</u>&nbsp;".$results['Question']."".$results['text']."</h2></u></strong></p><br />";

                echo "<p><strong>Sample Response / Must:</strong>&nbsp;".$results['Sample Response / Must']."".$results['text']."</p><br />";
                //echo "<p><strong>Location:</strong>&nbsp;<a href='".$results['Location']."' target='_blank'>".$results['SchoolLocation']."</a>".$results['text']."</p><br />";

                echo "<p><strong>Must:</strong>&nbsp;".$results['Must']."".$results['text']."</p><br />";
                echo "<p><strong>Can:</strong>&nbsp;".$results['Can']."".$results['text']."</p><br />";
                //echo "<p><strong>Never:</strong>&nbsp;".$results['Never']."".$results['text']."</p><br />";
                echo  "<span id=\"nevermsg\"><p><strong>Never:</strong>&nbsp;".$results['Never']."".$results['text']."</p></span><br />";
                echo "<p><strong>_____________________________________________</strong>&nbsp;"."</p><br />";                
                echo "<p><strong>Tags:</strong>&nbsp;".$results['Tags']."".$results['text']."</p>";
                // posts results gotten from database(title and text) you can also show id ($results['id'])
            }

        }
        else{ // if there is no matching rows do following

    echo "<br /><br /><br /><strong><h2>"."You have searched a term that is not in the database. Please contact <a href=\"mailto:" . htmlentities('email@domain.com') . "\">".htmlentities('email@domain.com') . "</a>, if you think this term should be added."."</h2></strong>";
        }

    }
    else{ // if query length is less than minimum
        echo "Minimum length is ".$min_length;
    }
?>
<br />
    <br />
        <br />
            <br />
<br />
    <br />




</div>
<!--End of Inner Main-->

此外,这里是该站点的链接,我在此处的 URL 中包含了一个查询。

最后,我将样式表称为“global.css”,这是样式所在的位置。

#nevermsg { color: red; }
4

4 回答 4

2

您需要更改while循环中的数组类型。 mysql_fetch_array将返回一个访问的标准数组,就像$array[0]不是$array['my_key']这样使用mysql_fetch_assoc

所以代替这个:

    while ($results = mysql_fetch_array($raw_results)) {
            echo "<p><strong>Never:</strong>&nbsp;<span id=\"nevermsg\">".$results['Never']."</span></p>"; //Doesn't
    }

做这个:

    while ($results = mysql_fetch_assoc($raw_results)) {
            echo "<p><strong>Never:</strong>&nbsp;<span id=\"nevermsg\">".$results['Never']."</span></p>"; //Works
    } 

更新

如果您不知道keyis 循环遍历$results数组本身,则另一种选择是foreach

    while ($results = mysql_fetch_assoc($raw_results)) {
         foreach ($results as $key => $value) {
              echo "<span id=\"nevermsg\"><p><strong>$key:</strong>&nbsp;".$value."</p></span><br/>";
         }
    } 

在此处查看循环的 PHP 小提琴示例和<span>实际操作。由于显而易见的原因,SQL 不能在小提琴中复制。

于 2013-07-25T19:33:27.040 回答
0

为什么不使用 HTML 的直接输出?

// some preceding PHP code
?>
<p>
  <strong>Never:</strong>&nbsp;<span id="nevermsg"><?=$results['Never'].$results['text'] ?></span>
</p>

它将更具可读性,并且不会出现 HTML 和 PHP 引号的混乱,也无需转义它们......

于 2013-07-25T18:31:08.603 回答
0

试试这个:

<?php
echo "<p><strong>Never:</strong>&nbsp;" . $results['<div id=\"nevermsg\">\'Never\'</div>'] . $results['text'] . "</p><br />";
?>
于 2013-07-25T19:07:31.020 回答
0

您的查询很好,是您的 HTML 非常混乱。尝试替换这个:

echo "<p><strong>Never:</strong>&nbsp;".$results['Never']."".$results['text']."</p><br />";

和:

?>
<strong>Never:</strong>
<div id="nevermsg"><?= $results['Never']; ?></div>
<br>
<?php
于 2013-07-25T19:21:27.317 回答