Как сохранить входное значение в многомерный массив php - PullRequest
0 голосов
/ 16 июня 2020

Когда я запускаю свой код, появляется сообщение

Неустранимая ошибка: невозможно использовать временное выражение в контексте записи в C: \ xampp \ htdocs \ PA2 \ PracticalAssignment2. php в строке 173

Вот мой код:

<body>
<form action="practical assignment2.php" methos="POST">
<table class="Table" id='STable'>
<tr>
<th rowspan='2'>No</th>
<th colspan='2'>Student</th>
<th colspan='5'>Subject's Score</th>
<th rowspan='2'>Total</th>
<th rowspan='2'>Average</th>
</tr>
<tr>
<td id='SId'>ID</td>
<td id='Sname'>Name</td>
<td id='Eng'>English</td>
<td id='Math'>Mathematic</td>
<td id='His'>History</td>
<td id='Sc'>Science</td>
<td id='Phy'>Physics</td>
</tr>
<tr id='Sdata' onmouseover="this.style.backgroundColor='yellow';" onmouseout="this.style.backgroundColor='white';">
<td id="Number" value="1">1.</td>
<td><input type="number" id='idS' name="id[]"  min="1" onmouseover="this.style.backgroundColor='#CCFFFF';" onmouseout="this.style.backgroundColor='white';"></td>
<td><input type="text" id="nameS" name="name[]" onmouseover="this.style.backgroundColor='#CCFFFF';" onmouseout="this.style.backgroundColor='white';"></td>
<td><input type="number" id='EngS' name="eng[]" min="1"  max="100" onmouseover="this.style.backgroundColor='#CCFFFF';" onmouseout="this.style.backgroundColor='white';"></td>
<td><input type="number" id='MathS' name="marh[]" min="1" max="100" onmouseover="this.style.backgroundColor='#CCFFFF';" onmouseout="this.style.backgroundColor='white';"></td>
<td><input type="number" id='HisS' name="his[]" min="1"  max="100" onmouseover="this.style.backgroundColor='#CCFFFF';" onmouseout="this.style.backgroundColor='white';"></td>
<td><input type="number" id='ScS' name="sc[]" min="1"  max="100" onmouseover="this.style.backgroundColor='#CCFFFF';" onmouseout="this.style.backgroundColor='white';"></td>
<td><input type="number" id='PhyS'  name="phy[]" min="1"  max="100"onmouseover="this.style.backgroundColor='#CCFFFF';" onmouseout="this.style.backgroundColor='white';"></td>
<td><input type="number" id='totS' name="total[]" min="1" readonly></td>
<td><input type="number" id='avgS' name="average[]"  min="1" max="100" readonly></td>
</tr>
<tr>
<td colspan='10' id="but"><button id="add_row" onclick="AllAction()">Submit</button></td>
</tr>
</table>

Это часть для меня, чтобы сохранить входное значение из таблицы выше в многомерный массив:

<?php
    // add item to array and display in text file
    $studentsArray = [
            'StudentID' => &_POST['id'],
            'StudentName' => &_POST['name'],
            'SubjectEnglish' => &_POST['eng'],
            'SubjectMaths' => &_POST['math'],
            'SubjectHistory' => &_POST['his'],
            'SubjectSciences' => &_POST['sc'],
            'SubjectPhysics' => &_POST['phy'],
            'ScoreTotal' => &_POST['total'],
            'ScaoreAverage' => &_POST['average']];

    $file = 'studentscore.txt';
    $fh = fopen($file, 'w') or die("can't open file");  
    fwrite($fh, print_r($studentsArray,true));
    fclose($fh);


    ?>

1 Ответ

0 голосов
/ 16 июня 2020

Я думаю, вам следует изучить структурированные данные, которые должны быть сохранены и проанализированы позже, например XML или, что еще проще: https://www.php.net/manual/de/function.json-encode.php

Print_r предназначен для вывода , json_encode генерирует строку, которую можно сохранить в файл. json_decode может прочитать эту строку и снова создать массив PHP.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...