1

The below-provided code is used to fill an array "data".

$query1="SELECT * FROM tab1, tab2 WHERE tab1.column1=tab2.column2;";
    $result1=DatabaseConnector::ExecuteQueryArray($query1);
    $data = array();
    $i = 0;
    foreach ($result1 as $row):
        $data[] = array(
            array($i,array("xxx",' EE112',$row['column3'],'FT445'),"2004-03-01 10:00","2004-03-01 14:00"));
        $i++;
    endforeach;

When I try reading data from the array, the error "Undefined offset: 1" occurs. The funny thing is that when I filled "data" array using $data = and not $data[] =, there was no error, just the last row was filled. The error is produced by line $bar = new GanttBar(..). I tried to substitute $row['column3'] by some string "xxx", but there was the same error.

for($i=0; $i<count($data); ++$i) {
        $bar = new GanttBar($data[$i][0],$data[$i][1],$data[$i][2],$data[$i][3]);
        $graph->Add($bar);
    }
3
  • var_dump that array and you'll see... Commented May 17, 2012 at 9:21
  • I added the line var_dump($data); I'm using Zend Studio. Where can I see the output of var_dump? Sorry I'm newbie. I tried Debug Mode and also looked at Console. There is no message there. Commented May 17, 2012 at 9:30
  • Run the code and you're supposed to see what $data looks like. Then compare that with the way you iterate over its elements. Commented May 17, 2012 at 10:58

2 Answers 2

3

Isn't

$data[] = array("xxx",' EE112',$row['column3'],'FT445',"2004-03-01 10:00","2004-03-01 14:00");

just enough, data structure should be as simple as possible.

Ok, if you want that structure.

$data[] = array(array("xxx",' EE112',$row['column3'],'FT445'),"2004-03-01 10:00","2004-03-01 14:00");

Then

foreach($data as $i => $var) {
    $bar = new GanttBar($i, $var[0], $var[1], $var[2]);
    $graph->Add($bar);
}
Sign up to request clarification or add additional context in comments.

5 Comments

No, I'm working with jpGraph Gantt Chart and it requires specific input format. Well, if I manually fill the array "data", then everything works fine.
The second bit of code does not work with the third one - $var will be again an array containing an array. Use the first way to fill $data with the loop at the end.
@Niko, He wants $var[0] to be an array.
Ups, both your solution and mine are fine if I do $data[] = array($i,array(... The error was caused by $data[] = array(array($i,array(...
@KlausosKlausos Why do you need include $i in the $data, you could just use the $i in the second loop.
1

I think there is a very basic problem:

The foreach loop is creating a new subarray in $data for every row. But later on your code is trying to get three rows from each entry in $data - which never has been created. Therefore $data[$i][1] is always empty.

You might need to adjust your sql query to receive the data in the correct format from the beginning.

2 Comments

The problem is that the following code also is not fine: $bar = new GanttBar($i,"xxx","2010-10-10 10:05","2010-10-10 11:00"); Maybe there is the problem with count($data)? I don't understand why this code works for manually created "data" array: $data = array(array(...),array(...),array(...));
Show us an example of your manually created data and what you do with it. Then also show us what print_r($row) contains.

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.