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
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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.