0

php foreach echo prints "Array" as value asks this question but thats not the case here

     try {require_once'libs/config.php';
    $con = new PDO($dsn, $user, $pass, $opt);
  $query = "SELECT * FROM table ";
  //first pass just gets the column names
  print "<table> \n";
  $result = $con->query($query);
  //return only the first row (we only need field names)
  $row = $result->fetch(PDO::FETCH_ASSOC);
  print "<thead> <tr> \n";
  foreach ($row as $field => $value){
   print " <th>$field</th> \n";
  } // end foreach
  print " </tr> </thead> <tbobdy> \n";
  //second query gets the data 
  $data = $con->query($query);
  $data->setFetchMode(PDO::FETCH_ASSOC);
  foreach($data as $row){

echo $row; // prints Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array

     print " <tr> \n";
      foreach ($row as $name=>$value){
           print "<td>$value</td>\n";       }
     } // end field loop
   print " </tr>  \n";
  } // end record loop
 // print "</tbody> </table> \n";
  } catch(PDOException $e) {
   echo 'ERROR: ' . $e->getMessage();
  } // end try
$con = null;
 ?>

3 Answers 3

1

So simple that means $row is an array not an variable. You need to do print_r($row); instead of echo $row;

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

Comments

0

You need to print $value in the foreach loop as it contains the values of the array elements.

Your final output will be something like this:

<?php
try {
    require_once 'libs/config.php';
    $con   = new PDO($dsn, $user, $pass, $opt);
    $query = "SELECT * FROM table ";
    //first pass just gets the column names
    print "<table> \n";
    $result = $con->query($query);
    //return only the first row (we only need field names)
    $row    = $result->fetch(PDO::FETCH_ASSOC);
    print "<thead> <tr> \n";
    foreach ($row as $field => $value) {
        print " <th>$value</th> \n";
    } // end foreach
    print " </tr> </thead> <tbobdy> \n";
    //second query gets the data 
    $data = $con->query($query);
    $data->setFetchMode(PDO::FETCH_ASSOC);
    foreach ($data as $Somefield => $SomeValue) {
        echo $SomeValue; // prints your disered output
        print " <tr> \n";
        foreach ($row as $name => $value) {
            print "<td>$value</td>\n";
        }
    } // end field loop
    print " </tr>  \n";
} // end record loop
// print "</tbody> </table> \n";
catch (PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
} // end try
$con = null;
?>
?>

5 Comments

you are right if you look a drop further you can see ` foreach ($row as $name=>$value){ print "<td>$value</td>\n"; } } // end field loop` i'm trying to understand how arrays work so i added this echo $data to what its defined, all i get is array
are you looking for this? foreach ($data as $Somefield => $SomeValue) { //echo $SomeValue; // prints your disered output print " <tr> \n"; foreach ($SomeValue as $name => $value) { print "<td>$value</td>\n"; } } // end field loop
Possibly, you can handle it this way: foreach ($data as $Somefield) { //echo $SomeValue; // prints your desired output print " <tr> \n"; foreach ($Somefield as $name => $value) { print "<td>$value</td>\n"; } } // end field loop
Please add your array elements with some data so that can be easily resolved. Based on your problem that can be possible fixed with above comments and results.
in my original code i have no errors i'm just trying to learn what everything means and does i'm a newbie, my understanding is that $data as $value returns $value=content, and data as $key=>$value returns the $key= column-name and the $value = content i was expecting $data as $row should return content only
0

i think i got it foreach as .. could not echo a full array its either key or value or both, if it's a array it prints 'array' overhere since $data contains a array the full row it just prints 'array', which is true by every multi-dimensional array as you can see the following example from the php docs

    /* foreach example 4: multi-dimensional arrays */
$a = array();
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";

foreach ($a as $v1) {
    foreach ($v1 as $v2) {
        echo "$v2\n";
    }
}

the result from a database is a multi-dimensional array

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.