将 CSS 样式应用到 PHP 输出 [英] Apply CSS Styling to PHP output
问题描述
以下显示来自数据库字段从不"的 HTML 结果.我正在尝试将 CSS 样式应用于输出.
这是我所拥有的...
echo "<p><strong>Never:</strong> ".$results['Never']."".$results['text']."<;/p>
";
这是我尝试过的...
echo "<p><strong>Never:</strong> ".$results['<div id="nevermsg">'Never'</div>]."".$results['text']."</p><br/>";
这是我的 CSS...
#nevermsg { 颜色:红色;}
...但它没有正确应用.我收到一个语法错误和头痛.我是不是把它放在错误的地方?
$results
变量没有被填充.
添加代码
这是我的联系方式...
输出没有其他 HTML 格式,除了这里的内容...
<p><h1>数据库搜索结果</h1></p></div><br/><div id="main_inner"><?php$query = $_GET['query'];//获取通过搜索表单发送的值$min_length = 2;//如果需要,您可以设置查询的最小长度if(strlen($query) >= $min_length){//如果查询长度大于或等于最小长度,则$query = htmlspecialchars($query);//将 html 中使用的字符更改为它们的等效字符,例如:<>$query = mysql_real_escape_string($query);//确保没有人使用 SQL 注入$raw_results = mysql_query("SELECT * FROM 合规性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."%')") 或 die(mysql_error());//* 表示选择所有字段,也可以写:`id`, `title`, `text`//IllegitimateHighSchools 是我们表的名称//'%$query%' 是我们要找的, % 意味着什么,例如 $query 是 Hello//它会匹配 "hello", "Hello man", "gogohello",如果你想精确匹配使用 `title`='$query'//或者,如果您只想匹配完整的单词,因此gogohello"已失效,请使用 '% $query %' ...OR ... '$query %' ... OR ... '% $query'if(mysql_num_rows($raw_results) > 0){//如果返回一行或多行,请执行以下操作while($results = mysql_fetch_array($raw_results)){//$results = mysql_fetch_array($raw_results) 将数据库中的数据放入数组中,当它有效时,它执行循环echo "<p><strong><u><h2>问题:</u> ".$results['Question']."".$results['text']."</h2></u></strong></p><br/>";echo "<p><strong>示例响应/必须:</strong> ".$results['示例响应/必须']."".$results['text']."<;/p>
";//echo "<p><strong>Location:</strong> <a href='".$results['Location']."' target='_blank'>".$results['SchoolLocation']."</a>".$results['text']."</p><br/>";echo "<p><strong>必须:</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>标签:</strong> ".$results['Tags']."".$results['text']."</p>";//发布从数据库中获取的结果(标题和文本)您还可以显示 id ($results['id'])}}else{//如果没有匹配的行,请执行以下操作echo "<br/><br/><br/><strong><h2>"."您搜索的词条不在数据库中.请联系<a href=\"mailto:" . htmlentities('email@domain.com') . "\">".htmlentities('email@domain.com') ."</a>,如果您认为应该添加这个词."."</h2></strong>";}}else{//如果查询长度小于最小值echo "最小长度为 ".$min_length;}?><br/><br/><br/><br/><br/><br/><!--内部主线程结束-->
此外,这里有一个指向该站点的链接,其中我在 URL 此处.
最后,我将样式表称为global.css",这是样式所在的位置.
#nevermsg { 颜色:红色;}
您需要在 while
循环中更改数组类型.mysql_fetch_array
将返回一个标准数组像 $array[0]
而不是 $array['my_key']
那样访问,所以使用 mysql_fetch_assoc
.
所以不是这样:
while ($results = mysql_fetch_array($raw_results)) {echo "<p><strong>从不:</strong><span id=\"nevermsg\">".$results['Never']."</span><;/p>";//没有}
这样做:
while ($results = mysql_fetch_assoc($raw_results)) {echo "<p><strong>从不:</strong><span id=\"nevermsg\">".$results['Never']."</span><;/p>";//作品}
更新:
如果您不知道 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 小提琴示例和 操作 这里.由于显而易见的原因,无法在小提琴中复制 SQL.
The following displays HTML results from the database field "Never". I am trying to apply CSS styling to the output.
Here's what I have...
echo "<p><strong>Never:</strong> ".$results['Never']."".$results['text']."</p><br />";
Here's what I've tried...
echo "<p><strong>Never:</strong> ".$results['<div id="nevermsg">'Never'</div>]."".$results['text']."</p><br />";
Here's my CSS...
#nevermsg { color: red; }
...but it's not applying properly. I am receiving a syntax error and a headache. Am I putting this in the wrong spot?
The $results
variable is not being filled.
Edit: Code Added
Here's my connection...
<?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 */
?>
There is no other HTML formatting for the output, other than what is right here...
<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-->
Additionally, here is a link to the site, where I've included a query in the URL here.
Lastly, I call the stylesheet 'global.css', which is where the style lives.
#nevermsg { color: red; }
You need to change the array type in your while
loop. mysql_fetch_array
will return a standard array accessed like $array[0]
not $array['my_key']
so use mysql_fetch_assoc
.
So instead of this:
while ($results = mysql_fetch_array($raw_results)) {
echo "<p><strong>Never:</strong> <span id=\"nevermsg\">".$results['Never']."</span></p>"; //Doesn't
}
Do this:
while ($results = mysql_fetch_assoc($raw_results)) {
echo "<p><strong>Never:</strong> <span id=\"nevermsg\">".$results['Never']."</span></p>"; //Works
}
UPDATE:
Another option if you don't know the key
is loop through the $results
array itself like so with a 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/>";
}
}
See the PHP fiddle example of the loop and <span>
in action here. For obvious reasons the SQL could not be duplicated in the fiddle.
这篇关于将 CSS 样式应用到 PHP 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!