0

I am trying to display searched data from database in wordpress child theme.

It does work on localhost but when I apply the code in wordpress only

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\testsite\wp-content\themes\twentyseventeen-child\custom-search-page.php on line 76.

Please help me solve this. Its been one week i have been doing research to solve this.

<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>

here is the code search query and display data code:

<?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>
  • WP doesn't have a fishsearch table by default, are you trying to interact with a 3rd party plugin? How far does the code get before it fails? Your linked code also appears to be incomplete, it's not clear how it fits into your theme or which template file it's in – Tom J Nowell Sep 13 '18 at 1:43
  • ya i have to search for two matching field in a third party table. pastebin.com/1gGGc989.. here is the full code.. – mastura Sep 13 '18 at 1:52
0

The text input names don't match the $_POST keys you are looping. Using [] in the input name attribute is for handling multiple inputs, not multiple keys.

Try replacing name="search[batch_no]" and name="search[rfid_chip_no]" with simply name="search_batch_no" and name="search_rfid_chip_no" and looping $_POST instead of $_POST["search"].

Also you need to check the conditions match, as only code and category are matched in your input processing, there is no sign of batch_no or rfid_chip_no, so I have no idea how it could be 'working locally' unless it is simply selecting all records locally because the query condition is ignored.

  • it is not working also..how should i do it? i am helpless wasted days figuring this one..please help me... – mastura Sep 14 '18 at 2:24
  • post the updated code you have, it's a mystery to me how code and category match up to the search inputs? where are the values coming from/stored? – majick Sep 14 '18 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.