I have the follwing code which was written to generate an array and send it back to my main page and append it to an existing table.
I am just woundering if there is any possible way to send the data generated from the below code as a single element within a JSON object.
For example:
{"arraydata":"Data Generated From The below Code","another variable":"some other data"}
An so on...
Any Suggestions??
$result = mysqli_query($con," SELECT * FROM `BIMTECH_academy_2016_classes`
WHERE `Serial`='$serial' ORDER BY `Serial` ");
while($row = mysqli_fetch_array($result))
{
echo "<tr id='" . $row['Serial'] . "'>";
echo "<td><img src='images/delete-icon.svg' id='classDeleteIcon'/></td>";
echo "<td class='datepicker' id='Date'>" . $row['Date'] . "</td>";
echo "<td class='timepicker' id='From'>" . $row['From'] . "</td>";
echo "<td class='timepicker' id='To'>" . $row['To'] . "</td>";
echo "</tr>";
}
Declare a variable ($html) before the while loop. Append/concat all data (html and $rows values) to $html variable. Create a array with different keys and values, one key contains $html variable as value.
$html = ''; // a variable
while($row = mysqli_fetch_array($result))
{ // concat all data to $html
$html.="<tr id='" . $row['Serial'] . "'>";
$html.="<td><img src='images/delete-icon.svg' id='classDeleteIcon'/></td>";
$html.="<td class='datepicker' id='Date'>" . $row['Date'] . "</td>";
$html.="<td class='timepicker' id='From'>" . $row['From'] . "</td>";
$html.="<td class='timepicker' id='To'>" . $row['To'] . "</td>";
$html.="</tr>";
}
$data_array = array(); // declare array
$data_array['arraydata'] = $html; // assign to array key
$data_array['otherdata'] = 'otherdata'; // other data to the array
$json_data = json_encode($data_array, JSON_HEX_QUOT | JSON_HEX_TAG); // encode array with html tags
echo $json_data;
Output:
{"arraydata":" your html data","otherdata":"otherdata"}