bonny
bonny

Reputation: 3247

Mysql result to limit output

I have a problem realizing some output to echo a list of results that comes from an Array. I would like to create a Live search engine that runs a query by the help of keyup-function by using AJAX.

Everything works fine when the output will be echoed for every match that is listed in the table. Now I would like to to combine all entries that are duplicates.

The code is like:

$search_term = $_POST['search_term'];

$where = "";

$search_term = preg_split('/[\s]+/', $search_term, -1, PREG_SPLIT_NO_EMPTY);
$total_search_terms = count($search_term);

$total_search_term = 0;

foreach ($search_term as $key=>$value) {

    $total_search_term = $total_search_term + 1;

    if ($total_search_term === 1){
        if (is_numeric($value) ){
            $where .= "(`a` LIKE '%$value%')";
        } else {
            $where .= "(`b` LIKE '%$value%')";
        }
    }else if ($total_search_term > 1){
        $where .= " AND ";

        if (is_numeric($value) ){
            $where .= "(`a` LIKE '%$value%')";
        } else {
            $where .= "(`b` LIKE '%$value%')";
        }
    }   
}
$duplicate = $db->query("SELECT a, b, COUNT(*) counter
                        FROM `table`
                        GROUP BY a, b
                        HAVING COUNT(*) > 1
                        ");

$check = $duplicate->fetch_assoc();
$query = $db->query("SELECT a, b FROM table WHERE $where");
$result = $query->num_rows;

if ($result !== 0 ){
    echo '<li id="hit">There are $result results!</li>';

    while ($row = $query->fetch_assoc() ) {

    echo '<li>', 
    $row["a"], 
    ' ', 
    $row["b"],  
    '</li>';
    }
} else {
        echo '<li id="hit">no result!</li>';
    }

To give an example of the output:

There are 3 results!
12345 New 
12345 New
56789 Chicago

And thats how it should be:

There are 3 results!
12345 New (2x)
56789 Chicago

So the table is:

a     |    b
12345   New 
12345   New
56789   Chicago

Thanks alot.

Upvotes: 0

Views: 185

Answers (2)

parascus
parascus

Reputation: 1249

I thought of something like this:

$query = $db->query("SELECT a, b, COUNT(*) counter FROM `table` WHERE ".$where." GROUP BY a, b");
$result = $query->num_rows;
if ($result !== 0 ){
  $resultSizeQuery = $db->query("SELECT COUNT(*) counter FROM `table` WHERE ".$where);
  $resultSize = $resultSizeQuery->fetch_assoc();
  echo '<li id="hit">There are '.$resultSize["counter"].' results!</li>';
  while ($row = $query->fetch_assoc() ) {
    echo '<li>'.$row["a"].' '.$row["b"];
    echo ($row["counter"] > 1 ? " (".$row["counter"]."x)" : "");
    echo '</li>';
  }
} else {
    echo '<li id="hit">no result!</li>';
}

Replacing all lines from "$duplicates = ..." to the end it should do it's work. Just give it a try because sometimes the step before the problem should be thought over.

Regards

parascus

Upvotes: 1

parascus
parascus

Reputation: 1249

first of all, your statement will return just 1 line with New York and the column counter will have 2. Chicago is missing because counter is just 1. So I think your result looks like: Ther are 1 results! 12345 New York

If you want to have "3 results" jsut do 2 queries, one for the number of rows (just leave out the group and having clause, also don't ask for a and b). So you get the output: There are 3 results!

Next you have to omit the having clause for getting all rows (also those without duplicates). You could write something like: echo ($row["a"].' '.$row["b"].($row["counter"] > 1 ? " (".$row["counter"]."x)" : "")

I hope this helps.

Regards

Parascus

Upvotes: 0

Related Questions