mryno Posted February 13, 2009 Share Posted February 13, 2009 I have an array of possible terms that could be in a URL: $results[0][id] = 'test1' $results[1][id] = 'test2' $results[2][id] = 'test3' Referring URL: www.mysite.com/something/test1/index.php I am looking for an alternative to foreach'ing through them and doing some kind of while so that I don't have to go through the entire array which could be quite long especially if the first term is a match...don't want to go through all the rest if I don't have to. **More Than You May Want To Know** I am basically checking the URL for known identifiers that I have stored in the DB that will identify a URL as belonging to a certain client. Given the project, this is just the way we have set up to serve multiple clients based on this specific project. There are other factors that have made it so that the URL identifies the client, but we won't go into that. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 13, 2009 Share Posted February 13, 2009 im not 100% sure, but it sounds to me that in_array is the answer to what your looking for. Quote Link to comment Share on other sites More sharing options...
mryno Posted February 13, 2009 Author Share Posted February 13, 2009 in_array is actually the opposite of what I want to do. Sorry if my explanation wasn't very good. I want to while/foreach through an array of strings and see which array item matches part of the URL. My array contains the search terms and I am searching the URL to see which of those array items it contains...if that makes sense. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 13, 2009 Share Posted February 13, 2009 you probably will still use in_array, I am guessing you are doing this: $items = explode('/','www.mysite.com/something/test1/index.php'); foreach($items as $item){ if(in_array($item,$results){ // it is in the array! }else{ // it isn't in the array } } Quote Link to comment Share on other sites More sharing options...
corbin Posted February 13, 2009 Share Posted February 13, 2009 foreach($list as $key = > $elem) { if($elem = $input) { echo "$key matches." } } Why not store company names in a database or something though? Quote Link to comment Share on other sites More sharing options...
mryno Posted February 13, 2009 Author Share Posted February 13, 2009 The company names/ids are in a DB. I queried them out though. After querying for the IDs, I then search the URL to see which company ID is in the URL. I then use the rest of the record to identify everything about the company.... Quote Link to comment Share on other sites More sharing options...
printf Posted February 13, 2009 Share Posted February 13, 2009 Where do these come from? $results[0][id] = 'test1' $results[1][id] = 'test2' $results[2][id] = 'test3' Where does this url come from www.mysite.com/something/test1/index.php If you are getting the id(s) from the database, then run a query on your database using a REGEX that contains the url that has the items in its path that you want to match... If you want an example tell the EXACT parts of the url you want the search to match! Quote Link to comment Share on other sites More sharing options...
mryno Posted February 13, 2009 Author Share Posted February 13, 2009 Where do these come from? $results[0][id] = 'test1' $results[1][id] = 'test2' $results[2][id] = 'test3' Where does this url come from www.mysite.com/something/test1/index.php The id's are coming from a DB query. The URL is where the HTTP request is coming from. The problem is that the URL could be anything and the identifier could be anywhere in the URL, so I can't search the same place in the URL every time because one company may do it differently from another. I need to search every element of the URL. Basically, I HAVE to use my array of id's and with each one look for a match in the URL. There are reasons. I promise. But I would like to have it so I don't foreach through all of them. If I call 'break' does that exit the foreach? That may be all I need. In plain English here is the problem: In this requesting URL there is an ID. Here are all the possible active IDs from the database. Go through the IDs looking for a match against the URL until you find a match. When you find a match, don't finish looking through the other IDs. That match is the company making the request. I kinda want to do a foreach/while combination.... While I haven't found a match, keep foreach'ing. When a match is found finish. Quote Link to comment Share on other sites More sharing options...
printf Posted February 13, 2009 Share Posted February 13, 2009 So your search will include (www.mysite.com, something, test1, index.php) or just the path parts (something, test1)? I'm trying to show a better way to do it. Looping arrays is not the right way! Quote Link to comment Share on other sites More sharing options...
mryno Posted February 13, 2009 Author Share Posted February 13, 2009 Cool. Really I just need the something and test1. I don't need the URL or file name. Don't get me wrong. I totally appreciate your help. I just wanted to make sure you understood the problem. Thanks again! Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 13, 2009 Share Posted February 13, 2009 Is there something with what I did? Quote Link to comment Share on other sites More sharing options...
mryno Posted February 13, 2009 Author Share Posted February 13, 2009 Is there something with what I did? Actually I think that would work. Is that the kind of solution you were looking at printf? For some reason I had it in my head that I had to foreach through the db results because a client could have: www.mysite.com/test1/test2/index.php - triggering two IDs and giving me issues, but that will be a problem no matter which direction I am checking from. Let's see what printf has to say though... The short answer, yeah that will work. I am going to have to find another method for checking against two identifiers in the URL since that could hypothetically, but not practically, happen. Quote Link to comment Share on other sites More sharing options...
printf Posted February 13, 2009 Share Posted February 13, 2009 Example... <?php mysql_connect ( 'localhost', 'user', 'pass' ); mysql_select_db ( 'database' ); $url = 'http://www.mysite.com/something/test1/index.php'; $parts = parse_url ( $url ); if ( isset ( $parts['path'] ) ) { /* get all the word to search for clean them to make them database safe */ $parts = array_map ( 'mysql_real_escape_string', array_slice ( array_diff ( array_map ( 'trim', explode ( '/', $parts['path'] ) ), array ( '' ) ), 0, -1 ) ); /* remove this next line, it's only here to show you what we are searching for */ print_r ( $parts ); $result = mysql_query ( "SELECT * FROM your_table WHERE your_column_to_search REGEXP '[[:<:]]" . implode ( '|', $parts ) . "[[:>:]]';" ); if ( mysql_num_rows ( $result ) > 0 ) { while ( $row = mysql_fetch_assoc ( $result ) ) { /* do stuff */ } } } ?> Quote Link to comment Share on other sites More sharing options...
mryno Posted February 13, 2009 Author Share Posted February 13, 2009 I feel pretty good about that solution printf. Nicely done. I thought you weren't understanding the problem, but that seems to be a pretty good solution. I will definitely give it a whirl and see how it goes. Thanks again! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.