Jump to content

Developing Multidiminsional Array


Xtremer360

Recommended Posts

I'm trying to develop a resultset that appears like so:

 

[1] Title One name
     [champion] Jeff
     [contender1] kevin
     [contentder2] brian
     [contender3] will

[2] Title Two name
     [champion] Jeff
     [contender1] kevin
     [contentder2] brian
     [contender3] will

[3] Title Three name
     [champion] Jeff
     [contender1] kevin
     [contentder2] brian
     [contender3] will

 

I'm using the codeigniter's active record to develop this query and have this so far. To understand how my db structure looks this is what I am using.

 

To understand what the structure means is that the titles table defines the title id and the title name. The roster table defines the roster id and roster name. The title champions table has the table id which is the same as the title id from the titles table and champion id, contender1 id, contender2 id, contender3 id all represent the id of the roster table.

 

Titles Table
title_id
title_name

Titles Champions Table    
title_id
champion_id
contender1_id
contender2_id
contender3_id

Roster Table
roster_id
roster_name

 

This is what I have so far.

 

/**
     * Gets the listing of all title champions and contenders.
     * 
     * @return mixed (object/NULL) Object of title champions/contenders if query returned any data
     */
    public function get_title_champions()
    {
        $this->db->select($this->master_model->titles_table.'.title_name');
        $this->db->select($this->master_model->titles_table.'.title_id');
        $this->db->from($this->master_model->title_champions_table);
        $this->db->join($this->master_model->titles_table, $this->master_model->titles_table.'.title_id ='.$this->master_model->title_champions_table.'.title_id');
        $query = $this->db->get();
        return $query->result();
    }

 

Is there any additional ideas from anyone?

Link to comment
Share on other sites

[1] Title One name

    [champion] Jeff,

    [contender1] kevin,

    [contentder2] brian,

    [contender3] will

 

Is not possible. What it could look like is:

 

$array = array();
$array[1] = array(
'title'=>'Title One name',
'champion'=>'Jeff'
'contender1'=>'kevin'
'contentder2'=>'brian'
'contender3'=>'will'
);

 

Array

(

    [1] => Array

        (

            [title] => Title One name

            [champion] => Jeff

            [contender1] => kevin

            [contentder2] => brian

            [contender3] => will

        )

 

)

 

Or even like this if you want:

$array = array();
$array[1] = array(
'title'=>'Title One name',
'data'=>array(
	'champion'=>'Jeff',
	'contender1'=>'kevin',
	'contentder2'=>'brian',
	'contender3'=>'will'
)
);

 

Array

(

    [1] => Array

        (

            [title] => Title One name

            [data] => Array

                (

                    [champion] => Jeff

                    [contender1] => kevin

                    [contentder2] => brian

                    [contender3] => will

                )

 

        )

 

)

 

For now, do a print_r($query->result); so we can see what you DO have. However, it'll probably be easiest to just use the result as it comes out of the DB, and modify your code that USES it.

Link to comment
Share on other sites

I did a print_r on the variable I have set that returns the function from the model and got this.

 

Array
(
    [0] => stdClass Object
        (
            [title_name] => Undisputed Title
            [title_id] => 1
        )

    [1] => stdClass Object
        (
            [title_name] => Tag Team Titles
            [title_id] => 2
        )

    [2] => stdClass Object
        (
            [title_name] => Outlaw Title
            [title_id] => 3
        )

)

Link to comment
Share on other sites

Contrary to how it might seem some days, we don't just sit here waiting for your next post. Some of us work for a living and use this forum as a place to take a break. I was going to answer your question about WHY I suggest inner join, rather than left join, but now I'll just say JFGI.

Link to comment
Share on other sites

You all will probably shoot me but I'm getting a similar syntax error like I did in my other post but not not seeing it.

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS champion INNER JOIN `roster` ON `roster`.`roster_id` =`title_champions`.`cont' at line 4

 

SELECT `titles`.`title_name`, `titles`.`title_id` FROM (`title_champions`) JOIN `titles` ON `titles`.`title_id` =`title_champions`.`title_id` INNER JOIN `roster` ON `roster`.`roster_id` =`title_champions`.`champion_id` AS champion INNER JOIN `roster` ON `roster`.`roster_id` =`title_champions`.`contender1_id` AS contender1 INNER JOIN `roster` ON `roster`.`roster_id` =`title_champions`.`contender2_id` AS contender2 INNER JOIN `roster` ON `roster`.`roster_id` =`title_champions`.`contender3_id` AS contender3

 

 

/**
     * Gets the listing of all title champions and contenders.
     * 
     * @return mixed (object/NULL) Object of title champions/contenders if query returned any data
     */
    public function get_title_champions()
    {
        $database_schema = $this->master_model->database_schema();
        $this->db->select($database_schema->titles_table.'.title_name');
        $this->db->select($database_schema->titles_table.'.title_id');
        $this->db->from($database_schema->title_champions_table);
        $this->db->join($database_schema->titles_table, $database_schema->titles_table.'.title_id ='.$database_schema->title_champions_table.'.title_id');
        $this->db->join($database_schema->roster_table, $database_schema->roster_table.'.roster_id ='.$database_schema->title_champions_table.'.champion_id AS champion', 'inner');
        $this->db->join($database_schema->roster_table, $database_schema->roster_table.'.roster_id ='.$database_schema->title_champions_table.'.contender1_id AS contender1', 'inner');
        $this->db->join($database_schema->roster_table, $database_schema->roster_table.'.roster_id ='.$database_schema->title_champions_table.'.contender2_id AS contender2', 'inner');
        $this->db->join($database_schema->roster_table, $database_schema->roster_table.'.roster_id ='.$database_schema->title_champions_table.'.contender3_id AS contender3', 'inner');
        $query = $this->db->get();
        return $query->result();
    }

Link to comment
Share on other sites

Perfect I have a working print_r of my resultset however its the wrong desired results.

 

title_champions_table
title_id = 1
champion_id = 1
contender1_id = 2
contender2_id = 3
contender3_id = 3

title_id = 2
champion_id = 1
contender1_id = 2
contender2_id = 3
contender3_id = 3

title_id = 3
champion_id = 1
contender1_id = 2
contender2_id = 3
contender3_id = 3

Roster Table
roster_id  Roster Name
1 - Kid Wonder
2- Oriel
3- Ryu Satoshi

 

 

Array
(
    [0] => stdClass Object
        (
            [title_name] => Undisputed Title
            [title_id] => 1
            [champion] => Ryu Satoshi
            [contender1] => Ryu Satoshi
            [contender2] => Ryu Satoshi
            [contender3] => Ryu Satoshi
        )

    [1] => stdClass Object
        (
            [title_name] => Tag Team Titles
            [title_id] => 2
            [champion] => Ryu Satoshi
            [contender1] => Ryu Satoshi
            [contender2] => Ryu Satoshi
            [contender3] => Ryu Satoshi
        )
)
    [2] => stdClass Object
        (
            [title_name] => Outlaw Title
            [title_id] => 3
            [champion] => Ryu Satoshi
            [contender1] => Ryu Satoshi
            [contender2] => Ryu Satoshi
            [contender3] => Ryu Satoshi
        )

 

/**
     * Gets the listing of all title champions and contenders.
     * 
     * @return mixed (object/NULL) Object of title champions/contenders if query returned any data
     */
    public function get_title_champions()
    {
        $database_schema = $this->master_model->database_schema();
        $this->db->select($database_schema->titles_table.'.title_name');
        $this->db->select($database_schema->titles_table.'.title_id');
        $this->db->select('contender3.roster_name AS champion');  
        $this->db->select('contender3.roster_name AS contender1');  
        $this->db->select('contender3.roster_name AS contender2');  
        $this->db->select('contender3.roster_name AS contender3');  
        $this->db->from($database_schema->title_champions_table);
        $this->db->join($database_schema->titles_table, $database_schema->titles_table.'.title_id ='.$database_schema->title_champions_table.'.title_id');
        $this->db->join($database_schema->roster_table.' AS champion', 'champion.roster_id ='.$database_schema->title_champions_table.'.champion_id', 'inner');
        $this->db->join($database_schema->roster_table.' AS contender1', 'contender1.roster_id ='.$database_schema->title_champions_table.'.contender1_id', 'inner');
        $this->db->join($database_schema->roster_table.' AS contender2', 'contender2.roster_id ='.$database_schema->title_champions_table.'.contender2_id', 'inner');
        $this->db->join($database_schema->roster_table.' AS contender3', 'contender3.roster_id ='.$database_schema->title_champions_table.'.contender3_id', 'inner');
        $query = $this->db->get();
        return $query->result();
    }

Link to comment
Share on other sites

Issue was that I was using contender3 table alias situation was obviously wrong however te real issue and this is the tricky part is what if one of the values inside of the champion_id or contender1_id or contender2_id or contender3_id has a value of 0 then how would I account for that.  If the champion_id is 0 then it should put Vacant into the array and for the contenders if the value is 0 then it should put TBD for each of them.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.