Jump to content

Not showing array, just "Array"


Recommended Posts

elseif(isset($_GET['search'])){
$search=$_POST['search'];
$search=str_replace(" ", "+", $search);
header("Location: ./index.php?q=".$search);
}
elseif(isset($_GET['q'])){
$search=$_GET['q'];
$keywords=explode("+",$search);
$sql10000="SELECT product_id FROM $tbl_name2 WHERE ";
while($query10000=each($keywords)){
$sql10000.="keyword LIKE '%".$query10000."%' OR";
}
$sql10000=substr($sql10000,0,(strLen($sql10000)-3));
$content=$sql10000;

 

That is returning this:

 

SELECT product_id FROM keywords WHERE keyword LIKE %Array%

 

For some reason it's showing "Array", it should be looping through the keywords array.

Link to comment
https://forums.phpfreaks.com/topic/236575-not-showing-array-just-array/
Share on other sites

Try this.  I haven't executed it, but it should work... I think. :P

 

<?php

elseif(isset($_GET['search'])) {
    $search = $_POST['search'];
    $search = str_replace(" ", "+", $search);
    header("Location: ./index.php?q=".$search);
}

elseif(isset($_GET['q'])) {
    $search = $_GET['q'];
    $keywords = explode("+", $search);
    $sql10000 = "SELECT product_id FROM $tbl_name2 WHERE ";
}

foreach($keywords as $query10000) {
    $sql10000 .= "keyword LIKE '%".$query10000."%' OR ";
}

$sql10000 = substr($sql10000,0,(strLen($sql10000)-3));
$content = $sql10000;

?>

What is the value of the variable $keywords?

 

Post the result of

<?php
echo '<pre>' . print_r($keywords,true) . '</pre>';
?>

 

If $keywords is an array, you can create the query like this:

<?php
$q = "SELECT product_id FROM $tbl_name2 WHERE keyword like '%" . implode("%' or keyword like '%",$keywords) . "%'";
?>

 

Ken

Try this.  I haven't executed it, but it should work... I think. :P

 

<?php

elseif(isset($_GET['search'])) {
    $search = $_POST['search'];
    $search = str_replace(" ", "+", $search);
    header("Location: ./index.php?q=".$search);
}

elseif(isset($_GET['q'])) {
    $search = $_GET['q'];
    $keywords = explode("+", $search);
    $sql10000 = "SELECT product_id FROM $tbl_name2 WHERE ";
}

foreach($keywords as $query10000) {
    $sql10000 .= "keyword LIKE '%".$query10000."%' OR ";
}

$sql10000 = substr($sql10000,0,(strLen($sql10000)-3));
$content = $sql10000;

?>

 

Alright, that got rid of the "Array" and placed it with the data, but it's now come up as

 

'%test test2%'... Don't get why, I've had this problem all day with it not add the closing % after test with the closing single quote and then opening % and single quote for test2.

What is the value of the variable $keywords?

 

Post the result of

<?php
echo '<pre>' . print_r($keywords,true) . '</pre>';
?>

 

If $keywords is an array, you can create the query like this:

<?php
$q = "SELECT product_id FROM $tbl_name2 WHERE keyword like '%" . implode("%' or keyword like '%",$keywords) . "%'";
?>

 

Ken

tried this and get the '%test test2 %' problem as well. The URL of the page has q=test+test2. Meaning it should be pulled into the explode and be placed into an array of $keywords("test","test2")... if I'm not mistaken.

 

 

echo '<pre>' . print_r($keywords,true) . '</pre>';

That returns:

 

Array

(

    [0] => test test2

)

 

On my computer running Windows XP with FireFox

 

on my windows 7 comp with FireFox it returns something different (weird since the computer shouldn't be doing any processing):

 

Array

(

    [0] => test

)

The "+" character has special meaning when used in a URL. It will replaced by a space. Pick another character for your deliminator, such as a "~" or "`" or "|" or any character that you're not expecting to be in a keyword.

 

Ken

 

The reason I was trying to use "+" is because I see it all the time in search engine URLs, and a lot of other stores.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.