Languages :: PHP :: Design quesion - multidimension arrays or... ? |
|||
| By: mdouglas69 |
Date: 12/04/2003 00:00:00 |
Points: 75 | Status: Answered Quality : Excellent |
|
I'm at an impass on how to design a portion of an application I'm building. I have four variables, which are all related - Username, area topic, specific topic, topic value. Area topic is relatively static, which points to specific topic (also static), which points to a topic value. Topic value is unique to each username. All of this information is stored in a database, and I need to retrieve it and put it into a formatted table. Initially, I had intended to use a multidimensional array of $data[username_level][area_topic_level][specific_topic_level][topic_value_level]. However, I'm finding working with a multidimensional array very difficult. I've managed to get two dimensions working, but that took me far too long, and moving to the third dimension is beyond me at this point - I don't see how I'm going to get to the fourth in this lifetime. So, here's what I have so far: <?php $userResult = mysql_db_query($sqldb,"SELECT username FROM skills_users WHERE topic IS NOT NULL GROUP BY username") or die(mysql_error()); $userData = array(); while ($row = mysql_fetch_array($userResult)) { array_push($userData, $row["username"]); } //print_r($userData); $areaResult = mysql_db_query($sqldb,"SELECT area FROM skills_topics GROUP BY area ORDER BY area ASC") or die(mysql_error()); $areaData = array(); while ($row = mysql_fetch_array($areaResult)) { array_push($areaData, $row["area"]); } //print_r($areaData); $skill_data = array(); for ($i = 0; $i < count($userData); $i++) { for ($c = 0; $c < count($areaData); $c++) { $skill_data[$userData[$i]][$c] = $areaData[$c]; } } //print_r($skill_data); for ($i = 0; $i < count($userData); $i++) { for ($c = 0; $c < count($areaData); $c++) { $area = $skill_data[$i][$c]; $topicResult = mysql_db_query($sqldb,"SELECT topic FROM skills_topics WHERE area = \"".$area."\" ORDER BY topic ASC") or die(mysql_error()); $topicData = array(); while ($row = mysql_fetch_array($topicResult)) { array_push($topicData, $row["topic"]); } // print_r($topicData); for ($x = 0; $x < count($topicData); $x++) { $skill_data[$userData[$i]][$areaData[$c]][$x] = $topicData[$x]; } } } Note that the third dimension does not work as I have lost the integer key values to the first dimension when putting data into the second. I tried building the second dimension by using $array[][] but that just erased the values in my first dimension, as did looping through $array[$counter][]. Any help is greately appreciated, whether it be an alternative solution or an explanation as to what I'm doing wrong with multidimensional arrays. |
|||
| By: VGR | Date: 23/09/2004 01:32:00 | Type : Comment |
|
| I'lldefinitely search what's wrong with your multidimensional arrays 8-) because it's rather simple, if you don't use silly things like array_push() and the like leave me some time, 'cause tyour code looks messy and cryptic. So cryptic in fact that - it's a grande premiere for me on EE - I've to print it out to look at it 8-) |
|||
| By: VGR | Date: 23/09/2004 02:00:00 | Type : Comment |
|
| ok, show me exactly what you try to achieve, 'cause I'm kinda lost in your dimensions $skill_data for instance is not clear at all. give me samples of topicData, areaData, userData so that I may understand what you want in 3D in skillData |
|||
| By: mdouglas69 | Date: 23/09/2004 02:08:00 | Type : Comment |
|
| Well a username is somebody's name, obviously. Area data would be something like "hardware" , and a topic within hardware could be "sun ultrasparc." The fourth dimension is all numerical. |
|||
| By: VGR | Date: 23/09/2004 02:52:00 | Type : Comment |
|
| yes, but the way you construct $skliiData, it looks like everybody (userName) will have all areas, then all skills in each area, etc am I right ? This would be ENORMOUS |
|||
| By: mdouglas69 | Date: 23/09/2004 02:55:00 | Type : Comment |
|
| You are right, and yes, it will be enormous... but my other option is doing a ton of DB queries... I'd rather use memory which I have lots of, rather than slow the server down with tons of DB queries. |
|||
| By: VGR | Date: 23/09/2004 02:57:00 | Type : Comment |
|
| how I would do it myself : -build $userData, $areaData and $topicData the way you build them (simple while with array_push(), althought the []= operator would be as good and clearer) -then for each user, for each area, for each skills_topic in that area, build the fourth dimension (numerical, I don't know where this does come from, a user's profile I guess) I wouldn't do this that way. I would just put relevant skills IDs in the user's profile, then build skillData by looking anly those skliis in area_skills (this gives me area), then areas in areaData, and for only that user (userData unnecessary) just an idea. |
|||
| By: VGR | Date: 23/09/2004 03:01:00 | Type : Comment |
|
| and what if we started it all over thinkering ? tell me WHY and WHEN (each session? Each request? each posting? etc) you would like to build this array in memory, an for WHAT purpose. Perhaps we can optimize the whole process. It looks like things I've done previously. |
|||
| By: mdouglas69 | Date: 23/09/2004 03:03:00 | Type : Comment |
|
| That's fine, but you're still talking about three dimensions in the array (areaData, topicData, and topicValue). I haven't been able to build a usable 3 dimensional array yet. |
|||
| By: mdouglas69 | Date: 23/09/2004 03:08:00 | Type : Comment |
|
| The way I have it set now is that it would happen every time you accessed the page... I could easily modify the process such that the page was assembled statically whenever a value in the database was modified, though. As for why... I'm not exactly sure what you want me to tell you. I have four seperate pieces of information that I need to assemble into a two dimensional table... ------------------------------------------- Topics -> | Topic (Area) | Topic2 (Area2) | Username1 | Value1 | Value2 | Username2 | Value3 | Value4 | ------------------------------------------- |
|||
| By: VGR | Date: 23/09/2004 03:11:00 | Type : Comment |
|
| like a number of user contributions in each topic/area ? |
|||
| By: mdouglas69 | Date: 23/09/2004 03:15:00 | Type : Comment |
|
| Yes |
|||
| By: VGR | Date: 23/09/2004 03:20:00 | Type : Comment |
|
| hum. and to get the numerical value (4th dimension), you of course would need some DB access ? a lot of them, in fact, as you've to query for all topics of all areas. Right ? just tell me : this big array, is it available to the user being cconnected ? Is it used to see any other user's contributions in any area and any topic ? Some kind of big shared data for all users of the site ? I'm thinking of a session variable that you don't completely compute each time, but just update where necessary. This requires to be able to get the new quadruplets (user,topic,area,number) from the last time the user refreshed a page. It's feasible : I use this technique in my forum to show all user's status (+icons, time inactive, speed of line, etc) |
|||
| By: mdouglas69 | Date: 23/09/2004 04:05:00 | Type : Comment |
|
| Yes, the 4th dimension is in the database as well. And yes, as I said before, this is a lot of DB queries. I previously had this displayed in a different format and I was doing approximately 400db queries per page load. Don't worry about the session variable and such, I can take care of optimizing this code once it's complete, right now I need a way of actually getting these 4 variables from the database and displaying them in the table example I showed above. For informational purposes, I have two tables - one that stores the area names and the topics in those areas, in this fashion: column1 | column2 Area1 | Topic1 Area1 | Topic2 Area2 | Topic3 Area2 | Topic4 The user information and value is stored in another table, like so: column1 | column2 | column3 Username1 | Topic1 | value Username1 | Topic2 | value Username2 | Topic1 | value Username2 | Topic4 | value Username3 | Topic2 | value This is the best way I could come up with to optimize table size and structure. |
|||
| By: VGR | Date: 23/09/2004 04:37:00 | Type : Answer |
|
| //from table1, build $topicData like this : $topicData[topic1]['area']=area1, $topicData[topic1]['id']=id of topic // now we'll build your big array // do query=select distinct column1 from table2 order by column1 asc; while ($row=mysql_fetch_array($result)) { $thename=$row['column1']; //do query= select * from table2 where column1='$thename'; while ($row2=mysql_fetch_array($result2)) { $thevalue=$row2['column3']; $thetopic=$row2['column2']; $topicID=$topicData[$thetopic]['id']; // the topic id $thearea=$topicData[$thetopic]['area']; // the area id // fill in big array $skill_data[$thename][$thearea][$thetopic]=$thevalue; } // while topics values for that user } // while users having topics values |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








