4 Replies - 218 Views - Last Post: 10 April 2019 - 01:19 PM Rate Topic: -----

#1 TonyR   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-April 19

Problem with form output

Posted 09 April 2019 - 11:46 AM

Hi

I am having a problem with my form output. No matter what I search I get "No matching records". Even if I am searching words that I know should return a result. Any help would be much appreciated.

Tony

<?php                                                
 
define("PERPAGE", 15);                                                // number of records on each page
 
/**************************************************************************************
* function to output page selection buttons
*                    
* @param int $total   total records
* @param int $page    current page number
* @return string      selection buttons html
*/
function page_selector($total, $page)
{
    if ($total==0) {
        return '';
    }
    $kPages = ceil($total/PERPAGE);
    $filler = '&nbsp;&middot;&nbsp;&middot;&nbsp;&middot;&nbsp;';
    $lim1 = max(1, $page-2);
    $lim2 = min($kPages, $page+3);
    $p = $page==1 ? 1 : $page - 1;
    $n = $page== $kPages ? $kPages : $page + 1;;
    $out = "$kPages page" . ($kPages==1 ? '' : 's') . " &emsp;";
    if ($kPages==1) {
        return $out;
    }
    $out .= ($page > 1) ? "<div class='pagipage' data-pn='$p'>Prev</div>&ensp;" : '';
    if ($page > 4) {
        $out .= "<div class='pagipage' data-pn='1'>1</div> $filler";
    }
    elseif ($page==4) {
        $out .= "<div class='pagipage' data-pn='1'>1</div>";
    }
    for ($i=$lim1; $i<=$lim2; $i++) {
        if ($page==$i)
            $out .= "<div class='pagicurrent'>$i</div>";
        else
            $out .= "<div class='pagipage' data-pn='$i'>$i</div>";
    }
    if ($page < $kPages-3) {
        $out .= "$filler <div class='pagipage' data-pn='$kPages'>$kPages</div>";
    }
    $out .= $page < $kPages ? "&ensp;<div class='pagipage' data-pn='$n'>Next</div>" : '';
    return $out;
}
 
/***********************************************
**  SEARCH FOR MATCHING TVS
************************************************/
$showResults = 0;
$search = '';
if (isset($_GET['tag'])) {
    $showResults = 1;
    $search = $_GET['tag'];
    $srch = array_unique(explode(' ', trim($_GET['tag'])));
    foreach ($srch as $t) {
        $repl[] = "<span class='hilite'>$t</span>";
        $placeholders[] = '?';
        $params[] = $t;
    }
    $params[] = count($srch);
    //
    // FINDTOTAL RECORDS IN SEARCH RESULTS                                      
    //
    $res = $db->prepare("SELECT COUNT(*) as tot
                       FROM  television
                       WHERE MATCH(title,description,keywords) AGAINST(? IN BOOLEAN MODE)
                       ");
    $res->execute($params);
    $total = $res->fetchColumn();
 
    $page = $_GET['page'] ?? 1;
   
   
    $params[] = ($page-1)*PERPAGE;    // append parameters offset
    $params[] = PERPAGE;              // and number of records for limit clause
    //
    // GET A PAGEFUL OF RECORDS
    //
   $sql = "SELECT
                  id
                , title
                , description
                , keywords
           FROM television
           WHERE MATCH(title,description,keywords) AGAINST(? IN BOOLEAN MODE)
           ORDER BY TITLE
           LIMIT ?,? ";
    $stmt = $db->prepare($sql);
    $stmt->execute($params);
   
    if ($stmt->rowCount()==0) {
        $results = "<h3>No matching records</h3>";
    } else {
        $results = "<tr><th>Product Id</th><th>Title</th><th>Description</th><th>Tags</th><th>Edit</th></tr>\n";
        foreach ($stmt as $rec) {
            $alltags = str_replace($srch, $repl, $rec['keywords']);
            $results .= "<tr><td>{$rec['id']}</td><td>{$rec['title']}</td><td>{$rec['description']}</td><td>$alltags</td>
           <td><a href='?action=edit&id={$rec['id']}'><img src='edit-icon.png' alt='edit'></a></td>
           </tr>\n";
        }
    }
}
?>
<div id='title'>Television Search</div>
<form id='form1'>
    <fieldset>
    <legend>Search for tags</legend>
        <input type='text' name='tag' size='50' value='<?=$search?>' placeholder='Search for...' >
        <input type="hidden" name="page" id="page" value="1">
        <input type="hidden" name="action" id="page" value="search">
        <input type="submit" name="btnSub" value="Search">
    </fieldset>
    <div id='formfoot'></div>
</form>
<?php if ($showResults) { ?>
         <div class="paginate_panel">
             <?=page_selector($total, $page)?>
         </div>
         <table border='1'>
             <?=$results?>
         </table>
<?php } ?>



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with form output

#2 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2836
  • View blog
  • Posts: 11,132
  • Joined: 03-December 12

Re: Problem with form output

Posted 09 April 2019 - 11:55 AM

Have you run the query directly to see what is returned?
Was This Post Helpful? 0
  • +
  • -

#3 TonyR   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-April 19

Re: Problem with form output

Posted 09 April 2019 - 12:13 PM

Thank you astonecipher for your reply. Excuse my ignorance but do you mean this code:

SELECT id , title , description , keywords FROM television

If you do, then it does return the results correctly in phpmyadmin when I type this in SQL.
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2836
  • View blog
  • Posts: 11,132
  • Joined: 03-December 12

Re: Problem with form output

Posted 09 April 2019 - 12:27 PM

Quote

For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement. Instead, use PDO::query() to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned. Your application can then perform the correct action.

Was This Post Helpful? 1
  • +
  • -

#5 TonyR   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-April 19

Re: Problem with form output

Posted 10 April 2019 - 01:19 PM

Thank you for the reply. With your assistance I have managed to get it working.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1