please help me cause I am totally lost on this one.
I am trying to fetch the results from a database that has the data inserted in a weird way. The IDs are not unique, thus I have multiple values with the same ID in the ID column.
I am trying to group these IDs and output the Values for those IDs on a single Row. Right now my code outputs the values aligned in a column associated with their ID. The database was generated by this by one of the client's scripts and it is a huge (limited only up to 40 results in the example page).
So how can I make the results output like this per row:
ID - Name - Surname - Email - Phone - etc
This are the current results of my code: http://jobselection.ro/DB/testing.php (if the page does not load the client's server might be acting up, so try a few refreshes)
This is the code I am using:
<?php
echo "<table style='border: solid 1px black;'>";
echo "<tr><th>Id</th><th>Task</th><th>Type</th><th>Value</th><th>Name</th><th>Surname</th><th>Email</th><th>Phone</th><th>.............</th></tr>";
class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}
function current() {
return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
}
function beginChildren() {
echo "<tr>";
}
function endChildren() {
echo "</tr>" . "\n";
}
}
$servername = "";
$username = "";
$password = "";
$dbname = "";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT submit_time, form_name, field_name, field_value FROM wp_cf7dbplugin_submits LIMIT 40");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?>