我试图在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>