简体   繁体   English

在html表中显示php搜索结果

[英]display php search results in html table

im running this php script and not quite getting the result i want. 我运行这个PHP脚本,并没有得到我想要的结果。 at the moment its giving me this output 此刻它给了我这个输出

scuba tank 水肺潜水

mike 麦克风

0.00 450.00 5.00 0.00 450.00 5.00

2012-06-04 18:50:22 2012-06-04 18:50:22

scuba tank 水肺潜水

liam 利亚姆

80.00 350.00 2.50 80.00 350.00 2.50

2012-06-04 19:00:09 2012-06-04 19:00:09

Displaying 3 results 显示3条结果

scuba tank 水肺潜水

josh 玩笑

410.00 0.00 5.00 410.00 0.00 5.00

2012-06-04 19:00:09 2012-06-04 19:00:09

its pretty much what i want except the line displaying 3 results should be displayed at the end instead of before the last entry. 它几乎是我想要的,除了显示3个结果的行应该显示在结尾而不是最后一个条目之前。 what do i need to do to my script to fix this? 我需要对我的脚本做些什么来解决这个问题?

<?php
    $host = "localhost";
    $user = "root";
    $pass = null;

    // ========================================
    $dbhost = @mysql_connect($host, $user, $pass) or die("Unable to connect to server");

    @mysql_select_db("divebay") or die("Unable to select database");

    $var = "scuba";
    $query = trim($var);

    if(!isset($query)){
        echo "Your search was invalid";
        exit;
    } //line 18

    $sql = "SELECT * FROM auction WHERE name LIKE '%" . $query . "%'";

    $result = mysql_query($sql);
    $numrows = mysql_num_rows($result);
    mysql_close($dbhost);

    if($numrows == 0){
        echo "Sorry, your search did not return any results";
    }

    $i = 0;

    while($i < $numrows){
        $row = mysql_fetch_array($result);

        $ID = $row['ID'];
        $name = $row['name'];
        $owner = $row['owner'];
        $holder = $row['holder'];
        $start = $row['sprice'];
        $current = $row['cprice'];
        $instant = $row['iprice'];
        $inc = $row['incprice'];
        $image = $row['img'];
        $time = $row['stime'];
        $length = $row['duration'];

        echo "
            <?xml version = '1.0' encoding = 'utf-8'?>
            <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org    /TR/xhtml1/DTD/xhtml1-transitional.dtd'>
            <html xmlns='http://www.w3.org/1999/xhtml'>
            <head>
                <title>searchdbresults</title>
            </head>

            <body>  
                <table style='width = 800px;'>
                    <tr style ='height = 200px;'>
                        <td style ='width = 200px;'></td>

                        <td style ='width = 300px;'>
                            <div style ='180px'> $name </div>
                            <div> $owner </div>
                        </td>

                        <td style='width =200px'>
                            <div style='height = 100px'> $current </div>
                            <div style='height = 50px'> $instant </div>
                            <div> $inc </div>
                        </td>

                        <td> $time </td>
                    </tr>
        ";

        $i++;   
    }

    echo "
                <tr> Displaying $numrows results</tr>
            </table>
        </body>
        </html>
    ";
?>

I can't comment, but there is several problems in your code. 我无法发表评论,但您的代码中存在一些问题。

1st the style must double quote 第一,风格必须双引号

<div style="width:100%;"> 

for example. 例如。

2nd : there must be a td inside a tr for this line : Displaying $numrows results 第二:此行中的tr内必须有一个td:显示$ numrows结果

and last one i see is : you have this : 我看到的最后一个是:你有这个:

<?xml version = '1.0' encoding = 'utf-8'?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org    /TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>

<head>
<title>searchdbresults</title>
</head>
<body>  

inside your while loop, so its several times in your page, and it must not 在你的while循环中,所以它在你的页面中多次,而且一定不能

You also have the table opening in your while loop, but not the closing. 您还可以在while循环中打开表,但不是结束。 So it's opened several times, but opened only once. 所以它开了好几次,但只开了一次。

edit : you also need to add protection into your sql query 编辑:您还需要在sql查询中添加保护

Your script is generating "messy" HTML. 您的脚本正在生成“混乱”的HTML。 From what I see your so generated HTML page will have (in the current example) 3 DOCTYPE definitions, 3 head 's as well as 3 opening table tags and only one closing /table . 从我看到你生成的这样的HTML页面(在当前的例子中)将有3个DOCTYPE定义,3个head以及3个开放table标签和只有一个结束/table And also you don't need to echo every single html entity, you can use plain html in php files Try something like that: 而且你也不需要echo每一个html实体,你可以在php文件中使用普通的html尝试类似的东西:

<?xml version = '1.0' encoding = 'utf-8'?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>

<head>
<title>searchdbresults</title>
</head>
<body>
<?php

$host = "localhost";
$user = "root";
$pass = null;

// ========================================
$dbhost = @mysql_connect($host, $user, $pass) or die("Unable to connect to server");

@mysql_select_db("divebay") or die("Unable to select database");

$var = "scuba";
$query = trim($var);

if(!isset($query)){
    echo "Your search was invalid";
    exit;
} //line 18

$sql = "SELECT * FROM auction WHERE name LIKE '%" . $query . "%'";

$result = mysql_query($sql);
$numrows = mysql_num_rows($result);
mysql_close($dbhost);

if($numrows == 0){
    echo "Sorry, your search did not return any results";
}
else{
?>
<table style='width = 800px;'>
<?php 
    $i = 0;

    while($i < $numrows){
    $row = mysql_fetch_array($result);

    $ID = $row['ID'];
    $name = $row['name'];
    $owner = $row['owner'];
    $holder = $row['holder'];
    $start = $row['sprice'];
    $current = $row['cprice'];
    $instant = $row['iprice'];
    $inc = $row['incprice'];
    $image = $row['img'];
    $time = $row['stime'];
    $length = $row['duration'];
?>
<tr style ="height: 200px;">
<td style ="width: 200px;"></td>
<td style ="width: 300px;">
    <div style ="width: 180px"><?php echo $name; ?></div>
    <div><?php echo $owner; ?></div>
</td>
<td style="width: 200px;">
    <div style="height: 100px;"><?php echo $current; ?></div>
    <div style="height: 50px;"><?php echo $instant; ?></div>
    <div><?php echo $inc; ?></div>
</td>
<td><?php echo $time; ?></td>
</tr>
<?php
      i++;
    } //end of while
} //end of else
?>
<tr>
    <td colspan="4">Displaying <?php echo $numrows; ?> results</td>
</tr>
</table>
</html>

And also consider preventing SQL Injection too: http://bobby-tables.com/ 并且还考虑防止SQL注入: http//bobby-tables.com/

I think you would be much better off if you separated your php and html into separate files. 如果你将你的php和html分成单独的文件,我想你会好得多。 You seem to be losing track of your opening " and closing ". 你似乎正在失去你的开场“和结束”。 If you want your 如果你想要你的

<tr> Displaying $numrows results</tr>

at the bottom of your page, then take it out of the table. 在页面底部,然后将其从表格中取出。

I'd suggest simplifying your table a little bit, maybe taking the 'Displaying $numrows results' out of the table entirely. 我建议稍微简化你的表格,也许完全从表格中取出'显示$ numrows结果'。

The line '<tr> Displaying $numrows results</tr>' is not valid HTML. “<tr>显示$ numrows结果</ tr>”行是无效的HTML。 <tr> means 'Define a new table row', but it needs a <td> inside it to wrap the content. <tr>表示“定义一个新的表行”,但它内部需要一个<td>来包装内容。 Because most rows of your table contain several TD elements, whilst this row only contains one piece of information, you would need to tell that table cell to span multiple columns . 由于表的大多数行包含多个TD元素,而此行仅包含一条信息,因此您需要告诉该表单元格跨越多个列

A good way of debugging this sort of thing is to feed the generated HTML to http://validator.w3.org/ , or replace the $variables with sample data and feed the template code to a validator. 调试此类事情的一种好方法是将生成的HTML提供给http://validator.w3.org/ ,或者用示例数据替换$ variables并将模板代码提供给验证器。 This is usually a little frustrating at first (as it will force you to be exact about the HTML version you want to use, etc) but it is a good way of tracing problems. 这通常有点令人沮丧(因为它会强迫您准确了解您想要使用的HTML版本等),但这是一种跟踪问题的好方法。 If you feed the following HTML snippet into the W3 validator, it will give you some useful feedback: 如果您将以下HTML代码段提供给W3验证程序,它将为您提供一些有用的反馈:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>A</title></head>
<body>

<table><tr><td>aaa</td><td>bbb</td></tr>
<tr><td>cccc</td><td>dddd</td></tr>
<tr>Test</tr>
</table>
</body>
</html>

The validator tells you that: Line 7, Column 5: character data is not allowed here 验证器告诉您:第7行,第5列:此处不允许使用字符数据

<tr>Test</tr>

In other words, the TR element should not directly contain text. 换句话说,TR元素不应直接包含文本。

It also says that: Line 7, Column 13: end tag for "tr" which is not finished 它还说:第7行,第13列:“tr”的结束标记未完成

<tr>Test</tr>

"Most likely, you nested tags and closed them in the wrong order... Another possibility is that you used an element which requires a child element that you did not include. Hence the parent element is "not finished", not complete. For instance, in HTML the <head> element must contain a <title> child element, lists require appropriate list items (<ul> and <ol> require <li> ...), and so on." “最有可能的是,你嵌套标签并以错误的顺序关闭它们......另一种可能性是你使用的元素需要你没有包含的子元素。因此父元素是”未完成“,不完整。例如,在HTML中,<head>元素必须包含<title>子元素,列表需要适当的列表项(<ul>和<ol> require <li> ...),依此类推。

Once you have the static HTML looking, and validating, the way you want, you can then add the PHP loops back in, but this time you can compare the script output to your working HTML example. 一旦你想要静态HTML查找和验证,你就可以重新添加PHP循环,但这次你可以将脚本输出与工作的HTML示例进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM