-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)
7
  • 1
    Where does $value come from and what does it contain? Commented Sep 25, 2017 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. Commented Sep 25, 2017 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? Commented Sep 25, 2017 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. Commented Sep 25, 2017 at 16:14
  • @AdamAzad I updated the question Commented Sep 25, 2017 at 16:17

1 Answer 1

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;

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.