Welcome Guest, Not a member yet? Register   Sign In
How to do a Join properly?
#1

Hi everyone,
I'm new to this forum, and I'm currently finishing the final project of my degree as a computer engineer. 
I'm using CodeIgniter 3, and I can't find a way of doing something. As I don't know where should I post this, I decided to create a new Thread here, in General Discusion.
I am creating an application to manage soccer matches and competitions.
My application has Matches and these matches belong to a specific Competition. I have a table for matches, and another one for competitions, and as more information can be obtained from both of them through views, in both tables I have the parameter "slug", which is used to create the URL and obtain an individual view of each element of both tables.
I would like to have the possibility to add the matches to a particular competition, and thus get all the matches to belong to a Competition.
I have tried using the join function, but as both have the slug parameter, when I try to access the view of a particular match, passing as parameter match['slug'], the model function returns the parameter competition['slug'], and I do not know how to solve this so that it returns the parameter I want.
Do any of you know how to solve this?
This is the code for match controllers, the function get_matches, wich is the one that is in charge of returning the matches and also of giving me the view for each one when you give it a parameter:
Code:
public function get_matches($slug = FALSE){
            if($slug === FALSE){
                $this->db->order_by('matches.id','DESC');
                $this->db->join('competitions','competitions.id = matches.competition_id');
                $query = $this->db->get('matches');
                return $query->result_array();
            }
            $query = $this->db->get_where('matches', array('slug' => $slug));
            return $query->row_array();
        }

Also, for those who need it, this is the code for the general view for the matches, it works perfectly but whe you click the Button "More Information", it takes the parameter match['slug'], but in this case, it returns the parameter competition['slug']:
Code:
<h2><?=$title?></h2>
<?php foreach($matches as $match) :  ?>
    <div class="container">
    <h3><?php echo $match['title']; ?></h3>

    <small><span class="badge badge-primary"><strong><?php echo $match['name']; ?></strong></span></small>
   
    <?php echo word_limiter($match['body'],60); ?><br>
   
    <small><span style="float:right" class="badge badge-info">Created at: <?php echo $match['created_at']; ?></span></small>

    <p><a class="btn btn-warning" href="<?php echo site_url('/matches/'.$match['slug']); ?>">More Information</a></p>
</div>
    <br>
<?php endforeach; ?>
 
If you need more information, please let me know. Aswell as if this is not the correct part of the forum to ask.
Thanks in advance.
Reply
#2

->select('*, matches.slug as matches_slug')

$matches['matches_slug']
Reply
#3

(01-28-2021, 07:33 PM)iRedds Wrote: ->select('*, matches.slug as matches_slug')

$matches['matches_slug']

Thank you very much, it fixed my problem.  Big Grin
Reply




Theme © iAndrew 2016 - Forum software by © MyBB