Page 1 of 1

Creating associative arrays based on a MySQL query

Posted: Fri Sep 04, 2009 2:30 pm
by phpBever
I'm trying to create some associative arrays based on a MySQL query. I'm getting the right data (can create the individual values from the query), but I can't seem to be able to create the arrays. I've tried working with 'while', with 'foreach', with 'for', but none seem to work. I think I'm missing something in the explanation of how the various PHP functions work that would let me grasp the correct logic.

For each faculty member i, I want to create an array $fac$i with fields: fac_id, fac_name, advStatus (boolean). Then I think I want to combine these into a two-dim array called $fac.

Code: Select all

 
<?php 
include("../include.php");
doDB();
//arrays
/*
$fac[fac1] = array("fac_id" => 1, "fac_name" => "Beversluis, Eric", "advStatus" => "False");
echo $fac[fac1]['fac_id']."<br />"; 
 
$fac[fac2] = array("fac_id" => 2, "fac_name" =>"Underwood, Lynn", "advStatus" => "True");
echo $fac[fac2]['advStatus']."<br />";
 
//$fac = array($fac1, $fac2);
echo "<br /><br />";
 
foreach ($fac as $f) {
    while (list($k, $v) = each($f)) {
        echo "The ".$k." value is ".$v.".<br />";
    }
}
*/
 
    //get list of all faculty
    $sqlFacultyList = "Select fac_id,fac_name FROM tblFaculty ORDER BY fac_name";
    $resFacultyList = mysqli_query($mysqli, $sqlFacultyList);
 
    $numRows = mysqli_num_rows($resFacultyList);
    echo $numRows."<br />"; //gives correct result (4)
 
 
//Now I want to create a set of arrays ($fac1, $fac2, etc), which I will then combine into a two-dim array $fac = array($fac1, $fac2,...)
 
 
/* Doesnt' work:
    while($FacultyList = mysqli_fetch_array($resFacultyList, MYSQLI_ASSOC)) {
        $arrName = "$fac".$FacultyList['fac_id'];
        $arrName = array(
        'fac_id' => $FacultyList['fac_id'],
        'fac_name' => $FacultyList['fac_name'],
        'advStatus' => "False",
        );
}
*/
 
//The following should work but doesn't want to
 
for ($i = 0; $i < $numRows; $i++) {
    $row = mysqli_fetch_assoc($resFacultyList);
    $fac_id = $row['fac_id'];
    $fac_name = $row['fac_name'];
    $advStatus = "False";
    //echo $fac_name; //ie, I've captured the variables correctly.
    //I want an array called $fac1 for the first round, $fac2 for the second round etc. 
    $val = $i+1;
    $fac = '$fac'.$val;
    echo $fac;  //(This give the right value (eg $fac1)
    $fac = array('fac_id'=>$fac_id,'fac_name'=>$fac_name,'advStatus'=>$advStatus);
}
 
echo $fac1['fac_name'];
 
//This also seems to me should work, but doesn't:
for ($i = 0; $i < $numRows; $i++) {
    $val = $i+1;
    $fac = '$fac'.$val;
    $fac = mysqli_fetch_assoc($resFacultyList);
}
 
echo "<br />End";
mysqli_free_result($resID);
msqli_close($mysqli);
?>
 

Re: Creating associative arrays based on a MySQL query

Posted: Fri Sep 04, 2009 9:45 pm
by phpBever
As I look at various "how to" pages about mysqli_fetch_* commands, they all illustrate the commands using "echo" or "printf()"--none seem to address my issue. I think the short way to express my issue is:

How to save a row output in a form that persists with a unique name after the loop has gone on to the next row.

Thanks for any help.

Re: Creating associative arrays based on a MySQL query

Posted: Fri Sep 04, 2009 10:19 pm
by phpBever
I think I got it. It struck me that maybe I need to create the entire two-dim array at once and give up trying to name the sub-arrays in a descriptive manner. So this seems to work:

Code: Select all

for ($i = 0; $i < $numRows; $i++) {
    $row = mysqli_fetch_assoc($resFacultyList);
     $fac_id = $row['fac_id'];
     $fac_name = $row['fac_name'];
     $advStatus = "False";
     //echo $fac_name; //ie, I've captured the variables correctly.
     $fac[] = array('fac_id'=>$fac_id,'fac_name'=>$fac_name,'advStatus'=>$advStatus);
    }
Then when I need the array for fac_id 4 I'll just call $fac[3][...] etc.

Thanks.