Jump to content

Could This Be Rewritten To A More Compact


denny911
 Share

Recommended Posts

Hi,could (something like) this be shortened or in any other way made more efficient:(not the real code, just an idea)

$string = "something,stuff,thing,whatever";$exploded = explode(',',$string);$item1 = $exploded[0];$item2 = $exploded[1];$item3 = $exploded[2];$item4 = $exploded[3];$sql = mysql_query("SELECT * FROM someTbl WHERE sth LIKE '%$item1%'OR sth LIKE '%$item2%'OR sth LIKE '%$item3%'OR sth LIKE '%$item4%' ";....

since this is only a presentation, the code probably has errors or something,but i'm sure you have a basic idea what i'm trying to achieve.but i think there might be better way for doing this, since i plan to be adding morewords (items) in the string later onso, maybe using an array? please post, thanks

Edited by denny911
Link to comment
Share on other sites

$string = "something,stuff,thing,whatever";$exploded = explode(',',$string);$sql = "SELECT * FROM someTbl WHERE ";$where = '';foreach ($exploded as $chunk){  if ($where != '') $where .= ' OR ';  $where .= "sth LIKE '%{$chunk}%'";}$result = mysql_query($sql . $where);

Link to comment
Share on other sites

$string = "something,stuff,thing,whatever";$exploded = explode(',',$string);$sql = "SELECT * FROM someTbl WHERE ";$where = '';foreach ($exploded as $chunk){  if ($where != '') $where .= ' OR ';  $where .= "sth LIKE '%{$chunk}%'";}$result = mysql_query($sql . $where);

this is great, but i decided to change the approach. i wont use sql query, i'll just utilize $_get stringso, i managed to do this:
$string = $_GET['string'];	if($string) {		$haystack = $string;		$needle = 'washington';				$pos = strpos($haystack,$needle);		if($pos === false) {		 // string needle NOT found in haystack		 print "string not found";		}		else {		// string needle found in haystack		print "string found!";		}

now, this needs some tweaking$needle should be an array of values (let's say: washington, chicago, dallas, ohio)and IF ANY OF THEM is found in the $haystack, execute the codeany suggestions? thanksP.S. One more VERY IMPORTANT thing: this string comparison should be CASE INSENSITIVE,or $haystack string to be lowercased (if this is a word? :) )

Link to comment
Share on other sites

if (isset($_GET['string']) && !empty($_GET['string'])) {	$needles = array('washington', 'chicago');//Add the needles up	$haystack = strtolower($_GET['string']);	foreach($needles as $needle) {		if (strpos($haystack, $needle) === false) {			//String needle NOT found in haystack			echo 'string not found';		}else {			//String needle found in haystack			echo 'string found!';			//Because you want this executed once after there's any match, we should terminate the loop now.			//If you want this executed on every match, remove the break.			break;		}	}}

You should really learn about foreach, and other loops, as they seem to be the thing you're missing.

Edited by boen_robot
Link to comment
Share on other sites

Thank you very much!How can I use an array (located in lets say config file) in multiple functions in my functions.php file.like this:

// config file$keywords = array('chicago', 'dallas', 'washington');

and usage in function file:

// the array with the same values like the above should go here, but i need it in form of a variable pulled from config file// so similarly i can simply use it in another function$needles = array(); // need values from keywords array located in config file

hope you understand what i wantEDIT: the problem is that i cant even print that external variable ($keyword) anywhere in my function.if i try to print it outside a function, it is printed fine.can this be resolved somehow?

Edited by denny911
Link to comment
Share on other sites

There are two ways:1. Ideally, pass the array as an argument to the function, i.e.

//The following could be in config.php or whatever your config file name is$keywords = array('chicago', 'dallas', 'washington');//The following could be in functions.phpfunction myFuction($needles) {//That code from before}//The following is in your main script, where you call the functionmyFunction($keywords);

2. Import the global variable's value with the global keyword, like:

function myFunction() {global $keywords;//That code from before}

This is not exactly a good practice though, especially if you also create those functions to be used by other people or for other projects of yours. Besides, you'd have to do one of those for each global variable you want to use. If you really need that kind of power and flexibility, consider investigating how classes work. With them, you could have a "property" containing all the stuff from $keywords, and (optionally), you may even make sure that only valid data gets into that array. Therefore, your functions (or "methods" as they are called in classes) will be sure to work properly.

Edited by boen_robot
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...