saeed_violinist Posted June 22, 2010 Share Posted June 22, 2010 Dear friends, I have a mysql table like this, and it is going to grow soon! Uploaded with ImageShack.us As you see it is a table and it holds different categories of articles. 'id' -> is the auto increment by MySQL. 'pid' -> is the Parent ID, 0 is main parent categories that is the categories which are going to hold other categories. and these categories with pid=0 shouldn't host any article themselves. and the categories with pid=0,1, etc... will be theire childs respectivly 'sort' -> is the way how they are going to be sorted in the array I want. 'isParent' -> is another way of telling wether or not the category is a parent or not. if it is not, then it is a child category and it can have articles. 'name' -> is the name of categories and so on! The problem is, I want to make a multi dimensional array out of this, but I don't know the right way. and they should sorted by theire any help and suggestion is really appriciated! here is the code I manage to write but it only will hold the parents, not the childs: $articleParentCats = $_DB->Query("SELECT * FROM articlecat WHERE isParent='Y'"); for($i=0; $i<$_DB->GetResultNumber($articleParentCats); $i++) { $_ART_P_CAT[$i]["name"]=$_DB->GetResultValue($articleParentCats, $i, "name"); $_ART_P_CAT[$i]["desc"]=$_DB->GetResultValue($articleParentCats, $i, "desc"); $_ART_P_CAT[$i]["img"]=$_DB->GetResultValue($articleParentCats, $i, "img"); $_ART_P_CAT[$i]["sort"]=$_DB->GetResultValue($articleParentCats, $i, "sort"); } Link to comment https://forums.phpfreaks.com/topic/205537-making-a-multi-dimensional-array-out-of-a-mysql-table/ Share on other sites More sharing options...
saeed_violinist Posted June 22, 2010 Author Share Posted June 22, 2010 I did some stuff but no result yet, My approach is to have two different arrays, one for parents and anotherone for childs. then I want to make a new array out of this two arrays that will contain parents with theire respective childs. here is my code, PLEASE HELP! : <?php // Get parent categories $articleParentCats = $_DB->Query("SELECT * FROM articlecat WHERE isParent='Y' ORDER BY sort ASC"); $parentCatsCount = $_DB->GetResultNumber($articleParentCats); for($i=0; $i< $parentCatsCount; $i++) { $_ART_PARENT_CATS[$i]["name"]=$_DB->GetResultValue($articleParentCats, $i, "name"); $_ART_PARENT_CATS[$i]["desc"]=$_DB->GetResultValue($articleParentCats, $i, "desc"); $_ART_PARENT_CATS[$i]["isParent"]=$_DB->GetResultValue($articleParentCats, $i, "isParent"); $_ART_PARENT_CATS[$i]["img"]=$_DB->GetResultValue($articleParentCats, $i, "img"); $_ART_PARENT_CATS[$i]["sort"]=$_DB->GetResultValue($articleParentCats, $i, "sort"); } // Get child categories $articleChildCats = $_DB->Query("SELECT * FROM articlecat WHERE isParent='N' ORDER BY sort ASC"); $childCatsCount = $_DB->GetResultNumber($articleChildCats); for($i=0; $i<$childCatsCount; $i++) { $_ART_CHILD_CATS[$i]["name"]=$_DB->GetResultValue($articleChildCats, $i, "name"); $_ART_CHILD_CATS[$i]["desc"]=$_DB->GetResultValue($articleChildCats, $i, "desc"); $_ART_CHILD_CATS[$i]["isParent"]=$_DB->GetResultValue($articleChildCats, $i, "isParent"); $_ART_CHILD_CATS[$i]["img"]=$_DB->GetResultValue($articleChildCats, $i, "img"); $_ART_CHILD_CATS[$i]["sort"]=$_DB->GetResultValue($articleChildCats, $i, "sort"); } // Making a multidimensional array with parents and childs $countCats = $parentCatsCount + $childCatsCount; for($i=0; $i<$countCats; $i++) { for($j=0; $j<$parentCatsCount; $j++) { $ARTICLES[$j]= $_ART_PARENT_CATS[$j]; } if(in_array()); // DONT KNOW WHAT TO DO THEN! } echo"<pre>"; print_r($_ART_PARENT_CATS); echo"</pre>"; echo"<br />"; echo"<pre>"; print_r($_ART_CHILD_CATS); echo"</pre>"; echo"<br />"; echo"------ FINAL ARRAY -------"; echo"<pre>"; print_r($ARTICLES); echo"</pre>"; ?> Link to comment https://forums.phpfreaks.com/topic/205537-making-a-multi-dimensional-array-out-of-a-mysql-table/#findComment-1075544 Share on other sites More sharing options...
gwolgamott Posted June 22, 2010 Share Posted June 22, 2010 I love and hate multidimensional arrays. See here for a good reference to help wrap your head around them first of: http://www.webcheatsheet.com/php/multidimensional_arrays.php $TABLE_DATA[$My_parentID][$Child_ID][$name] = "Electronics"; $TABLE_DATA[$My_parentID][$Child_ID][$desc] = "All articles about electronics"; $TABLE_DATA[$My_parentID][$Child_ID][$img] = "NULL"; $TABLE_DATA[$My_parentID][$Child_ID][$forum] = "NULL"; I'd have a flag to fill the $Child_ID if it is a parent, such as PAR then do a check on that for printing. It should be the only one that would not have a ID at times if it's a parent. A child will always have a parent associated with it. Link to comment https://forums.phpfreaks.com/topic/205537-making-a-multi-dimensional-array-out-of-a-mysql-table/#findComment-1075566 Share on other sites More sharing options...
saeed_violinist Posted June 22, 2010 Author Share Posted June 22, 2010 Hi, Thanks for reply! it gave me some ideas but still I can't work it around. let me make it simple. I will only have 2 type of categories, PARENT and CHILD . parents are not going to host articles, so they are only a way to sort the childeren. I want something like this" - Electronics //PARENT 1 - Academic //CHILD 1 - Microprocessors //CHILD 2 - Hardware //PARENT 2 - Video cards //CHILD 1 - CPU //2CHILD Thanks! Link to comment https://forums.phpfreaks.com/topic/205537-making-a-multi-dimensional-array-out-of-a-mysql-table/#findComment-1075719 Share on other sites More sharing options...
gwolgamott Posted June 22, 2010 Share Posted June 22, 2010 - Electronics //PARENT 1 - Academic //CHILD 1 - Microprocessors //CHILD 2 - Hardware //PARENT 2 - Video cards //CHILD 1 - CPU //2CHILD So you want something like this... maybe. I like to sometimes give the main keys the value of a "parent" instead of a number.... $multi[Electronics][0] = "Academic"; $multi[Electronics][1] = "Microprocessors"; $multi[Hardware][0]="Video Cards"; $multi[Hardware][1]="CPU"; You could say that you have a parent who's child has 3 traits and store it as such too: $multi[Electronics][0][0] = "I am trait one of the Academic child who belongs to the Electronics Parent."; $multi[Hardware][1][2] ="I am trait three of the CPU child who belongs to the Hardware Parent."; Or use key values for parent & Child... $multi[Electronics][Academic][0] = "I am trait one of Academic Child who belongs to Electronics Parent."; ... also as such $multi[Electronics][Academic][Trait1] = "I am trait one of Academic Child who belongs to Electronics Parent."; Link to comment https://forums.phpfreaks.com/topic/205537-making-a-multi-dimensional-array-out-of-a-mysql-table/#findComment-1075761 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.