2

i have two tables category and images.i want create a multidimensional array like

$headings = ['heading 1', 'heading 2', 'heading 3'];
$images = [
    'heading 1' => ['image 1', 'image 2', 'image 3', 'image 4', 'image 5'],
    'heading 2' => ['image 1', 'image 2', 'image 3'],
    'heading 3' => ['image 1']
];

So how can i do that using php.

Images table

enter image description here

category table

enter image description here

2
  • title careate correct to create Commented Oct 1, 2016 at 10:36
  • 1
    what have you tried? It seems like you want us to develop the whole system for you stackoverflow.com/questions/39804655/… Commented Oct 1, 2016 at 10:37

2 Answers 2

2
$sql = "select category.name,group_concat(images.image_name) from category inner join images on (category.id = images.category_id) group by category.name";
$table = mysql_query($sql);
$arr = array();
while($row = mysql_fetch_row($table))
{
    $row['image_name'] = explode(",", $row['image_name']);
    $arr[] = $row;
}
Sign up to request clarification or add additional context in comments.

Comments

1

The tool code you will need: mysqli. Now suppose you know it.

Solution 1

The slowest. Just fetch all the lines you need, then use a for loop to make it what you want.

Solution 2

Use GROUP_BY + GROUP_CONCAT function in sql.

I am in a hurry. So which do you like best, and do you need a further example?

Comments

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.