Categories > TinyButStrong general >

MergeBlock and in_array()

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Trent
Date: 2008-02-23
Time: 05:26

MergeBlock and in_array()

I have a small shop system set up where the users can purchase different items at different locations on the site.  Some shops may contain all items, and some shops may only contain certain items.  The shop location that each item is a available in is contained within the location field of the items table.  Now if each item were available in only 1 location, the statement below would work perfectly.  However, I want the items available in multiple locations that I specify.

$TBS->MergeBlock('items','tbssql','SELECT * FROM items where type_id = 1 and location = 8');


Now, what I'm trying to do is this:
1) The location field in the database will contain an array of different locations
For instance, item_1 might have (1,2,5,8) and item_2 might have (2,4,8) and item_3 might have (1,2,8)
2) Also, some location fields might contain (0) as a default for being available in ALL locations

SO what I'm asking is how to re-write the above MergeBlock statement to search for the appropriate location_id with in the array that will be contained in each item data file.

I can break it down in pure php with in_array(), but I can't figure out how to do this with the MergeBlock statement.  Any help, or just pointing me in the right direction would be great.
By: TomH
Date: 2008-02-23
Time: 14:36

Re: MergeBlock and in_array()

hi

There are several ways to accomplish that - but I prefer the TBS "ondata function" way

Look in the "TBS Examples" under "Event functions"

The example shows a function that displays all the rows returned with an added field created in the function() -- but I often process arrays myself, and create returns only when an if() condition passes.

If you try it and need any troubleshooting on your code - just stop back here.

Hope that helps,
TomH
By: TomH
Date: 2008-02-23
Time: 14:56

Re: MergeBlock and in_array()

Trent,

I should have mentioned... if you're building a real application then you should consider making the db engine do the work for you.

For example, MySQL has data types that enable you to query among multiple values directly (SET and ENUM)
SELECT * FROM items where type_id = 1 and location LIKE '%8%'

It might be owrth the time it takes to restructure the  table specification now and then alleviate all of the extra PHP function writing in the future.

By: Trent
Date: 2008-02-24
Time: 04:13

Re: MergeBlock and in_array()

After looking into ENUM it's has a lot of potential for a few other areas in my application I'm working on right now, but because of the constant changing of the values to the location field, I'm going to try my hand at the ondata function.

Thanks for all your help and excellent suggestions Tom.