子主题中的自定义搜索查询数据库

时间:2018-09-13 作者:mastura

我试图在wordpress子主题中显示数据库中搜索到的数据。

它可以在localhost上运行,但当我仅在wordpress中应用代码时

警告:在C:\\xampp\\htdocs\\testsite\\wp content\\themes\\twentyeven child\\custom search页中为foreach()提供的参数无效。php第76行。

请帮我解决这个问题。我已经研究了一个星期来解决这个问题。

<form name="frmSearch" method="post" action="">
        <div class="search-box">
        <p><input type="text" placeholder="Batch No" name="search[batch_no]"
     class="demoInputBox" value="<?php echo $batch_no; ?>"  />
            <br/>
            <input type="text" placeholder="RFID Chip No"
    name="search[rfid_chip_no]" class="demoInputBox" value="<?php echo
    $rfid_chip_no; ?>"  />
            <input type="submit" name="go" class="btnSearch" value="Search">
            <input type="reset" class="btnSearch" value="Reset"
  onclick="window.location=\'\'"></p>
            </div>
        </form>
以下是代码搜索查询和显示数据代码:

<?php
    function perpage($count, $per_page = \'10\',$href) {
        $output = \'\';
        $paging_id = "link_perpage_box";
        if(!isset($_POST["page"])) $_POST["page"] = 1;
        if($per_page != 0)
        $pages  = ceil($count/$per_page);
        if($pages>1) {

            if(($_POST["page"]-3)>0) {
                if($_POST["page"] == 1)
                    $output = $output . \'<span id=1 class="current-page">1</span>\';
                else                
                    $output = $output . \'<input type="submit" name="page" class="perpage-link" value="1" />\';
            }
            if(($_POST["page"]-3)>1) {
                    $output = $output . \'...\';
            }

            for($i=($_POST["page"]-2); $i<=($_POST["page"]+2); $i++)    {
                if($i<1) continue;
                if($i>$pages) break;
                if($_POST["page"] == $i)
                    $output = $output . \'<span id=\'.$i.\' class="current-page" >\'.$i.\'</span>\';
                else                
                    $output = $output . \'<input type="submit" name="page" class="perpage-link" value="\' . $i . \'" />\';
            }

            if(($pages-($_POST["page"]+2))>1) {
                $output = $output . \'...\';
            }
            if(($pages-($_POST["page"]+2))>0) {
                if($_POST["page"] == $pages)
                    $output = $output . \'<span id=\' . ($pages) .\' class="current-page">\' . ($pages) .\'</span>\';
                else                
                    $output = $output . \'<input type="submit" name="page" class="perpage-link" value="\' . $pages . \'" />\';
            }

        }
        return $output;
    }

    function showperpage($sql, $per_page = 10, $href) {
        $result  = mysql_query($sql);
        $count   = mysql_num_rows($result);
        $perpage = perpage($count, $per_page,$href);
        return $perpage;
    }
?>
<div class="wrap">
    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">
 <form name="frmSearch" method="post" action="">
            <div class="search-box">
            <p><input type="text" placeholder="Batch No" name="search[batch_no]" class="demoInputBox" value="<?php echo $batch_no; ?>"  />
                <br/>
                <input type="text" placeholder="RFID Chip No" name="search[rfid_chip_no]" class="demoInputBox" value="<?php echo $rfid_chip_no; ?>" />
                <input type="submit" name="go" class="btnSearch" value="Search">
                <input type="reset" class="btnSearch" value="Reset" onclick="window.location=\'\'"></p>
                </div>
            </form> 

            <h2>Search Result</h2>

            <?php

    $category = "";
    $code = "";

    $queryCondition = "";
    if(!empty($_POST["search"])) {
        foreach($_POST["search"] as $k=>$v){
            if(!empty($v)) {

                $queryCases = array("category","code");
                if(in_array($k,$queryCases)) {
                    if(!empty($queryCondition)) {
                        $queryCondition .= " AND ";
                    } else {
                        $queryCondition .= " WHERE ";
                    }
                }
                switch($k) {
                    case "category":
                        $category = $v;
                        $queryCondition .= "category LIKE \'" . $v . "%\'";
                        break;
                    case "code":
                        $code = $v;
                        $queryCondition .= "code LIKE \'" . $v . "%\'";
                        break;
                }
            }
        }
    }
    $orderby = " ORDER BY id desc"; 
    $sql = "SELECT * FROM toy " . $queryCondition;
    $href = \'index.php\';                    

    $perPage = 2; 
    $page = 1;
    if(isset($_POST[\'page\'])){
        $page = $_POST[\'page\'];
    }
    $start = ($page-1)*$perPage;
    if($start < 0) $start = 0;

    $query =  $sql . $orderby .  " limit " . $start . "," . $perPage; 
    $result = $db_handle->runQuery($query);

    if(!empty($result)) {
        $result["perpage"] = showperpage($sql, $perPage, $href);
    }
?>
<html>

    <head>
    <title>Search Result</title>
    </head><?php 
    if (empty($result)) {
   echo "<p>No results matched. Please try again..</p>\\n";
} else {
?>

<table cellpadding="10" cellspacing="1">
                <?php
                        foreach($result as $k=>$v) {
                        if(is_numeric($k)) {
                    ?>
        <thead>
                    <tr>
          <th><strong>Name</strong></th>
                        <td><?php echo $result[$k]["name"]; ?></td>
            </tr>
            <tr>
          <th><strong>Code</strong></th> 
                 <td><?php echo $result[$k]["code"]; ?></td>
            </tr>
            <tr>
          <th><strong>Category</strong></th>
                <td><?php echo $result[$k]["category"]; ?></td>
            </tr>
                </thead>
                <tbody>

          <tr>


                    <?php
                        }
                    }
                    if(isset($result["perpage"])) {
                    ?>
                    <tr>
                    <td colspan="6" align=right> <?php echo $result["perpage"]; ?></td>
                    </tr>
                    <?php }
    }?>
                <tbody>
            </table>

1 个回复
SO网友:majick

文本输入名称与正在循环的$\\u POST键不匹配。在输入名称属性中使用[]是为了处理多个输入,而不是多个键。

尝试替换name="search[batch_no]"name="search[rfid_chip_no]" 使用simplyname="search_batch_no"name="search_rfid_chip_no" 和循环$_POST 而不是$_POST["search"].

此外,您还需要检查条件是否匹配,因为codecategory 在输入处理中匹配,没有batch_norfid_chip_no, 因此,我不知道它如何“在本地工作”,除非它只是在本地选择所有记录,因为查询条件被忽略。

结束

相关推荐

Wordpress Database Cleanup

重建WordPress数据库和更改网站的目录结构有多困难?我继承了两个网站的维护工作,我很确定目录结构设置不正确,数据库一团糟(两个网站有6个数据库?)。我是WordPress的新手,我怀疑现在使用现有网站重建网站会更容易。丢失数据没什么大不了的,网站也没什么特别的。该网站运行正常,但维护起来越来越困难。这是一个合理的计划吗?