Building a specific array from a table of data - php

I have a table of data as such:
id | item | parent_id
1 item 1 0
2 item 2 0
3 item 3 2
4 item 4 3
5 item 5 1
...
The id is autoincrementing, and the parent_id reflects the id on the left. You may have come accross a database table design like this before.
The parent_id is not sequential as you can see.
I need to get this table data into an array in the format where all parents become a potential heading with their children underneath.
So I am looking at a structure like this:
Item 1
Item 5
Item 2
Item 3
Item 4
etc
In PHP I need an array structure that can display the above. But I am having a serious brain fart!
Can anyone help me with the array structure?

you may write somethin like this:
$a = Array(item1 => Array(item5), item2 => Array(item3 => Array(item4)))
or
$a = Array(item1 => parentid, item2 => parentid2 ....)
in the first example one item is the key for all ist children stored in the Array, in the other example all items are stored using an item key and an int value. this int value is the key for the parent item. which method you use depends on what you want to do with this Array. maybe both of my ideas are not good enough for your Needs. if thats the case, tell me what you need.

First of all, i will suggest you to read this, it's very useful for hierarchical structured data and there are available queries which will help you to get parents, children, etc ... and so and so.
Now to answer your question, try this :
$result = array();
while($data = mysql_fetch_assoc($query)) {
$id = $data['id'];
$parent = $data['parent_id'];
$keys = array_keys($result);
if(in_array($parent, $keys)) {
$result[$parent] = array_merge($result[$parent], array($id => $data['item']));
} else {
$result = array_merge($result, array($id => $data['item']));
}
}

Related

Multipage Navigation and AJAX

I'm looking at creating a 'Multi Page Navigation System' using PHP and MySQL along with jQuery.
Ideally, what I want is to be a a list of items, such as:
* Item 1
* Item 2
* Item 3
Within Item 1, there is 3 other subcategories, within Item 2 there are 2 subcategories and within them 2 subcategories there are 3 more subcategories
So what I'm really looking for is the following:
1) when i click on 'Item 2` is displays the 2 subcategories
2) when I click on one of these subcategories it displays the 3 others
Ideally, I'd like to do this in using AJAX as I'd like this to be in a jQuery UI Dialog.
I have 2 tables:
category
id | title
----------
1 | item 1
2 | item 2
3 | item 3
subcategory (simplified)
id | cat_id | parent_id | title
-------------------------------
1 | 1 | 0 | subcat1
2 | 1 | 0 | subcat2
3 | 1 | 1 | subcat1_subcat1
4 | 1 | 1 | subcat1_subcat2
5 | 1 | 1 | subcat1_subcat3
My main issue is how I'd go about doing this?
I don't really want to have a big array with all that data in, as it could potentially have more categories and subcategories.
Does any have an idea what would be the best solution to about this?
Thanks
What's wrong with having it all stored in an array? Unless you're planning on have thousands of elements in those menu items (which would be incredibly un-user-friendly), then it's just a walk in the park for PHP.
Also, you might want to be abit more specific about what your requirement is. Is it the jQuery, the PHP or both? Do you need the code or the concept?
EDIT: Solution
So based on the comments you listed, here's a proof of concept.
PHP:
You'll need to read from the Database and load them into an array. That's pretty easy to do with PDO. Just use the fetchAll() command and retrieve the entire result set in an associative array. The tricky part becomes converting your 'flat' DB into a multi-dimensional array. Here goes:
// Retrieve the details from the database in the $rows associative array
...
// Now, we need to expand the 'flat' DB structure into a multi-
// dimensional array.
$multid=findKids($rows);
// Send it back, JSON-encoded
die(json_encode(
'result' => 'success',
'response' => $multid
)); // Send the response back via AJAX
/**
* Here's the function that converts the flat DB into a multi-
* dimensional array. It tracks all the parents in a single
* array and collects the kids for those parents. If it comes
* across a new parent, if fetches all the kids recursively.
*/
function findKids( $rows, $parentid=NULL, &$parents=NULL ) {
// Create a temporary array for the kids
$shelter;
// Go through all the rows
foreach($rows as $index => $row) {
// Find the kids that belong to this parent
if($row['parentid']==$parentid) {
// This kid belongs to this parent
// Move it to the temporary shelter
$shelter[$parentid][]=$row;
}
else {
// This kid doesn't belong to this parent.
// Have we already talked to the parent before?
if(isset($parents[$row['parentid']])) {
// Yes, the parent has already been visited. Ignore
}
else {
// Parent hasn't been visited; add it
$shelter[$row['parentid']][]=findKids($rows,$row['parentid'],$parents);
}
} // close else{ }
} // close foreach{ }
// Return the shelter, with all the kids
return $shelter;
}
The returned array will include a response that looks like this:
$response=[
'result'=>'success',
'response'=>[
0=>[ // Contains the kids for $parentid=0
['id'=>1, 'cat_id'=>1, 'parent_id'=>0],
['id'=>2, 'cat_id'=>1, 'parent_id'=>0]
],
1=>[ // Contains the kids for $parentid=1
['id'=>3, 'cat_id'=>1, 'parent_id'=>1],
['id'=>4, 'cat_id'=>1, 'parent_id'=>1],
['id'=>5, 'cat_id'=>1, 'parent_id'=>1]
],
]
];
jQuery: You'll interpret the JSON response and iterate through the response to create the menu on the fly.
Here's a sample script that'll display the array as a nested unordered list.
// Call the printMyFamily method on the element to display
// that you'd like to see the menu appear in
$outputElem.html($.printMyFamily(NULL,response['response']));
$.printMyFamily = function(parentid,haystack){
// Our output variable
output="<ul>";
// Find the kids
$haystack.each(function(index,elem){
// Is this kid ours?
if(elem[parentid] == parentid){
// Yes. Add to output
output+="<li id='"+elem['id']+"'>"+elem['catid']+"</li>";
// Find this items kids (if any)
output+=$.printMyFamily(elem['id'],haystack);
}
else { /* not ours, ignore */ }
});
// Is the result an empty string? If so, just clear it
if(output=="<ul>"){ output=""; }
// Return the output
return output;
};

Getting total amount of rows that contain parent/child categories. Confused why in_array isn't working

I have two tables I'm working with: categories and businesses. The categories table looks like this:
id name parent
1 Automotive NULL
2 Tires 1
3 Oil Change 1
4 Home Renovations NULL
5 Painting 4
6 Landscaping 4
7 Bathroom 4
Basically, any category that has parent as NULL is a parent. Anything that is a child of it references it's ID in the parent column. Simple.
I have businesses stored in a table, and each business has categories. The categories are stored as json_encode so they look like this:
["1","4","5","13"]
The user can add a subcategory without adding a parent, so some businesses only have subcategories.
If I want to get the total number of business for a parent category INCLUDING subcategories, here's what I'm doing:
$parent_categories = $this->db->order_by('name', 'asc')->get_where('categories', array('parent' => NULL));
$businesses = $this->db->select('category')->get('businesses');
foreach ($parent_categories->result() as $parent):
$child_categories = $this->db->order_by('name', 'asc')->get_where('categories', array('parent' => $parent->id));
$parentChildCategories = array();
array_push($parentChildCategories, $parent->id);
foreach($child_categories->result() as $child):
array_push($parentChildCategories, $child->id);
endforeach;
// CONTINUED BELOW
At this point, if i print_r($parentChildCategories), I get the following (excluding a bunch of other category arrays, just focusing on one):
Array ( [0] => 81 [1] => 80 )
So this is the parent category id as well as the child category id. This parent category only has one child, but others might have multiple. This appears to work.
Now I want to go through each businesses category field, decode the json into a PHP array ($categories_array), then see if the above array ($parentChildCategories) is in it. If it is, I echo 'yep'.
foreach($businesses->result() as $business):
$categories_array = json_decode($business->category);
if (in_array($parentChildCategories, $categories_array)):
echo 'yep';
endif;
endforeach;
The problem is, I never get 'yep'. Nothing. So I `print_r($categories_array)' and it gives me the following:
Array ( [0] => 80 [1] => 81 )
The array values are the same as $parentChildCategories, but they are in different positions. So in_array doesn't see it as being in the array.
I'm banging my head against a wall trying to figure this out. Is there a better way of doing this? I'm obviously doing something wrong. Any help would be greatly appreciated.
Why do you store the categories related to businesses this way? If you'd normalise your database, you wouldn't have this problem in the first place.
I'd suggest creating a new table 'business_category_coupling', with 2 columns: business_id and category_id. That's basically all you'll ever need and eases maintenance dramatically.
The reason in_array does not work is that it checks whether the first array is an element in the second array - which, of course, it is not. Without going through the full logic, to do your comparison, you can use array_diff:
$ad = array_diff($parentChildCategories, $categories_array);
if(count($ad)) {
echo 'yep';
}
This code finds all elements from $parentChildCategories that are not present in $categories_array. If there are none, then you output yep.

PHP Make a multidimensional array from one mysql Row's columns?

I have the following mysql db row.
id | user_id | title_1|desc_1|link_1|title_2|desc_2|link_2|
and so on up to 10
from this one row I want to remove id and user id and have the resulting multidimensional array.
the main issue is iterarating over the associative array that is returned by my query and splitting it up into arrays of 3.
Array = (
[0] = array (
[tite_1] => 'sometitle'
[desc_1] => 'description'
[link_1] => 'a link'
)
[1] = array (
[tite_2] => 'sometitle'
[desc_2] => 'description'
[link_2] => 'a link'
)
and so on how can I achieve this I am stumped!!?
You probably want to structure your table into two tables like this:
parent(id, user_id, more_fields, whatever_you_need_here)
child(parent_id, title, desc, link)
Now it'll be very easy to get the data that you want to have.
SELECT title, desc, link FROM child WHERE parent_id = 12;
Of course, parent and child should be named appropriately, e.g. user and links.
The correct answer would be to redesign your database to use 3rd normal form. You should probably drop everything and read up on database normalization before you do anything further.
A proper design would be something like:
CREATE TABLE user_has_links (
id INT PRIMARY KEY,
user_id INT,
title TEXT,
description TEXT,
link TEXT
)
To store multiple links per user, you would simply create a new row in this table per link.
The real solution here is to fix your database to normalize these columns into other tables. However, if you are not in a position to fix your database, this code will do the job:
// $output will hold your full result set
$output = array();
while ($row = mysql_fetch_assoc($result)) {
// For each row returned, add a new array to $output
$output[] = array(
// The new array consists of 10 sub-arrays with the correct
// keys and values
array (
"title"=>$row['title1'],
"desc"=>$row['desc1'],
"link"=>$row['link1']
),
array (
"title"=>$row['title2'],
"desc"=>$row['desc2'],
"link"=>$row['link2']
),
...,
...,
array (
"title"=>$row['title10'],
"desc"=>$row['desc10'],
"link"=>$row['link10']
)
);
}

Removing Records from an Associated Table PHP

I am working on a Many to Many table and I fixed an issue where I couldn't pull the records. Now I'm trying to delete records when that is needed.
I have a site where children can be associated to an event. In the event that a child was accidentally added I want to be able to remove the child. They are added via Checkbox as follows:
<input type="checkbox" name="eventChildren[]" id="childId_12" />
<input type="checkbox" name="eventChildren[]" id="childId_13" />
<input type="checkbox" name="eventChildren[]" id="childId_14" />
I have multiple checkboxes that are pre-checked if the child has already been added.
I have three tables (not sure if this is relevant):
DB:
- children
- events
- eventChildren
When I save the record I'm doing a check to make sure I don't add duplicates to my eventChildren table. I know if records are already in the database but not submitted from the form then the child is being removed from the list...and I can't figure out how to catch this subset.
Example code:
// $eventChildren comes from the form above name="eventChildren[]"
// $eventId is passed into the function
$currentChildren = $this->eventChildren->GetEventChildren(array('eventId' => $eventId));
// Loop through all submitted children
foreach ($eventChildren as $childId)
{
// Loop through all existing children
foreach ($currentChildren as $currChild)
{
// If the child ID's do not match then it's a new record
if ($currChild->childId != $childId)
{
$this->eventChildren->AddEventChildren(array(
'eventId' => $eventId,
'childId' => $childId
));
}
}
}
When the form is submitted I get a return that grabs all records from the eventChildren table where the eventId matches.
eventChildren (return)
- [0]
eventChildrenId => 1
childId => 12
eventId => 4
- [1]
eventChildrenId => 2
childId => 13
eventId => 4
- [2]
eventChildrenId => 3
childId => 14
eventId => 4
submittedChildren
- [0] => 12
- [1] => 13
How can I say, based on the submittedChildren array remove the record where eventChildrenId = 3?
I hope this makes sense. :)
There's many ways to approach this(better ways), but I'm just going to go with a straight forward answer I can give based on the info you presented.
$orphaned = array();
foreach ($currentChildren as $currChild) {
if (!in_array($currChild->childId, $submittedChildren)) {
$orphaned[] = $currChild->childId;
}
}
Just to be clear, you're deleting the relationship between the two items (i.e. deleting from the many-to-many table)?
Usually the event_id and the child_id fields would be the identifying (PRIMARY) key in the relationship. Therefore, if the user submits a child_id of 13, and you can contextually derive that the event_id is 4, then deleting from the eventChildren table where child_id = 13 and event_id = 4 should suffice. You shouldn't need to first gather information about the eventChildren object (you already have the identifying info).

Query regarding associative arrays PHP

I have a question about associative arrays in php.
I have following array in which there are two items named 4 and 2 respectively.
$items = array(4,2);
Now i want to associate each item's quantity to it which can be done as follows:
$items['4']=23;
$items['2']=0;
which means that there are 23, 'item 4s' and no 'item 2'.
But I sometimes don't know in advance what is there in the $items so i want to associate quantity on basis of location. I wanted to do something like associate 23 to whatever is there on the zero location of the item array:
$items['items[0]']=23;
This of course did not work because its not the right way to extract whatever is placed on the zero location of items. Can anyone please tell me how do i do that?
You are confusing in the use of item and items. I imagine you have both an item array and an items array, else things can easily get hairy.
Anyhow, you just refer to it as a variable, not as a string:
$items[$item[0]] = 23;
Let me get this straight. So you start with an array that looks like this:
$items = array( 0 => 4, 1 => 2 )
And you want to end up with an array that looks like this: ?!
$items = array( 0 => 4, 1 => 2, 2 => 0, 4 => 23 )
I think you should use your array as a kind of "map". The item number is your key, and the quantity your value.
By calling
$items = array(4,2);
you create
$items[0] = 4;
$items[1] = 2;
but you want to use the 4 and 2 as a key in your array. So you should instead use
$items = array( 4 => false, 2 => false );
where false stands for an item that has not yet a quantity associated (could also be e.g. -1).
This creates
$items[2] = false;
$items[4] = false;
When using false, you can check for not assigned values by calling
if ($items[4] === false) {
echo "No quantity set!";
}
And now the second step.. if you want to assign the item #4 a quantity of 23, just call
$items[4] = 23;
So I don't think you will want to rely on the order inside your array..

Categories