Laurent Laurent - 1 month ago 8
MySQL Question

Split up array into rows

I have a table, with values from

dat_eb_registrants
as rows (e.g.
$row[1]
) and values from a horizontal array, extracted from
dat_eb_field_values
, I'd like to split those up so I can order everything into the table how I want it.

How the data gets put into my table:

$count = 0;
while ($row = mysql_fetch_row($result))
{
echo "<tr>";
$myArray[] ="<tr><td>" . $row[9] . "</td><td>"; echo $myArray[$count];
$count++;
echo "</tr>";
}


How all of the data get extracted from the database (yes, I know it's old):

SELECT dr.id, dr.first_name, dr.last_name, dr.email, dr.comment, dr.amount, dr.published, dr.transaction_id, dr.register_date, GROUP_CONCAT(df.field_value SEPARATOR '</td><td>')
FROM dat_eb_registrants dr
LEFT JOIN dat_eb_field_values df
ON dr.id=df.registrant_id
WHERE `event_id` >= 20 AND `event_id` <= 25
GROUP BY dr.id
ORDER BY '".$sort."', '".$ascdsc."'


Now, I want to put some rows from the first table (e.g.
$row[1]
) and (.eg.
$row[2]
) vertically, in the middle of the array. How can I do this?

Because the array fills my table in one time, and using the
$rows
, you can simply tell which rows to display where...

This is what I want (code might not be correct):

while ($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>"'.$row_table_1[1]'"</td>";
echo "<td>"'.$row_table_1[2]'"</td>";
echo "<td>"'.$row_table_2[35]'"</td>";
echo "<td>"'.$row_table_2[45]'"</td>";
echo "<td>"'.$row_table_1[5]'"</td>";
echo "<td>"'.$row_table_2[6]'"</td>";
echo "</tr>";
}


I used to do:

echo "<td>"; $result24 = mysql_query("SELECT field_id, field_value FROM dat_eb_field_values WHERE (field_id = 88) AND (registrant_id = $row[0])"); $r24 = mysql_fetch_row($result24); echo $r24[1]; echo "</td>";
echo "<td>"; $result25 = mysql_query("SELECT field_id, field_value FROM dat_eb_field_values WHERE (field_id = 57) AND (registrant_id = $row[0])"); $r25 = mysql_fetch_row($result25); echo $r25[1]; echo "</td>";
echo "<td>" . $row[5] . "</td>";
echo "<td>" . $row[6] . "</td>";


I want to do:

echo "<td>" . $rowfromsecondtable[1] . "</td>";
echo "<td>" . $rowfromsecondtable[2] . "</td>";
echo "<td>" . $row[5] . "</td>";
echo "<td>" . $row[6] . "</td>";


(using the code I gave above)

Preview of
dat_eb_registrants
:

| id | first_name | last_name | email |
------------------------------------------------------------------------
| 1 | Mike | Doe | mikedoe@hotmail.com |
| 2 | John | Smith | j_smith@hotmail.com |


Preview of
dat_eb_field_values
:

field 1 = fav.sport
field 2 = fav. color
field 3 = fav. food

| registrant_id | field_id | field_value |
----------------------------------------------------------
| 1 | 1 | tennis |
| 1 | 2 | green |
| 1 | 3 | spagetti |
| 2 | 1 | hockey |
| 2 | 2 | red |
| 2 | 3 | fish |


I need:

first_name | id | fav.sport | last_name | fav.food |
---------------------------------------------------------------------
Mike | 1 | Tennis | Doe | spagetti |
John | 2 | Hockey | Smith | fish |

Answer

Maybe I am missing something but why not just perform this task in SQL. This is basically a pivot. MySQL does not have a pivot but you can use an aggregate function with a CASE statement:

select r.first_name,
  r.id,
  r.last_name,
  max(case when f.field_id =1 then f.field_value else null end) As FavSport,
  max(case when f.field_id =2 then f.field_value else null end) As FavColor,
  max(case when f.field_id =3 then f.field_value else null end) As FavFood
from dat_eb_registrants r
left join dat_eb_field_values f
  on r.id = f.registrant_id
group by r.first_name, r.id, r.last_name
order by r.id

See SQL Fiddle with Demo

The result of the query is the output that you want:

| FIRST_NAME | ID | LAST_NAME | FAVSPORT | FAVCOLOR |  FAVFOOD |
----------------------------------------------------------------
|       Mike |  1 |       Doe |   tennis |    green | spagetti |
|       John |  2 |     Smith |   hockey |      red |     fish |

Or you can use multiple joins on the dat_eb_field_values table:

select r.first_name,
  r.id,
  r.last_name,
  fSport.field_value FavSport,
  fColor.field_value FavColor,
  fFood.field_value FavFood
from dat_eb_registrants r
left join dat_eb_field_values fSport
  on r.id = fSport.registrant_id
  and fSport.field_id = 1
left join dat_eb_field_values fColor
  on r.id = fColor.registrant_id
  and fColor.field_id = 2
left join dat_eb_field_values fFood
  on r.id = fFood.registrant_id
  and fFood.field_id = 3
order by r.id

See SQL Fiddle with Demo. It produces the same result.