以下显示数据库字段“从不”的HTML结果。我正在尝试将CSS样式应用于输出。
这就是我所拥有的......
echo "<p><strong>Never:</strong> ".$results['Never']."".$results['text']."</p><br />";
这是我尝试过的......
echo "<p><strong>Never:</strong> ".$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 >
$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> ".$results['Question']."".$results['text']."</h2></u></strong></p><br />";
echo "<p><strong>Sample Response / Must:</strong> ".$results['Sample Response / Must']."".$results['text']."</p><br />";
//echo "<p><strong>Location:</strong> <a href='".$results['Location']."' target='_blank'>".$results['SchoolLocation']."</a>".$results['text']."</p><br />";
echo "<p><strong>Must:</strong> ".$results['Must']."".$results['text']."</p><br />";
echo "<p><strong>Can:</strong> ".$results['Can']."".$results['text']."</p><br />";
//echo "<p><strong>Never:</strong> ".$results['Never']."".$results['text']."</p><br />";
echo "<span id=\"nevermsg\"><p><strong>Never:</strong> ".$results['Never']."".$results['text']."</p></span><br />";
echo "<p><strong>_____________________________________________</strong> "."</p><br />";
echo "<p><strong>Tags:</strong> ".$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-->
此外,这是指向该网站的链接,我在网址here中添加了一个查询。
最后,我将样式表称为“global.css”,这就是样式所在的位置。
#nevermsg { color: red; }
答案 0 :(得分:1)
您需要在while
循环中更改数组类型。 mysql_fetch_array
将返回一个标准数组,例如$array[0]
而不是$array['my_key']
,因此请使用mysql_fetch_assoc
。
所以不要这样:
while ($results = mysql_fetch_array($raw_results)) {
echo "<p><strong>Never:</strong> <span id=\"nevermsg\">".$results['Never']."</span></p>"; //Doesn't
}
这样做:
while ($results = mysql_fetch_assoc($raw_results)) {
echo "<p><strong>Never:</strong> <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> ".$value."</p></span><br/>";
}
}
请参阅循环的PHP小提琴示例和<span>
in here。出于显而易见的原因,SQL无法在小提琴中复制。
答案 1 :(得分:0)
为什么不使用HTML的直接输出?
// some preceding PHP code
?>
<p>
<strong>Never:</strong> <span id="nevermsg"><?=$results['Never'].$results['text'] ?></span>
</p>
它将更具可读性,并且不会出现HTML和PHP引用的混乱,也无需转义它们......
答案 2 :(得分:0)
试试这个:
<?php
echo "<p><strong>Never:</strong> " . $results['<div id=\"nevermsg\">\'Never\'</div>'] . $results['text'] . "</p><br />";
?>
答案 3 :(得分:0)
您的查询很好,这是您的HTML非常混乱。尝试替换它:
echo "<p><strong>Never:</strong> ".$results['Never']."".$results['text']."</p><br />";
使用:
?>
<strong>Never:</strong>
<div id="nevermsg"><?= $results['Never']; ?></div>
<br>
<?php