Render mysql data using php array

  arrays, mysql, php

I have been tring to make my return look this way

// sample array data 
$data = array(

 '0' => array('Name'=> 'user1', 'Status' =>'complete', 'Priority'=>'Low', 'Salary'=>'001'),

 '1' => array('Name'=> 'user2', 'Status' =>'inprogress', 'Priority'=>'Low', 'Salary'=>'111'),

 '2' => array('Name'=> 'user3', 'Status' =>'hold', 'Priority'=>'Low', 'Salary'=>'333'),

 '3' => array('Name'=> 'user4', 'Status' =>'pending', 'Priority'=>'Low', 'Salary'=>'444'),

 '4' => array('Name'=> 'user5', 'Status' =>'pending', 'Priority'=>'Low', 'Salary'=>'777'),

 '5' => array('Name'=> 'user6', 'Status' =>'pending', 'Priority'=>'Low', 'Salary'=>'777')

);

But this what i did and it’s not working out

<?php
// sample array data 

$data = "";

$sql = "SELECT * FROM uploaded";

$display = "";

$i = 0;

foreach( $db->query($sql) as $data ) {

    $i++;

    $id           = $data["id"];

    $studentName  = $data["name_of_student"];

    $RegNo        = $data["reg_no"];

    $dept         = $data["dept"];

    $yearStudy    = $data["year_of_study"];

    $inCourse     = $data["in_course"];

    $exam   = $data["exam"];

    $score  = $data["score"];

    $grade  = $data["grade"];
   
    $details .= "'$i' => array('NAME OF THE STUDENT'=> '$studentName', 'REG. NO' =>'$RegNo', 'DEPT.'=>'$dept, 'YEAR OF STUDY'=>'$yearStudy', 'IN COURSE'=>'$inCourse', 'EXAM'=>'$exam', 'TOTAL SCORE'=>'$score', 'GRADE'=>'$grade'),";

    $data = array($details);

}

Source: Ask PHP

LEAVE A COMMENT