I want to store variables into a while loop for my mysqli:fetch() statement, problem is that I think I am writing the code incorrectly. Especially for the variable $dataArraySession, can anybody look at code below and state what is the correct way in writing the code below:
$sessionquery = "
SELECT SessionId, SessionDate, SessionTime, ModuleId
FROM Session
WHERE
(ModuleId = ?)
ORDER BY SessionDate, SessionTime
";
$sessionqrystmt=$mysqli->prepare($sessionquery);
// You only need to call bind_param once
$sessionqrystmt->bind_param("s",$_POST['modulesDrop']);
// get result and assign variables (prefix with db)
$sessionqrystmt->execute();
$sessionqrystmt->bind_result($dbSessionId,$dbSessionDate,$dbSessionTime, $dbModuleId);
$sessionqrystmt->store_result();
$sessionnum = $sessionqrystmt->num_rows();
$dataArraySession = array();
while ( $sessionqrystmt->fetch() ) {
$dataArraySession[$dbModuleId]['Sessions'][$dbSessionId]['SessionDate'] = $dbSessoonDate['SessionTime'] = $dbSessionTime;
}
foreach ($dataArraySession as $sessionId => $sessionData) {
$sessionHTML = "";
$sessionHTML .= '<select name="session" id="sessionsDrop">'.PHP_EOL;
$sessionHTML .= '<option value="">Please Select</option>'.PHP_EOL;
$sessionHTML .= "<option value='$sessionId'>" . $sessionId . " - " . $sessionData['SessionDate']. " - " . $sessionData['SessionTime'] ."</option>".PHP_EOL;
$sessionHTML .= '</select>';
}
if ($sessionnum > 0) {
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>Sessions: <?php echo $sessionHTML; ?><input id="sessionSubmit" type="submit" value="Submit" name="sesionsubmit" /></p>
</form>
<?php
}
else {
echo "<p>Sorry, You have No Sessions under this Module</p>";
}
?>
There are several errors in your code:
After
,
$dataArraySessionis an array, that’s all. So assigningis valid, but accessing/assigning
(in your while-loop) is invalid, because
$dataArraySession[$dbModuleId]is still undefined;$dbSessionDateis a bound value from$sessionqrystmt->bind_result(), so it would probably be a string or a number, sois invalid, because it’s just a string, not an array;
In your output loop, you’re also echoing invalid variables.
In your while-loop,
$dataArraySessionis defined asSo with
,
$sessionDatais only at the level of'Sessions' => array (..., thus$sessionDatahas only one key,'Sessions', and both$sessionData['sessionDate']and$sessionData['sessionTime']are invalid.