2

my database table contained the folowing data
____________________________________
|name | start_date  | end_date      | class       |
================================
|john   | 2015-01-01 | 2015-01-01  | urgent     |
|mary | 2015-01-12 | 2015-01-15  | important |
|peter | 2015-01-19 | 2015-01-20  | important |
|john  | 2015-01-26 | 2015-01-28   | important |
=================================

and these is my sql code in fetching the data

$sql="
SELECT `name`,`start_date`,`end_date`,`class`,
       TIMESTAMPDIFF(DAY,start_date,end_date)+1 AS no_days
     FROM `schedule`"

$result=mysql_query($sql);
     while($row=mysql_fetch_assoc($result)){
        $data[] = array(
             'label' => $row["name"],
             'start' => $row["start_date"],
             'no_days'=> $row["no_days"],
             'class' => $row["class"],
        );
     }


class Gantti{
    var $data = array();
    var $blocks = array();
    var $block = array();
    // the rest of gantti class code is for table format so i didnt include it in here
}


 function parse(){
   foreach($this->data as $d){
        $this->blocks[$d['label']][]=array(
             'label' => $d['label'],
             'start'=>$start=($d['start']),
             'no_days'=>$no_days=$d['no_days'],
             'class'=>$d['class']);
     }
  }
  var_dump($this->blocks);

after i dump $this->block, it gave me the following arrangement of array

 array()
    john=>
      array()
        0=>
          array()
            'label' => string 'john'
            'start' => string '2015-01-01'
            'no_days'=> string '1'
            'class' => string 'urgent'
        1=>
          array()
            'label' => string 'john'
            'start' => string '2015-01-26'
            'no_days'=> string '3'
            'class' => string 'important'
    mary=>
      array()
        0=>
          array()
            'label' => string 'mary'
            'start' => string '2015-01-12'
            'no_days'=> string '4'
            'class' => string 'important'
    peter=>
      array()
        0=>
          array()
            'label' => string 'peter'
            'start' => string '2015-01-19'
            'no_days'=> string '2'
            'class' => string 'important'

but my intended output is like this

 array()
   0=>
      array()
          'label' => string 'john'
          'start' =>
              array()
                  0=> string '2015-01-01'
                  1=> string '2015-01-26'
          'no_days' =>
              array()
                  0=> string '1'
                  1=> string '3'
          'class' =>
              array()
                  0=> string 'urgent'
                  1=> string 'important'
   1=>
      array()
          'label' => string 'mary'
          'start' =>
              array()
                  0=> string '2015-01-12'
          'no_days' =>
              array()
                  0=> string '4'
          'class' =>
              array()
                  0=> string 'important'
   2=>
      array()
          'label' => string 'peter'
          'start' =>
              array()
                  0=> string '2015-01-19'
          'no_days' =>
              array()
                  0=> string '2'
          'class' =>
              array()
                  0=> string 'important'

How can i combine same label value to get my intended output?

3
  • And you are still using mysql functions, why? They have even deprecated for a while now, which means all new code should not be using it... Commented May 27, 2015 at 7:18
  • @WadeShuler it's kind of long story.. but after i get the answer to this question i will stop using mysql.. :-) Commented May 27, 2015 at 7:21
  • I would keep the 'labels' as keys and work from there. You will only complicate things if you want to use an index number as you stated in you intended example. To support the example you want I think you need to loop through the array as in builds up to check of there is an occurrences of john and then add data. To use john as a key would make the code easier. Commented May 27, 2015 at 18:06

1 Answer 1

1

Try to rearrange the array structure by modifying forloop

function parse()
{
    foreach($this->data as $d)
    {
        $this->raw_blocks[$d['label']]['label'] = $d['label'];
        $this->raw_blocks[$d['label']]['start'][] = $d['start'];
        $this->raw_blocks[$d['label']]['no_days'][] = $d['no_days'];
        $this->raw_blocks[$d['label']]['class'][] = $d['class'];
    }

    foreach($this->raw_blocks as $block)
    {
         $this->blocks[] = $block;
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

thank you for the help.. the code you provide gave me the output where john was executed 4 times and mary 2 times and peter once..
No problem :) Depends on the input. Is this the one you provided?
Could you output the result?
the first array look like this one array () 0 => array () 'label' => string 'john' 'start' => array () 0 => string '2015-01-01' 'no_days' => array () 0 => string '1' 'class' => array () 0 => string 'urgent'
the next one is like my intended output already but it apear 3 times 1 => array () 'label' => string 'john' 'start' => array () 0 => string '2015-01-01' 1 => string '2015-01-26' 'no_days' => array 0 => string '1' 1 => string '3' 'class' => array () 0 => string 'urgent' 1 => string 'important'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.