Jump to content

Entering data into multidimensional array within for loop


henryhenry

Recommended Posts

Here is where I've got to. I think I'm quite close but I'm a bit stuck (you can see some debugging stuff in the script var_dump etc)I get an error: $table is not recognised on $a_data[$table] = $a_row; line. But I'm not sure why it does that as table is set at the beginning of the loop and is recognised fine up to that point.Basically I want a multidimensional array called $a_data which contains all the arrays created by the SELECT query ($a_row) so something like {[0] => $a_row(1); [1] => $a_row(2)... }I'm going to be calling the data from the $a_data in a form so that I can update/add to the database. The $a_data will also be used to populate the form fields values when the page is loaded, displaying what is already on the database. It's part of a CMS.Thanks so much for your help! Here's the code:

$pid= 1;$a_data= array();for ($j = 1; $j <= 6; $j ++)  {    $table= 'gs_p_head_h'.$j;    echo '<br /><br /><hr />'.$table.'          <br /><br />         ';    $query= "SELECT * FROM $table WHERE page_id_fk=$pid";    $result= mysqli_query($dbc, $query);    if (mysqli_affected_rows($dbc) > 0)      {        $a_row= mysqli_fetch_array($result, MYSQLI_ASSOC);        var_dump($a_row);        if (isset($a_data[$table]))          {            $a_data[$table] = $a_row;          }        else          {            $a_data[$table] .= $a_row;          }        echo '<br /><br />';        var_dump($a_data);              }    echo '<br /><br />'.$query.'          <hr /><br /><br />         ';  }var_dump($a_data);

Link to comment
Share on other sites

Hi!The problem was that you use isset wrong...

if (isset($a_data[$table])) // Checking if there is data{		 $a_data[$table] = $a_row; // setting new data}else // No data{		$a_data[$table] .= $a_row; // appending new data to the "non-existring data"}

Do you get it? :?)It had also worked fine if you used the "append operator" (.=) on the first line (after the if) and just the "assign operator" (=) on the second...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...