I am not sure whether this is possible or not, but I would really like to know if SQL can solve this issue, or I should continue using PHP to handle it.
I have a table that contains information from a form. The setup is made so that the submission column identifies the form entry, the field column represents an input field's name attribute and then data is the posted information.
It looks like this:
+---------------------------------------------------------------+
|id |submission|ref |field |data |
+---------------------------------------------------------------+
|1 |1 |hox23 |name |John Doe |
+---------------------------------------------------------------+
|2 |1 |hox23 |address |Sesame Street 12 |
+---------------------------------------------------------------+
|3 |1 |hox23 |phone |5555-1234 |
+---------------------------------------------------------------+
|4 |1 |hox23 |email |[email protected] |
+---------------------------------------------------------------+
|5 |2 |hox23 |name |Josh Smith |
+---------------------------------------------------------------+
|6 |2 |hox23 |address |Any Street 34 |
+---------------------------------------------------------------+
|7 |2 |hox23 |phone |5555-5678 |
+---------------------------------------------------------------+
|8 |2 |hox23 |email |[email protected] |
+---------------------------------------------------------------+
|9 |3 |hox23 |name |Jane Summer |
+---------------------------------------------------------------+
|10 |3 |hox23 |address |Last Street 4 |
+---------------------------------------------------------------+
|11 |3 |hox23 |phone |5555-9012 |
+---------------------------------------------------------------+
|12 |3 |hox23 |email |[email protected] |
+---------------------------------------------------------------+
|13 |4 |hox23 |name |Patrick Thom |
+---------------------------------------------------------------+
|14 |4 |hox23 |website |www.thom.ex |
+---------------------------------------------------------------+
|15 |4 |hox23 |phone |555-1235 |
+---------------------------------------------------------------+
|16 |4 |hox23 |email |[email protected] |
+---------------------------------------------------------------+
|17 |5 |hox23 |name |Hillary Good |
+---------------------------------------------------------------+
|18 |5 |hox23 |website |www.good.ex |
+---------------------------------------------------------------+
|19 |5 |hox23 |phone |5555-8365 |
+---------------------------------------------------------------+
|20 |5 |hox23 |email |[email protected] |
+---------------------------------------------------------------+
|21 |6 |hox23 |name |Toby Chalk |
+---------------------------------------------------------------+
|22 |6 |hox23 |email |[email protected] |
+---------------------------------------------------------------+
|23 |6 |hox23 |website |www.chalk.ex |
+---------------------------------------------------------------+
|24 |7 |hox23 |name |Kat Buo |
+---------------------------------------------------------------+
|25 |7 |hox23 |email |[email protected] |
+---------------------------------------------------------------+
|26 |7 |hox23 |website |www.buo.ex |
+---------------------------------------------------------------+
|27 |8 |hox23 |name |Mill Green |
+---------------------------------------------------------------+
|28 |8 |hox23 |email |[email protected] |
+---------------------------------------------------------------+
|29 |8 |hox23 |website |www.green.ex |
+---------------------------------------------------------------+
|30 |9 |hox23 |phone |555-6123 |
+---------------------------------------------------------------+
|31 |9 |hox23 |address |Some other place 7 |
+---------------------------------------------------------------+
|32 |9 |hox23 |name |Carl Stuff |
+---------------------------------------------------------------+
As you can see, there are not the same amount of lines per entry, nor is there the same order or the same fields. Currently my PHP script fetch the table content, creates a KEY array based on the different field variables. Then I loop through all the entries again to merge them into another array to end up with a final array containing a row for each submission with data per field.
What I would like to know, is if SQL (currently I am using MySQL) can do this for me. Is there a way to make a select statement that is so dynamic that it can output a table like this:
+---------------------------------------------------------------------------------+
|submission|name |address |phone |email |website |
+---------------------------------------------------------------------------------+
|1 |John Doe |Sesame Street 12 |5555-1234|[email protected] |- |
+---------------------------------------------------------------------------------+
|2 |Josh Smith |Any Street 34 |5555-5678|[email protected] |- |
+---------------------------------------------------------------------------------+
|3 |Jane Summer |Last Street 4 |5555-9012|[email protected] |- |
+---------------------------------------------------------------------------------+
|4 |Patrick Thom|- |555-1235 |[email protected]|www.thom.ex |
+---------------------------------------------------------------------------------+
|5 |Hillary Good|- |5555-8365|[email protected]|www.good.ex |
+---------------------------------------------------------------------------------+
|6 |Toby Chalk |- |- |[email protected] |www.chalk.ex|
+---------------------------------------------------------------------------------+
|7 |Kat Buo |- |- |[email protected] |www.buo.ex |
+---------------------------------------------------------------------------------+
|8 |Mill Green |- |- |[email protected] |www.green.ex|
+---------------------------------------------------------------------------------+
|9 |Carl Stuff |Some other place 7|555-6123 |- |- |
+---------------------------------------------------------------------------------+
My current select statement are looking for ref as it's identifier. I only want to fetch all the entries connected with this id. Note that the column field is not consistent, and you don't know if there is 3 or 4 lines per submission id. You don't know if the name field is the first or the last row per entry, or if the name field exists at all. I myself have found it very difficult to figure this out, but I would really like to know, if there is a way for SQL to manage this setup?
EDIT
I have at least reduced my PHP script a lot. Currently it looks like this:
<?php
$conn = new PDO(
'mysql:host=localhost;dbname=data_loop',
"username",
"password",
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
// Reference variable. Can be a posted variable to look up.
$ref = 'hox23';
$sql = $conn->prepare('SELECT * FROM data_loop WHERE ref = :ref GROUP BY field');
$sql->bindParam(':ref', $ref, PDO::PARAM_STR);
$sql->execute();
$res = $sql->fetchAll(PDO::FETCH_ASSOC);
$que = 'SELECT A.submission';
$ry = 'FROM data_loop AS A ';
$i = 'B';
foreach ($res as $key => $value) {
$que .= ', '.$i.'.Data AS '.$value['field'].' ';
$ry .= 'LEFT JOIN data_loop AS '.$i . ' ON A.submission = '.$i.'.submission '. 'AND '.$i.".field = '".$value['field']."' ";
$i++;
}
$query = $que.$ry."WHERE A.ref = '".$ref."'";
$stmt = $conn->prepare($query);
$stmt->execute();
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($row as $key => $value) {
echo $value['name'] . '<br>' . $value['address'] . '<br>' . $value['email'] . '<br>' . $value['phone'] . '<br>' . $value['website'] . '<br>';
}
?>
Hope it can help someone else out there.