【问题标题】:Can't style PHP echo无法设置 PHP 回显样式
【发布时间】:2017-05-08 10:48:26
【问题描述】:

我无法在 PHP 中使用 CSS 设置回显样式。不管我做什么,它都不起作用。 php 函数是一个搜索栏,它将回显来自 mysql 数据库的结果。 这是 CSS:

.php {
font-family: montserrat, sans-serif;
font-style: normal;
font-weight: 200;
text-align: justify;
color: rgba(200,200,200,1.00);
}

PHP:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<form  method="GET" action="index.php"  id="searchform"> 
<input  type="text" name="query" placeholder="Enter keyword..."> 
<input  style="width:30%;height:24px;" type="submit" name="submit" value="Search">
</form>
<?php

$hostname = "localhost";
$username = "user123";
$password = "pass123";

mysql_connect($hostname, $username, $password);
mysql_select_db("tehdatabase") or die(mysql_error());

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

$min_length = 1;
// 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 env
        WHERE (`id` LIKE '%".$query."%') OR (`name` LIKE '%".$query."%') OR (`short` LIKE '%".$query."%') OR (`short_withtag` LIKE '%".$query."%')") or die(mysql_error());

    // * means that it selects all fields, you can also write: `id`, `title`, `text`
    // articles 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><h3>".$results['id']."</h3>".$results['name'].$results['short'].$results['short_withtag']."</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 "No results";
    }

}
else{ // if query length is less than minimum
    echo "Please enter at least one character";
}
?>
<div class="php">
<?php
echo("$output");
?>
</div>
</body>
</html>

我在互联网上找到了这个,它工作得很好,除了一件事。 应该设置输出样式的 div 类 php 不起作用。回声是黑色的,时代新罗马文字。我该如何解决这个问题?

【问题讨论】:

  • 您没有“设置 PHP 回显样式”。样式完全发生在客户端。此处生成的最简单的 HTML 是什么?
  • 正如大卫所说,您的 PHP 代码与此无关。它返回的任何内容都覆盖了您的 CSS 样式,或者您在标记中缺少某些内容

标签: php html styles echo


【解决方案1】:

您没有在 div.php 块中回显任何内容。 $output 从未在您的代码中定义。你在 div.php 块之前回显了很多次。

在我看来,您想在各个部分定义或附加 $output 而不是 echo:

echo "No results";

或者只是将&lt;div class="php"&gt; 放在 PHP 块之前,这样回声就会在其中。

【讨论】:

  • OP 的调试提示:在检查实际 HTML(并看到空的 div)时,这个问题会立即显而易见。当出现样式问题时,总是首先查看标记和 CSS。
  • 是,或者不抑制错误,因为 PHP 会发布一条通知说 $output 未定义。
猜你喜欢
  • 1970-01-01
  • 2013-08-17
  • 1970-01-01
  • 1970-01-01
  • 2020-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-05
相关资源
最近更新 更多