-1

I have been writing a code snippet of PHP in HTML which iterates an array inside another array (data from mysql). But when I run the code, it gives the following two errors.

Undefined variable Array to string conversion in ...... Following is my code.

$sql = "SELECT * FROM person";
$result = mysqli_query($conn, $sql);
$resultDataSet = array();
if (mysqli_num_rows($result) > 0) {

while ($row = mysqli_fetch_array($result)) {
    array_push($resultDataSet, $row);
}
if ($type == "HTML") {
    $htmlSerialize = "
        <table>
            <tr>
                <th>Name</th>
                <th>Age</th>
                <th>City</th>
            </tr>
            <?php foreach($resultDataSet as $value): ?>
             <tr>
                <th><?php=?".$value['Name']." ?></th>
                <th><?php=?".$value['Age']." ?></th>
                <th><?php=?".$value['City']." ?></th>
            </tr>
            <?php endforeach; ?>
        </table>";


    echo $htmlSerialize;

}

Also following are the errors.

enter image description here

What is the error I have made? How can I solve this?

Edited

Following is the var dump of $resultDataSet

array (size=2)
  0 => 
    array (size=8)
      0 => string '1' (length=1)
      'ID' => string '1' (length=1)
      1 => string 'Akila' (length=5)
      'Name' => string 'Akila' (length=5)
      2 => string '22' (length=2)
      'Age' => string '22' (length=2)
      3 => string 'Mount Lavinia' (length=13)
      'City' => string 'Mount Lavinia' (length=13)
  1 => 
    array (size=8)
      0 => string '2' (length=1)
      'ID' => string '2' (length=1)
      1 => string 'Randil' (length=6)
      'Name' => string 'Randil' (length=6)
      2 => string '23' (length=2)
      'Age' => string '23' (length=2)
      3 => string 'Colombo' (length=7)
      'City' => string 'Colombo' (length=7)
  • 1
    Where does `$value` come from and what does it contain? – M. Eriksson Sep 25 '17 at 16:11
  • Hard to tell what the query is returning. See the output of `var_dump($resultDataSet)`. It could be the columns names are all lowercase. – Adam Azad Sep 25 '17 at 16:12
  • @magnus I am a beginner in PHP. Isn't '$value' the object that will be assigned by the for loop for each iteration? – Kapila Hettiarachchi Sep 25 '17 at 16:13
  • 1. I don't see you have that code inside any loop. You have one loop in this code, the `while`-loop that you're closing before you're using `$value`. 2. The variable name will be what you set it to be. I would recommend spending some time going through some basic PHP-tutorials and reading the manual. – M. Eriksson Sep 25 '17 at 16:14
  • @AdamAzad I updated the question – Kapila Hettiarachchi Sep 25 '17 at 16:17
  • @MagnusEriksson I got the code from [this](https://stackoverflow.com/questions/10258345/php-simple-foreach-loop-with-html) – Kapila Hettiarachchi Sep 25 '17 at 16:20
  • That code is using `foreach($array as $key=>$value)`. That is a loop that assigns the array key to the variable `$key` and the value to the variable `$value`. Those variables can be set to anything you like. But for it to be a loop like that, you need `foreach()` (as that is the construct that actually performs the loop and sets the variables). http://php.net/manual/en/control-structures.foreach.php – M. Eriksson Sep 25 '17 at 16:23

1 Answers1

3

The error reports that value is undefined. PHP does not parse nor compile code wrapped by quotes -- strings. Try this where I split the loop from the HTML output.

if ($type == "HTML") {

   // Start by opening the table and header
   $htmlSerialize = '
        <table>
            <tr>
                <th>Name</th>
                <th>Age</th>
                <th>City</th>
            </tr>';

    // loop over the results and append each row to $htmlSerialize
    foreach($resultDataSet as $value):

        $htmlSerialize .= '
                 <tr>
                    <th>'. $value['Name'] .'</th>
                    <th>'. $value['Age'] .'</th>
                    <th>'. $value['City'] .'</th>
                </tr>';

    endforeach;

    // close the table
    $htmlSerialize .= '</table>';

    // flush results
    echo $htmlSerialize;

}
Adam Azad
  • 11,171
  • 5
  • 29
  • 70