close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Comparing arrays

Thread began 3/18/2010 8:40 am by sam398359 | Last modified 3/19/2010 2:10 pm by Eric Mittman | 1713 views | 5 replies |

sam398359

Comparing arrays

So I finally got the multiple sections made from some dynamic dropdowns saved into the MySQL, i can implode and explode etc.

Now I want to search them.

I have a table of users with a col for "localStores" where i have a an array of the user's local stores stored as 234,135,10 etc

I then have a table of news articles, when the author writes the article they use a similar multiple select to choose the stores that story is applicable to.

When the user is logged in, I want to be able to pull each article that relates to their chosen stores.

Any ideas how to do this? I assumed arrays was the right way to go about this, but it is proving very difficult. I was hoping to be able to it MySQL side.

Thanks

Sam

Sign in to reply to this post

Eric Mittman

I think that you might be able to accomplish this with a special query using the IN keyword with another query like this:

SELECT * FROM articles
WHERE store_name IN (SELECT localStores FROM users where userID = colparam)

colparam defined as:
name: colparam
type: numeric
default value: -1
runtime value: <your userID session variable>



This query should select all from your articles table where the store name is found in a comma separated list of values. This list of values is created with the query that is inside the parenthesis. The query in here is selecting the localstores from your users table for the logged in user. Let me know if you have any further questions on this.

Sign in to reply to this post

sam398359

Interesting

$query_getUserNews = sprintf("SELECT * FROM news_articles WHERE news_articles.news_local IN (SELECT localRetailer FROM users WHERE userID = %s)", GetSQLValueString($userIDv_getUserNews, "int"));
$getUserNews = mysql_query($query_getUserNews, $datab2) or die(mysql_error());
$row_getUserNews = mysql_fetch_assoc($getUserNews);
$totalRows_getUserNews = mysql_num_rows($getUserNews);

Gives me an exact match.

i.e. the news article row has "246, 51, 122, 127, 29, 70" in its news_local field

it only come up as a result when the user row has the exact "246, 51, 122, 127, 29, 70" in its local_stores field.

I will continue to investigate the IN command

Sign in to reply to this post

Eric Mittman

Ok, that result makes a lot of sense. I was assuming that you would have only a single value in the news_local column of this table and that you would have a single record for each of the different articles. Is this how you have it setup? Post back with this and any other info you can come up with so we can get an idea of how this can be done.

Sign in to reply to this post

sam398359

it could be that I have designed the database incorrectly.

I wanted each news story to be assigned to multiple stores if necessary (some stores are chains). I then want the articles to be pulled depending on what store(s) the user has selected as "favourite".

So say a new article has its local field as "1,2,5" and the user has local stores "3,4,5" they would get that article.

i would use DISTINCT to remove duplicates should the user have "1,2,5,7"

I think if I explode the user's local stores into an array I could use a php loop and query the news articles each time. Though I feel this is really bad practice and should be done at a MySQL level.

Sign in to reply to this post

Eric Mittman

Your solution to getting the proper results by looping over the query and accumulating the values in an array seems like it would work.

If you wanted to store the multiple news article value on a per store basis that is fine, but to search them more effectively you should have your news articles in a table with a single record per article. Then rather than trying to store all of the values in single string to relate them you should have a lookup table. This table would have an id column then it would have an article id and a store id. You could then use this table to relate the stores to the articles and query by using a join.

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...