Jump to content

Search using text not integer


aeafisme23

Recommended Posts

This is a snippet of code from the search page (http://www.thegreatestsave.org/sms/view_records_contact_log.php) i created. In the search box you can enter any value you wish because it pulls all the results regardless just put in A if you want and it will say it's looking for 0 since the code is looking for an integer, if you put in an integer such as 3 it will say it's looking for 3 so i know the mechanics are working but not to the specifics i need. Please do not delete the records as it is working :) I have rigged it to pull results by erasing part of this:

 

$searchcode_query = "SELECT * FROM contact_log WHERE station_id = '".$codes."'";

to 

$searchcode_query = "SELECT * FROM contact_log ";

 

I understand my problem lies within the station_id = '".$codes . This script is being modified from another project i did using AREACODES such as 220 , 574 , etc. So the next line of code that i believe is the problem is the defining of $codes as it is converting or looking for an integer value (a number like 220), so when i put in text like (WUSA) it will say it searched for 0 'zero' because it's looking for an integer.

 


foreach($keywords as $k => $v) 
$keywords[$k] = (int)$v;	
$codes = implode(",", $keywords);

 

I am trying to figure out how i can modify this code to look for text and not an integer and still keep my code structure if at all possible. Any help would greatly be appreciated. Below is just more code in case you need it. if there is more code you need let me know  Thanks again.

 

 


// connect to the mysql database server.
$connect = mysql_connect($dbhost, $dbusername, $dbuserpass) or die("Unable to connect to SQL server..<br />".mysql_error());
$selectDB = mysql_select_db($dbname) or die("Unable to select database.<br />".mysql_error());

//trim whitespace from variable
$searchcode = preg_replace('/\s+/', ' ', trim($_GET['search']));

//seperate multiple keywords into array space delimited
$keywords = array_diff(explode(" ", $searchcode), array(""));

if( !empty($searchcode) ) {
foreach($keywords as $k => $v) 
$keywords[$k] = (int)$v;	
$codes = implode(",", $keywords);

$searchcode_query = "SELECT * FROM contact_log";

//Store the results in a variable or die if query fails
$result = mysql_query($searchcode_query) or die("Error executing MySQL query<br />".mysql_error());

$count = mysql_num_rows($result);
}

Link to comment
https://forums.phpfreaks.com/topic/91893-search-using-text-not-integer/
Share on other sites

Awesome thanks, one quick question though, what do i do about this section:

 

foreach($keywords as $k => $v) 
$keywords[$k] = (int)$v;	
$codes = implode(",", $keywords); 

Is it something i should just eliminate or keep in? I was thinking that somehow i had to define the (int) to text so when it does show my results and what i looked for it will not check for an integer and return an integer value but it will return a text value. Does this make sense :D?

If $keywords is an array of strings then you want to drop the (int).  The (int) converts the value that follow it into an integer.

 

So here's a couple questions for you.  How do you convert the string '54' to an int?  Just drop the quotes, right?  Easy enough.

 

How do you convert the string 'WUSA' into an integer?

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.