Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

The PHP & SQL

            list( $since, $start_index, $count ) = func_get_args();
            $query =    "SELECT 
                            size_groups.id, size_groups.name, GROUP_CONCAT( size_values.names SEPARATOR ';' ) AS `display_name`, GROUP_CONCAT( size_values.vals SEPARATOR ';' ) AS `values`, GROUP_CONCAT( size_count.count SEPARATOR ';' ) AS `count`
                        FROM size_groups
                        LEFT JOIN
                            (SELECT CONCAT_WS( ', ', sizes.size ) AS names, CONCAT_WS( ', ', sizes.id ) AS vals, sizes.id AS ids, sizes.group_id FROM sizes)
                            AS size_values ON size_values.group_id = size_groups.id
                        LEFT JOIN
                            (SELECT CONCAT_WS( ', ', COUNT( product_to_sizes.size_id ) ) AS count, product_to_sizes.size_id FROM product_to_sizes)
                            AS size_count ON size_count.size_id = size_values.ids
                        WHERE size_groups.timestamp >= '%s' AND id = %s
                        GROUP BY size_groups.id
                        ORDER BY size_groups.timestamp ASC LIMIT %s, %s";

            return $this->result( $this->connection->query( $query, $since, $category_id, $start_index, $count ) );

And

   public function result( $data ) {

        $result = array();
        while ($row = $data->fetch_assoc())
            array_push( $result, $row ); 

        return $result;

    }

outputs an array like this

 Array
 (
    [0] => Array
        (
            [id] => 21
            [name] => Sleeve length
            [display_name] => 33;34;35;36;37;38
            [values] => 94;95;96;97;98;99
            [count] => 
        )

    [1] => Array
        (
            [id] => 22
            [name] => Waist
            [display_name] => 40;30;32;34;36;38
            [values] => 105;100;101;102;103;104
            [count] => 
        )
 )

but I want it to be like this

Array
(
    [0] => Array
        (
            [id] => 21
            [name] => Sleeve length
            [values] => Array(
                  Array(
                          [display_name] => 33
                          [value] => 94
                          [count] => 5
                       ),
                  Array(
                          [display_name] => 34
                          [value] => 95
                          [count] => 31
                       )
               )
        ),
        [1] => Array
             (
               etc
             )
)

Table structure for sizes

+-----+--------+----------+----------+---------------------+
| id  | size   | alt_size | group_id | timestamp           |
+-----+--------+----------+----------+---------------------+
|  81 | Small  |          |       19 | 2011-07-15 17:26:01 |
|  82 | Medium |          |       19 | 2011-07-15 17:26:10 |
|  83 | Large  |          |       19 | 2011-07-15 17:26:14 |
|  84 | 14.5   |          |       20 | 2011-07-15 17:28:01 |
|  85 | 15     |          |       20 | 2011-07-15 17:28:05 |
|  86 | 15.5   |          |       20 | 2011-07-15 17:28:08 |
|  87 | 16     |          |       20 | 2011-07-15 17:28:13 |
|  88 | 16.5   |          |       20 | 2011-07-15 17:28:18 |
|  89 | 17     |          |       20 | 2011-07-15 17:28:21 |
|  90 | 17.5   |          |       20 | 2011-07-15 17:28:24 |
|  91 | 18     |          |       20 | 2011-07-15 17:28:27 |
|  92 | 19     |          |       20 | 2011-07-15 17:28:32 |
|  93 | 20     |          |       20 | 2011-07-15 17:28:36 |
|  94 | 33     |          |       21 | 2011-07-15 17:28:49 |
|  95 | 34     |          |       21 | 2011-07-15 17:28:54 |
|  96 | 35     |          |       21 | 2011-07-15 17:28:56 |
|  97 | 36     |          |       21 | 2011-07-15 17:29:00 |
|  98 | 37     |          |       21 | 2011-07-15 17:29:05 |
|  99 | 38     |          |       21 | 2011-07-15 17:29:15 |
| 100 | 30     |          |       22 | 2011-07-15 17:29:42 |
| 101 | 32     |          |       22 | 2011-07-15 17:29:46 |
| 102 | 34     |          |       22 | 2011-07-15 17:29:51 |
| 103 | 36     |          |       22 | 2011-07-15 17:30:03 |
| 104 | 38     |          |       22 | 2011-07-15 17:30:06 |
| 105 | 40     |          |       22 | 2011-07-15 17:30:10 |
| 106 | 32     |          |       23 | 2011-07-15 17:30:52 |
| 107 | 34     |          |       23 | 2011-07-15 17:30:56 |
| 108 | 36     |          |       23 | 2011-07-15 17:31:00 |
| 109 | 38     |          |       23 | 2011-07-15 17:31:04 |
+-----+--------+----------+----------+---------------------+

For size_groups

+----+---------------+---------------------+
| id | name          | timestamp           |
+----+---------------+---------------------+
| 19 | Size          | 2011-07-15 17:25:45 |
| 20 | Collar size   | 2011-07-15 17:26:50 |
| 21 | Sleeve length | 2011-07-15 17:27:00 |
| 22 | Waist         | 2011-07-15 17:27:28 |
| 23 | Leg           | 2011-07-15 17:27:38 |
+----+---------------+---------------------+

And products_to_sizes

+------+------------+---------+-------------+----------+---------------------+
| id   | product_id | size_id | stock_level | theorder | timestamp           |
+------+------------+---------+-------------+----------+---------------------+
| 8050 |        683 |     109 |           0 |        0 | 2011-07-16 10:45:30 |
| 8049 |        683 |     108 |           0 |        0 | 2011-07-16 10:45:30 |
| 8048 |        683 |     107 |           0 |        0 | 2011-07-16 10:45:30 |
| 8099 |        679 |     109 |           0 |        0 | 2011-07-16 10:48:30 |
| 8098 |        679 |     108 |           0 |        0 | 2011-07-16 10:48:30 |
| 8097 |        679 |     107 |           0 |        0 | 2011-07-16 10:48:30 |
| 8096 |        679 |     106 |           0 |        0 | 2011-07-16 10:48:30 |
| 8095 |        679 |     105 |           0 |        0 | 2011-07-16 10:48:30 |
| 8094 |        679 |     104 |           0 |        0 | 2011-07-16 10:48:30 |
| 8093 |        679 |     103 |           0 |        0 | 2011-07-16 10:48:30 |
+------+------------+---------+-------------+----------+---------------------+

Any ideas?

share|improve this question
    
You do know that without custom looping and constructing of the array you won't be able to have the output the way you described it? –  N.B. Oct 20 '11 at 11:02
    
can you post your php? –  Herb Oct 20 '11 at 11:04
    
@Herb PHP added –  Max Rose-Collins Oct 20 '11 at 11:09
add comment

1 Answer

up vote 1 down vote accepted

Easy enough to fix via PHP. Assuming $items contains your records array ...

foreach ($items as $itemkey=>$item) {
    if ($item['display_name']) {

        // prep vars
        $displaynames = explode(';',$item['display_name']);
        $values = explode(';',$item['values']);
        $count = explode(';',$item['count']);
        unset($items[$itemkey]['values']);

        // iterate
        $i=0;
        foreach ($displaynames as $null) {
            $items[$itemkey]['values'][$i] = array(
                'display_name' => $displaynames[$i],
                'value'        => $values[$i],
                'count'        => $count[$i]
            );
            $i++;
        }

    } else {
        $items[$itemkey]['values'] = null;
    }
}
share|improve this answer
2  
Or better yet, show me the table syntax and sample data for the tables sizes, size_groups, size_values and size_count. Perhaps there is a MySQL-only way. –  neokio Oct 20 '11 at 12:18
    
i've added the structure –  Max Rose-Collins Oct 20 '11 at 16:08
    
Cool, I gave it a go. Thought for sure the current MySQL query would be a bear, but it's surprisingly efficient. So I'd leave that as is, and "fix it in post" with the above PHP, doing a FOREACH on the $result, which will iterate the array and rebuild it to your requirements. Usually it's best to nail your MySQL queries, but sometimes PHP is the right tool for the job :) –  neokio Oct 21 '11 at 5:59
    
Thanks man, great help :) –  Max Rose-Collins Oct 21 '11 at 10:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.