将CSS样式应用于PHP输出
下面显示来自数据库字段“从不”的HTML结果。我正在尝试将CSS样式应用于输出 这是我的…将CSS样式应用于PHP输出,php,mysql,css,Php,Mysql,Css,下面显示来自数据库字段“从不”的HTML结果。我正在尝试将CSS样式应用于输出 这是我的… echo "<p><strong>Never:</strong> ".$results['Never']."".$results['text']."</p><br />"; <div id="title"> <p><h1>Database Search Results</h1>&
echo "<p><strong>Never:</strong> ".$results['Never']."".$results['text']."</p><br />";
<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-->
…但它没有正确应用。我收到一个语法错误和头痛。我放错地方了吗
未填充$results
变量
编辑:添加代码
这是我的连接…
echo "<p><strong>Never:</strong> ".$results['<div id="nevermsg">'Never'</div>]."".$results['text']."</p><br />";
#nevermsg { color: red; }
<?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的直接输出
// some preceding PHP code
?>
<p>
<strong>Never:</strong> <span id="nevermsg"><?=$results['Never'].$results['text'] ?></span>
</p>
//前面的一些PHP代码
?>
从不:
它将更具可读性,不会有混乱的HTML和PHP引号,也无需转义…尝试以下方法:
<?php
echo "<p><strong>Never:</strong> " . $results['<div id=\"nevermsg\">\'Never\'</div>'] . $results['text'] . "</p><br />";
?>
您的查询很好,是您的HTML非常混乱。尝试替换此:
echo "<p><strong>Never:</strong> ".$results['Never']."".$results['text']."</p><br />";
echo“从不:“$results['Never'..”.$results['text'.”
”;
与:
?>
从不:
您需要在while
循环中更改数组类型。将返回一个访问的标准数组,如$array[0]
而不是$array['my_key']]
,因此请使用
因此,与此相反:
while ($results = mysql_fetch_array($raw_results)) {
echo "<p><strong>Never:</strong> <span id=\"nevermsg\">".$results['Never']."</span></p>"; //Doesn't
}
请参见循环的PHP fiddle示例和动作中的
。由于明显的原因,SQL不能在小提琴中复制。这不可能是有效的PHP<代码>解析错误:语法错误,意外的“从不”(T_字符串),应为“]”
顶部的是有效的PHP。最下面的显然不是。@webfrogs然后您需要发布用于建立数据库连接和查询的PHP代码。我猜这就是它失败的地方。你的问题是关于PHP语法错误,还是CSS“makeit red”代码?如果是关于PHP的,请澄清您的问题,并将其标记为PHP*。如果是关于CSS的,请删除所有PHP,并提供原始HTML和CSS来演示您的问题。你想做什么?这不是他在评论中发现的问题。他的查询似乎不起作用。查询工作正常。我希望输出以红色显示。不幸的是,它所做的只是隐藏我试图使其变为红色的输出的显示。您可能需要执行echo print\r($results)
只需查看返回的每个列的键和值。如果你的键不正确,那么很容易从那里调整代码。我做到了。我很好奇你为什么让我回答这个问题。无论哪种方式,它都没有改变“从不”行输出的样式。你可以重新查看我添加的更新链接,但它所做的只是重复结果。现在,它显示了两组结果。您可以查看@webfrogs,它告诉我您不需要第二个键。我把它拿走了。。。仍然不能解释数据的缺乏。您是否进行了echo打印(结果)代码>?这将告诉你你的实际回报。你介意添加那一行吗?我来看看输出。查询很好。我正在返回我想要的结果。我只想要这一行echo“从不:”$results['Never']。“$results['text']。”
返回红色字体的结果。我尝试了,但出现了服务器错误。。。页面甚至无法显示。
?>
<strong>Never:</strong>
<div id="nevermsg"><?= $results['Never']; ?></div>
<br>
<?php
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
}
while ($results = mysql_fetch_assoc($raw_results)) {
foreach ($results as $key => $value) {
echo "<span id=\"nevermsg\"><p><strong>$key:</strong> ".$value."</p></span><br/>";
}
}