• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problems with array

#1
[eluser]dunken[/eluser]
Hello.

Can some1 tell me what i am doing wrong. I get duplicates in the second array.

code:
Code:
function get_documents($section){

        $query = $this->db->query("SELECT DISTINCT flow FROM documents WHERE section = '$section' ORDER BY flow ASC");
        foreach($query->result_array() as $row)
        {
            $flowName = $row['flow'];
            
            $query2 = $this->db->query("SELECT * FROM documents WHERE section = '$section' AND flow = '$flowName' ORDER BY date DESC");
            foreach($query2->result_array() as $row2)
            {
                $flows[] = array
                (
                    'date'         => $row2['date'],
                    'user'         => $row2['user'],
                    'header'    => $row2['header'],
                    'flow'        => $row2['flow'],
                    'section'     => $row2['section'],
                    'document'     => $row2['document']            
                );
            }
            
            $arrFlows[$flowName] = $flows;
        }                                
        return $arrFlows;
    }

after putting some test data in it, i get this.
Code:
Array
(
    [TestFlöde] => Array
        (
            [0] => Array
                (
                    [date] => 1259077792
                    [user] => 1
                    [header] => crazy christmas
                    [flow] => TestFlöde
                    [section] => hoga_radet
                    [document] => Crazy_Christmas_2.doc
                )

            [1] => Array
                (
                    [date] => 1259074844
                    [user] => 1
                    [header] => fvsd
                    [flow] => TestFlöde
                    [section] => hoga_radet
                    [document] => avatar.jpg
                )

        )

    [TestFlöde2] => Array
        (
//from here
            [0] => Array
                (
                    [date] => 1259077792
                    [user] => 1
                    [header] => crazy christmas
                    [flow] => TestFlöde
                    [section] => hoga_radet
                    [document] => Crazy_Christmas_2.doc
                )

            [1] => Array
                (
                    [date] => 1259074844
                    [user] => 1
                    [header] => fvsd
                    [flow] => TestFlöde
                    [section] => hoga_radet
                    [document] => avatar.jpg
                )
//to here

            [2] => Array
                (
                    [date] => 1259079232
                    [user] => 1
                    [header] => nytt dokument
                    [flow] => TestFlöde2
                    [section] => hoga_radet
                    [document] => crazy_cristmaz_2009_first.jpg
                )

            [3] => Array
                (
                    [date] => 1259078001
                    [user] => 1
                    [header] => bombay
                    [flow] => TestFlöde2
                    [section] => hoga_radet
                    [document] => bombay.txt
                )

        )

)

as u can see the mark text has already been written to the first array, why is it in the second as well?

thanks for understanding my English Big Grin

#2
[eluser]Bazze[/eluser]
And how do you call the get_documents()-function? Do you call it once or twice?

#3
[eluser]dunken[/eluser]
Code:
function documents(){
        $data = $this->data;
        $data['flow_section'] = $this->flow_section;
        $data['arrFlows'] = $this->document_model->get_documents($this->flow_section);
        
        //print_r($data['flows']);
        $data['main_content_view'] = $this->load->view('hoga_radet/documents',$data,TRUE);
        $this->load->view('common/default_view', $data);        
    }

edit: once

#4
[eluser]jedd[/eluser]
Please provide your TABLE definition for your documents table - and we'll be able to write a cleaner query that does this in one fell swoop.

It looks like you just need a simple LEFT JOIN here.

#5
[eluser]dunken[/eluser]
id int(7)
date int(11)
user int(7)
header varchar(255)
flow varchar(255)
section varchar(255)
document varchar(255)

edit: some of u make such fine tables with _ and |, is there a way to do that without making them myself?

#6
[eluser]jedd[/eluser]
[quote author="dunken" date="1259108423"]
edit: some of u make such fine tables with _ and |, is there a way to do that without making them myself?[/quote]

Code:
$ mysql
mysql> describe documents;

#7
[eluser]dunken[/eluser]
Code:
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(7)       | NO   | PRI | NULL    | auto_increment |
| date     | int(11)      | NO   | MUL | NULL    |                |
| user     | int(7)       | NO   |     | NULL    |                |
| header   | varchar(255) | NO   |     | NULL    |                |
| flow     | varchar(255) | NO   |     | NULL    |                |
| section  | varchar(255) | NO   |     | NULL    |                |
| document | varchar(255) | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+

thx for the tip

#8
[eluser]jedd[/eluser]
Okay, I think you want something like this - but note this is untested code, so you'll have to play around with it.

Code:
SELECT
       id, date, user, header, flow, section, document,
       documents_x.id  AS  id_child, documents_x.date  AS  date_child,  .... etc
FROM
      documents
LEFT JOIN
      documents AS documents_x  ON  documents_x.section = documents.flow
WHERE
      documents.section = $section

Remember to put your LIMIT and WHERE clauses back in.

Not sure if you need a DISTINCT with this.

#9
[eluser]dunken[/eluser]
ok, but I want an answer like my array in the first post (but without the duplicates).

I need to send the multidim-array so that i can catch both the arrayname and the content, something like this.

Code:
foreach($arrFlows as $flowname => $flownames)
{
    echo $flowname;
        foreach ($flownames as $id => $flowdata){
            echo $flowdata['something'];
        }
    }
}

How do I do that with your sql?

I mean, there has to be a simple mistake in my code.

#10
[eluser]cahva[/eluser]
Yes, there is a simple mistake in your code.

You have to clear the $flows array for the next iteration. Now its always adding to the same array.

Code:
$flows = array(); // add this to clear it
            foreach($query2->result_array() as $row2)
            {


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.