I am struggling with a relatively simple problem but just cant get my head around it.
I have a method getNextRound()
which returns an array of numbers. The numbers represents the week numbers in the db table.
I then have a second method getUpcomingGames()
where I call the the first method I then want to use the numbers from the first method to use in my query.
Here is an example: METHOD 1
public function getNextRound(){
$sql = "SELECT min(weekNum) from schedule WHERE schedule.gameDateTime > NOW() GROUP BY tournament ORDER BY gameDateTime ASC";
$stmnt = $this->db->query($sql);
if ($stmnt->num_rows() > 0) {
print_r($stmnt->result());
return $stmnt->result();
}
return false;
}
RESULT FROM ABOVE METHOD / QUERY
array (size=3)
0 =>
object(stdClass)[24]
public 'min(weekNum)' => string '38' (length=2)
1 =>
object(stdClass)[25]
public 'min(weekNum)' => string '14' (length=2)
2 =>
object(stdClass)[26]
public 'min(weekNum)' => string '7' (length=1)
I now want to use the data in the array to get all info contained in schedule table related to the week number.
My problem is here
METHOD 2
public function getUpcomingGames()
{
//HERE I WANT TO GET ALL INFO FROM SCHEDULE WHERE ROUND = $week
$rounds[] = $this->getNextRound();
foreach ($rounds as $round) {
$sql = "SELECT * from schedule WHERE weekNum = '$round' ORDER BY gameDateTime ASC ";
$data[] = $this->db->query($sql);
var_dump($data);
}
ERROR: Among others I get an array to string conversion error.
I have looked through the codeigniter docs, but could not find the method which I am looking for.
QUESTION:
Is there a query method in CI where I can insert an array into a query and loop over array (), if that makes sense?
How can I improve / fix the above query?
I presume you require a query like this:
SELECT *
FROM schedule AS parent
JOIN (
SELECT tournament,
MIN(weekNum) AS nextWeek
FROM schedule AS child
WHERE gameDateTime > NOW()
GROUP BY tournament
) ON parent.tournament = child.tournament AND parent.weekNum = child.nextWeek
ORDER BY gameDateTime";
This will maintain the relationship between tournaments and weekNums when passing qualifying rows to the parent query. This way the result set remains true even if you have a non-qualifying tournament with a qualifying WeekNum.
The codeigniter equivalent is:
$this->db->select('tournament, MIN(weekNum) AS nextWeek');
$this->db->from('schedule');
$this->db->where('gameDateTime >', 'NOW()', false);
$this->db->group_by('tournament');
$subquery = $this->db->get_compiled_select();
// $this->db->select('*'); <- not necessary
$this->db->from('schedule AS parent');
$this->db->join('(' . $subquery . ') AS child', 'parent.tournament = child.tournament AND parent.weekNum = child.nextWeek');
$this->db->order_by('gameDateTime');
return $this->db->get()->result();
*Disclaimer: None of the above is tested. Confirmation of success would be appreciated.