I am having some issues with posting data from a 2 dimensional array and echoing them.
I have read several topics and tutorials on multidimensional array, but most of them used fixed length of arrays as examples. In my case both the dimensions of the array are variable. The project I am working with is to provide awards to students from various year-levels.
Example: Year-level 3 may have 5 awards and each award is given to variable number of students. Award A to 5 students, Award B to 2 students and so on. Similarly, Year 6 may have 15 awards and so on.
There are tables which has the record for which year-level has which all Awards and the quantity of awards to be allocated each year.
I have been able to display the year levels and their respective awards in one form (in html/php). But I am desperately trying to figure out how to post the data from the 2 dimensional array and echo them (the idea is to insert them into tables if I can echo them).
The list of students are coming from a table (displaying the student name but selecting the student code)
<?php
include 'db_connect.php';
$query = mysqli_query($conn, "SELECT a.awardcode, a.year_grp, a.awardqty, b.awardname FROM awardallocation AS a INNER JOIN award AS b
ON a.awardcode = b.awardcode
WHERE a.year_grp = '$level' AND b.awardstatus != '0' ORDER BY b.awardname ASC ");
$row = mysqli_num_rows($query);
if($row > 0) {
while ($result = mysqli_fetch_assoc($query)){
$awardname = trim($result['awardname']);
$awardcode = trim($result['awardcode']);
$awardqty = trim($result['awardqty']);
?>
<table>
<tr>
<th>Award Name: <?php echo $awardname; ?></th>
</tr>
<tr style="background-color:#FFF9C4">
<?php
for($i = 0; $i < $awardqty; $i++ ){
?>
<td>
<select name="stud_code[$awardcode][$i]">
<option disabled selected value> -- Select Student -- </option>
<?php
// include 'db_connect.php';
$sql = mysqli_query($conn, "SELECT stud_code, surname, preferred_name FROM student WHERE year_grp = '$level' ORDER BY surname ASC ");
while ($row1 = mysqli_fetch_assoc($sql)){
$name = trim($row1['surname']).', '.trim($row1['preferred_name']);
echo '<option value = " '.$row1['stud_code'].'"> '.$name.' </option>';
}
?>
</select>
</td>
<?php } ?>
</tr>
</table>
<?php
}
}
?>
<table>
<tr>
<td><lable>Lock data to prevent changes in future?</lable><input type="checkbox" name="lock" value="1"/></td>
</tr>
</table>
<div class="button-section">
<input id="save_button" type="submit" name="submit" value="Save Selection">
<input id="exit_button" type="submit" name="exit" value="Exit" onclick="window.history.go(-1); return false;">
</div>
</form>
</div>
</body>
<?php
if(isset($_POST['submit'])) {
for ($row=1; $row<=count($_POST ['stud_code["awardcode"]']); $row++){
echo $_POST["awardcode"];
for ($col = 0; $col < 3; $col++){
// echo("<li>".$_POST['stud_code']['awardcode'][$i]."</li>");
//$student = $_POST['stud_code[$awardcode][$i]'];
}
}
$lock = (isset($_POST['$lock']) ? $_POST['$lock'] : '0');
$next_year = date("Y")+1;
}
Please forgive me if the explanation above is not very clear. The code I have written is included.
Any suggestion and help will be highly appreciated as I can have a good night’s sleep after this.. :)
You forgot to echo out within your name attribute keys:
<select name="stud_code[<?=$awardcode?>][<?=$i?>]">
I'd recommend changing the name to something like award_allocations.
<select name="award_allocations[<?=$awardcode?>][<?=$i?>]">
Dump $_POST to check it is as you want. You can iterate through your allocations something like this:
if(isset($_POST['submit']))
{
foreach($_POST['award_allocations'] as $award_code => $student_codes) {
printf("Award code: %s allocated to Student codes: %s\n",
$award_code,
implode(', ', $student_codes)
);
}
$lock = isset($_POST['lock']) ? true : false;
}