0

I'm new to php arrays. I wanter to store below multidimensional array into a table from mysql say table_chapterswhere each array is stored per row. After that, I wanted to display the data from mysql database with the same format using php. Please help.

table_chapters (Sample)

id |        name           | logo                |   skin
0  |    Chapter 1          | logo.1484573133.png |   chapter-1.css
1  |    Chapter 2          | null                |   chapter-2.css
2  |    Chapter 3          | null                |   chapter-3.css
3  |    Chapter 4          | null                |   chapter-4.css

The Array:

$chapters = array (
  0 => 
  array (
    'name' => 'Chapter 1',
    'logo' => 'logo.1484573133.png',
    'skin' => 'chapter1.css',
    'show-time' => true,
    'streams' => 
    array (
      'Default Quality' => 
      array (
        'mp3' => 'mp3-1.mp3',
      ),
    ),
    'stats' => 
    array (
      'method' => 'server1',
      'url' => 'localhost',
      'user' => 'srem',
      'use-cover' => 'true',
    ),
  ),
  1 => 
  array (
    'name' => 'Chapter 2',
    'logo' => NULL,
    'skin' => 'chapter2.css',
    'show-time' => true,
    'streams' => 
    array (
      'Default Quality' => 
      array (
        'mp3' => 'mp3-2.mp3',
      ),
    ),
    'stats' => 
    array (
      'method' => 'server1',
      'url' => 'localhost',
      'user' => 'ecr00',
      'use-cover' => 'true',
    ),
  ),
  2 => 
  array (
    'name' => 'Chapter 3',
    'logo' => NULL,
    'skin' => 'chapter3.css',
    'show-time' => true,
    'streams' => 
    array (
      'Default Quality' => 
      array (
        'oga' => 'music.oga',
      ),
    ),
    'stats' => 
    array (
      'method' => 'server1',
      'url' => 'localhost',
      'user' => 'vand',
      'use-cover' => NULL,
    ),
  ),
  3 => 
  array (
    'name' => 'Chapter 4',
    'logo' => NULL,
    'skin' => 'chapter4.css',
    'show-time' => true,
    'streams' => 
    array (
      'Default Quality' => 
      array (
        'mp3' => 'mp3-4.mp3',
      ),
    ),
    'stats' => 
    array (
      'method' => 'server1',
      'url' => 'localhost',
      'user' => 'ooff',
      'use-cover' => 'true',
    ),
  ),
);

1 Answer 1

0

Try this:

foreach($chapters as $index => $chapter)
{
    $query = "INSERT INTO table(id, name, logo, skin) VALUES(".$index.", '".$chapter['name']."', '".$chapter['logo']."', '".$chapter['skin']."')";

    // fire the query on the specified database table. Each iteration add a new row in the table    
}
Sign up to request clarification or add additional context in comments.

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.