0

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.. :)

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
sansam
  • 57
  • 10
  • You need to show us an example what the array looks like (do a `var_dump($_POST)`), what the expected outcome should be and what you've tried. Remove any code that's not directly connected to the issue at hand. – M. Eriksson Nov 23 '17 at 05:53
  • You are placing a leading space in your student code value. Is this why you trim everything? – Progrock Nov 23 '17 at 06:16

1 Answers1

1

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;
}
Progrock
  • 7,373
  • 1
  • 19
  • 25
  • Hi Progrock, that works perfectly. Thanks a tonne. In that case can I use a INSERT query replacing the printf within the foreach loop? Something like: $query = mysqli_query($conn, "INSERT INTO awardwinner (awardcode, stud_code) VALUES('$award_code', '$student_codes'"); – sansam Nov 23 '17 at 13:45
  • I don't know the format of your database. You'll have to do something with the student_codes as they are in an array format. – Progrock Nov 23 '17 at 16:29
  • Hi Progrock, I managed to insert data into the table. Thanks for your clue. Can you please show the method if it was a 3 dimensional array? – sansam Nov 24 '17 at 01:23