Forum Moderators: coopster

Message Too Old, No Replies

Search form Invalid arguement

Search form Invalid arguement, when a misspelt word is entered in search

         

ecnaralc

1:56 am on Apr 30, 2009 (gmt 0)

10+ Year Member



Hello.
I have a problem with my search form giving two warnings when a misspelt word is entered into the search string.
Can someone tell me why this would be... and please how to fix.. The Code works fine except when a single misspelt word is used in the search string

Thanks in advance
Wayne.

The Search Search Results

Warning: array_unique() [function.array-unique]: The argument should be an array in /hermes/web09d/b2861/pow.xchange/htdocs/search.php on line 175

Warning: Invalid argument supplied for foreach() in /hermes/web09d/b2861/pow.xchange/htdocs/search.php on line 177

"Search results for:druging"
Sorry, your search returned zero results

LINE 175 $tmparr = array_unique($adid_array);
LINE 176 $i=0;
LINE 177 foreach ($tmparr as $v) {

The Rest of the code - LINE 175 and 177 marked
<?php
$hostname_logon = "localhost" ;
$database_logon = "articles" ;
$username_logon = "root" ;
$password_logon = "" ;
$connections = mysql_connect($hostname_logon, $username_logon, $password_logon) or die ( "Unabale to connect to the database" );
mysql_select_db($database_logon) or die ( "Unable to select database!" );
$limit = 15;
$var = @$_GET['q'] ;
$trimmed = trim($var);
$trimmed_array = explode(" ",$trimmed);
if ($trimmed == "") {
$resultmsg = "<p>Search Error</p><p>Please enter a search...</p>" ;
}
if (!isset($var)){
$resultmsg = "<p>Search Error</p><p>We don't seem to have a search parameter! </p>" ;
}
foreach ($trimmed_array as $trimm){
$query = "select * from cmsarticles WHERE ID LIKE \"%$trimm%\" OR title LIKE \"%$trimm%\" OR tagline LIKE \"%$trimm%\" OR thearticle LIKE \"%$trimm%\" ORDER BY ID DESC" ;
$numresults=mysql_query ($query);
$row_num_links_main =mysql_num_rows ($numresults);
$s=0;
if (isset($_GET['s'])) $s = (int)$_GET['s'];

$query .= " LIMIT $s,$limit" ;
while($row=mysql_fetch_array($numresults)){
$adid_array[] = $row[ 'id' ];
}
} //end foreach

if($row_num_links_main == 0 && $row_set_num == 0){
$resultmsg = "<p>Search results for:" . $trimmed ."</p><p>Sorry, your search returned zero results</p>" ;
}
// LINE 175 is next line below
$tmparr = array_unique($adid_array);
$i=0;
// LINE 177 is next line below
foreach ($tmparr as $v) {
$newarr[$i] = $v;
$i++;
}

?></p>
<p>&nbsp;</p></td></tr>
<tr> <td valign="top" colspan="2">
<h4>
<?php
if( isset ($resultmsg)){
echo $resultmsg;
exit();
}else{
echo "Search results for: " . $var;
}

foreach($newarr as $value){
// EDIT HERE and specify your table and field names for the SQL query
$query_value = "SELECT * FROM cmsarticles WHERE ID = '$value'";
$num_value=mysql_query ($query_value);
$row_linkcat= mysql_fetch_array ($num_value);
$row_num_links= mysql_num_rows ($num_value);
$titlehigh = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'ID' ] );
$linkhigh = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'title' ] );
$linkdesc = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'tagline' ] );
$linklow = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'thearticle' ] );

foreach($trimmed_array as $trimm){
if($trimm != 'b' ){
$titlehigh = preg_replace( "'($trimm)'si" , "<b>\\1</b>" , $titlehigh);
$linkhigh = preg_replace( "'($trimm)'si" , "<b>\\1</b>" , $linkhigh);
$linkdesc = preg_replace( "'($trimm)'si" , "<b>\\1</b>" , $linkdesc);
$linklow = preg_replace( "'($trimm)'si" , "<b>\\1</b>" , $linklow);
}
//end highlight

?>
</h4>
</td></tr>
<tr>
<td valign="top" colspan="2"><?php
//BEGINNING OF 2ND SCRIPT
$finalresult = mysql_query($query) or die('Query failed: ' . mysql_error());
$numofrows = mysql_num_rows($finalresult);
for($i = 0; $i < $numofrows; $i++) {
$frow = mysql_fetch_array($finalresult); //get a row from our result set
if($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#F0F0F0\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"white\">\n";
}
echo '<p> <a href="view.php?id='.$frow['ID'].'">';
echo $frow['title'];
echo "</a> </p></TR>\n";
echo $frow['tagline'];
echo ' <a href="view.php?id='.$frow['ID'].'">more';
}
echo "</TABLE>\n";
//END OF 2ND SCRIPT
?></td>
</tr>
<tr>
<td valign="top" colspan="2">&nbsp;</td>
</tr>
<tr>
<td valign="top" colspan="2"><?php
} //end foreach $trimmed_array
if($row_num_links_main > $limit){
// next we need to do the links to other search result pages
if ($s>=1) { // do not display previous link if 's' is '0'
$prevs=($s-$limit);
echo "<div align='left'><a href='$PHP_SELF?s=$prevs&q=$var&catid=$catid'>Previous " .$limit. "</a></div>";
}
// check to see if last page
$slimit =$s+$limit;
if (!($slimit >= $row_num_links_main) && $row_num_links_main!=1) {
// not last page so display next link
$n=$s+$limit;
echo "<div align='right'><a href='$PHP_SELF?s=$n&q=$var&catid=$catid'>Next " .$limit. "</a></div>";
}
}
} //end foreach $newarr
?>

midtempo

12:36 pm on Apr 30, 2009 (gmt 0)

10+ Year Member



the reason is that this bit of the code

while($row=mysql_fetch_array($numresults)){
$adid_array[] = $row[ 'id' ];
}

only happens when there's a row returned from the db

which means that $adid_array is only being set IF you've results. no results = no array.

then, when you hit line 175, you're doing unique_arrays on something that doesn't exist.

there are a number of ways that you can deal with this, but my preference would be only do lines 175 onwards if $resultmsg doesn't exist.

midtempo

12:39 pm on Apr 30, 2009 (gmt 0)

10+ Year Member



oh, you can also set array variables at the top of your script so that issues like this don't arise

e.g. $adid_array = array();

that'll stop "The argument should be an array" error messages

ecnaralc

1:10 am on May 1, 2009 (gmt 0)

10+ Year Member



Hi!
Thanks for the replies..
Now I understand why the errors.

I am a bit thick, but it seems the solution is to place $adid_array = array(); at the top of script.

Can you please show me exactly where at the top of the script.
Regards
Wayne

midtempo

9:46 am on May 1, 2009 (gmt 0)

10+ Year Member



that's one of the solutions. it doesn't matter where you put it, provided it's above

while($row=mysql_fetch_array($numresults)){
$adid_array[] = $row[ 'id' ];
}

the other solution is to put just before line 175:

if ($resultmsg) {
echo $resultmsg;
} else {

and then, at the bottom of the page, below:

} //end foreach $newarr 

to put another }

that way, if there's an error message/no results, it'll print that. otherwise it'll run through the rest of your code.

ecnaralc

6:38 am on May 2, 2009 (gmt 0)

10+ Year Member



Hey Thanks dude..
The other solution is to put... line 175, worked just fine.
Thanks very much.
Wayne