Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Liam Maclachlan
Liam Maclachlan
22,805 Points

How resource heavy is it to creat a MDimensional array using a series of If and while loops?

I'm working on a plugin and pulling in a load of information from a table.

The information is posted to the php application and is then split in to 3 layers:

1 - products

2 - catagories

3 - publishers

Then arrays are then nested in reverse order:

top - Publishers

    mid - categories

        last - products 

My questions are:

1 - Is there a more effective way of doing this?

2 - How resource heavy IS this, considering it will be used my a limited amount of users

I understand that it may not pe too problematic as an internal applciation with only a few users but I would liek to know how to find a more friendly approach if the application was used by a much wider audience. (1000's of uesrs per day, not 2 users per week)

2 Answers

Tom Sager
Tom Sager
18,987 Points

Please define your data flow with more detail.

Do you mean that your PHP app is selecting information from a database table? And that info is being loaded by a web client and displayed to the user? And the client is then posting the data back to the server? In which case maybe posting back some metadata, instead of the whole data set, might be an option.

Or by 'posted' do you mean the client is doing a GET from the server? In which case doing the layering in the database is probably more efficient, and lot less load on the client. Databases are engineered for work like this; web browsers are not.

Liam Maclachlan
Liam Maclachlan
22,805 Points

Hi tom. The data flow starts on an HTML table and is posted to the database. The database then compiles the array and stores it once serialised.

It is then pulled back and parsed In a few different loops to display the data. I haven't got got as far as editing the data yet. Just filling in the form and trying the a store it a storey the moment

Here Is a link to the UI of the plug in on my codepen

Tom Sager
Tom Sager
18,987 Points

Liam --

I do not have any good answers for you.

  1. The snippet you posted does not include any sample data, so I can't quite tell what you are trying to accomplish here.

  2. The form declaration and submit button are missing, so I cannot see the form submit process. In fact, I don't see any interactions with the server, only client side code.

  3. I found your 'aliasing' of the selectors (lines 35 - 54 of the JS file) to be problematic. For instance, the HTML defines a '#categories-check' div, but this same div is aliased to '#checkBoxID" in your jQuery code. You have to be very familiar with the code to recognize (and remember) that these are the same thing. Much worse, debuggers cannot follow these relationships, so they show that the HTML div has no event listeners - making it almost impossible to debug (I tried Chrome and Firefox). I suggest that you pick one descriptive ID and stick with it.

  4. As a general impression, it seems like you are expecting a lot of edits to be done in the browser, and the whole data set is posted to the server at the end. You might consider making a short post every time the user adds a new piece of information, e.g. Add Category or Add Product. This would probably generate more web traffic, but would also keep tasks such as data validation a lot simpler. For example, I can add a new category with no name or description, and then start adding products to it. This would presumably be rejected on the server side, but then what does the server do with all the new products that do not have a category?

I hope this helps!

Tom

Liam Maclachlan
Liam Maclachlan
22,805 Points

No problem. Thanks for your help anyway, man.