[Solved] column based mysql results in php [closed]


assuming you have a mysql table setup like this

https://www.dropbox.com/s/cth0mkfwt382z0g/Screenshot%202014-08-20%2016.27.48.png

and you have your php files setup to db properly

  //query database for data
  $result = mysql_query("SELECT question, answer, ip FROM test_answers");

  //Create some arrays
  $answer_array = array();
  $ip_array = array();
  $question_array = array();

  //loop through the mysql data and populate the arrays
  while ($row = mysql_fetch_object($result)) {
    $answer_array[$row->ip][$row->question] = $row->answer;
    $ip_array[] = $row->ip;
    $question_array[] = $row->question;
  }

  //remove dups
  $ip_array = array_unique($ip_array);
  $question_array = array_unique($question_array);

  //print out the view
  echo "<table>";
  //heading row
  echo "<tr><td>question</td>";
  foreach($ip_array as $ip) {
    echo "<td>$ip</td>";
  }
  echo "</tr>";
  //answer rows
  foreach($question_array as $question) {
    echo "<tr><td>$question</td>";
      foreach($ip_array as $ip) {
         echo "<td>" . $answer_array[$ip][$question] . "</td>";
      }
    echo "</tr>";
  }
  echo "</table>";

then you’ll end up with this

https://www.dropbox.com/s/yhuy5br3p1o3npg/Screenshot%202014-08-20%2016.31.06.png

1

solved column based mysql results in php [closed]