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>";
?>

Recommended Answers

All 2 Replies

Member Avatar for diafol

The site is password protected.

It would help if you posted the table schema and included a short example of typical data.

In general, when you have non-uniques and you want to get data per id, you can do an ORDER BY id to group them in a normal output - and identifying a change in ID for the next batch, or you can use GROUP BY clause or you can even use some nifty PDO functions to group by a column:

$stmt->fetchAll(PDO::FETCH_GROUP)

Which will group your output by ID if this is the first column in your SQL.

However, without knowing how your data is stored, guessing is fruitless.

Hello seularts ,
I can't understand the “I am trying to group these IDs and output the Values for those IDs on a single Row” part. If you have multiple rows with the same ID how the other columns of these rows will be grouped to output only one row?
You mentions four fields (columns) submit_time, form_name, field_name, field_value is there another one like e.g. id? If the id is not unique then what it represents and you want to group by it ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.