0

I have 2 table events and categories. categories table has 2 columns id and category. The event table contains category_idas foreign key of categories table.

Now the content of events table is given by

+------------------------------------------------------+
| id | category_id | event_name | event_date | created |
+------------------------------------------------------+
| 1  |     1       | event 1    | 12-04-2016 |TIMESTAMP|
| 2  |     1       | event 2    | 14-04-2016 |TIMESTAMP|
| 3  |     2       | event 1    | 16-04-2016 |TIMESTAMP|
| 4  |     1       | event 3    | 14-04-2016 |TIMESTAMP|
| 5  |     2       | event 2    | 12-04-2016 |TIMESTAMP|
+------------------------------------------------------+

Now what I want to do is to select all rows and produce output in the form

=> Category 1
     <> event 1
     <><> 12-04-2016
     <> event 2
     <><> 14-04-2016
     <> event 3
     <><> 14-04-2016
=> Category 2
     <> event 1
     <><> 16-04-2016
     <> event 2
     <><> 12-04-2016

How it can be done. I am using PHP and mysqli and I want to store this information in PHP array so that I could print it anywhere in page using PHP for loop or foreach

1

1 Answer 1

2

Sort the query by category, event name

SELECT  categories.category,
        events.event_name

FROM categories

    LEFT JOIN events
    ON events.category_id = categories.id

ORDER BY categories.category, events.event_name

then iterate through the result to construct the multi-dimensional array you want:

$data = array();

foreach($results as $row){
    $data[$row['category']][$row['event_name']]['event_name'] = $row['event_name'];
    $data[$row['category']][$row['event_name']]['event_date'] = $row['event_date'];
}

you can then perform a nested iteration something like this to output:

foreach($data as $category => $events){

    echo $category.'<br>';

    foreach($events as $event){
        echo '    <> '.$event['event_name'].'<br>';
        echo '        <> '.$event['event_date'].'<br>';
    }

}

Rather than using the event / category names as indexes for the multidimensional array, you could grab the event id and use that instead if the names aren't unique.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much. This is what I wanted. But there is a little issue. The last loop foreach($evets as $event){ echo '<> '.$event.'</br>'; } is printing only <> Array and not the array element
I have removed [skills] from second last code and now it's working as expected. Thank you
please see the updated question. I had to add one more column event_date to events table. Now how could I show the event date as well with each event in foreach loop. I have added $data[$row['category']][$row['event_date']] = $row['event_date']; just below $row['event_name']
You are creating an array of arrays where the 'category' acts as the index for the category grouping and then the event name acts as the index for the array that holds. Rather than add the line you have, add another index to the end of both lines: $data[$row['category']][$row['event_name']]['event_name'] = $row['event_name']; and $data[$row['category']][$row['event_name']]['event_date'] = $row['event_date']; then use the named indexes in your nestedr output loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.