I'm trying to populate the array for the variable $example_data. I have an array of Mass' and dates that I want to populate this array with. However I do not know how to create a new array entry for each $mass[$x] and $date[$x] that I want to store. I've tried putting a foreach loop inside the $example_data = array( but it didnt work.
This is what I want it to do, but doesn't work:
$example_data = array(
foreach($exer->results() as $ex){
$mass = $ex->Mass;
$date = $ex->Date;
array($date,$mass),
);
This is what I've tried but not sure how to complete it:
$userID = $user->data()->id;
$sql = "SELECT * FROM userdetails WHERE UserID = ".$userID."";
$details = DB::getinstance()->query($sql);
$x = 1;
foreach($details->results() as $detail){
/** getting data from each record from field Mass and storing it in $mass array **/
$mass[$x] = $detail->Mass;
$date[$x] = $detail->Date;
$x++;
}
$x = 1;
$example_data = array(
array($date[$x],$mass[$x]),
/** I want it to create a new array entry for each $mass[] **/
);
It depends on the type of database you're using...
For example if you're using MySQL, it's mysqli_fetch_assoc()
You're trying to fetch a result set into an associative array but the result set itself is an associative array
try...
$sql = "SELECT * FROM userdetails WHERE UserID = ".$userID."";
$details = DB::getinstance()->query($sql);
$example_data = array(
array("Mass"),
array("Date")
);
while($row = $details->fetch_assoc()) {
array_push($example_data['Mass'], $row['name_of_mass_column_in_db']);
array_push($example_data['Date'], $row['name_of_date_column_in_db']);
}
}
Basically, you can just fetch an associative array instead of populating an associative array with an associative array...