0

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>";
     }
Mazen
  • 171
  • 4
  • 14
  • 1
    Look into [json_encode()](http://php.net/manual/en/function.json-encode.php) – mferly Jun 15 '16 at 20:57
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Jun 15 '16 at 20:57
  • Declare an empty array before the `while` loop. Keep appending `$row`s, *another variable*, *some other data* and finally `json_encode()` the array. – Rajdeep Paul Jun 15 '16 at 20:57
  • you have a form in your code, right ? – Istiaque Ahmed Jun 15 '16 at 22:05
  • No, it's an AJAX request, and I want to pass the PHP result to its `success` function – Mazen Jun 15 '16 at 22:32

1 Answers1

0

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"}
Ravinder Reddy
  • 3,869
  • 1
  • 13
  • 22