簡體   English   中英

將CSS樣式應用於PHP輸出

[英]Apply CSS Styling to PHP output

下面顯示了數據庫字段“從不”的HTML結果。 我正在嘗試將CS​​S樣式應用於輸出。

這就是我所擁有的...

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; }

您需要在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
    } 

更新

如果您不知道key另一種選擇,是通過$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不能在小提琴中重復。

為什么不使用HTML的直接輸出?

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

它將更具可讀性,並且不會出現HTML和PHP引號的混亂情況,也無需轉義它們...

嘗試這個:

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

您的查詢很好,這是您的HTML,非常混亂。 嘗試替換此:

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

帶有:

?>
<strong>Never:</strong>
<div id="nevermsg"><?= $results['Never']; ?></div>
<br>
<?php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM