Jump to content

[SOLVED] searching using an array


mryno

Recommended Posts

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. :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 */
	}
}
}

?>

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.