1

i want to use arrData array outside loop too, how to declare it outside loops scope, as if now it is not being populated correctly

  $SQL = "SELECT * FROM DailyData WHERE User='$_SESSION[userID]'"; 
    $result = mysql_query($SQL); 
    $i=0; 
    // trying this
    $arrData= array();
    $arrData []= array();
    while($row = mysql_fetch_array($result)) 
    { 
    echo $row['Score'] . " " . $row['Date']; 
    $arrData[i][1]=$row['Date']; 
    $arrData[i][2]=$row['Score']; 
    $i++; 
    echo "<br />"; 
    }
3
  • Can you be a little more specific, it appears that your variable $arrData is residing within the outer scope of the while-loop.. so this code should be golden Commented Aug 7, 2011 at 22:25
  • but how to properly declare it outside loop's scope, because it is not getting populated right Commented Aug 7, 2011 at 22:27
  • Just use $arrData = array(); You dont need the $arrdata [] = array(); when youŕe in the loop and specify the $arrData[i][x], it will automagically create an array on i for you Commented Aug 7, 2011 at 22:28

2 Answers 2

2

$arrData is available outside the scope of the while loop.

Looks like you forgot the dollar sign:

$arrData[i][1]=$row['Date']; 
$arrData[i][2]=$row['Score']; 

Should be

$arrData[$i][1]=$row['Date']; 
$arrData[$i][2]=$row['Score']; 

In the original, PHP thinks you mean $arrData['i'], which means you keep over writing $arrData['i'][1] and $arrData['i'][2] - however, you are incrementing $i, so you want to to make use of that variable - $arrData[$i][1] and $arrData[$i][2].

Sign up to request clarification or add additional context in comments.

Comments

1

I guess this may be what you're looking for:

  $SQL = "SELECT * FROM DailyData WHERE User='$_SESSION[userID]'"; 
  $result = mysql_query($SQL); 
  $arrData= array();

  while($row = mysql_fetch_array($result)) 
  { 
    $arrData[]=$row;
    //you can also do some output here if you want
  }

  //arrData is now available outside the while-loop
  foreach($arrData as $row) {
    echo $row['Score'] . ' ' . $row['Date'] . '<br />'; 
  }

1 Comment

What you are doing here is exactly the same as $arrData = mysql_fetch_array($result); btw :p

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.