CodeIgniter Forums
Sub request in where - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Sub request in where (/showthread.php?tid=72487)



Sub request in where - xenos92 - 12-27-2018

Hello,
I come to you because I need help on request ...

I have a rank table that lists all the ranks available to a player with the maximum number of points for the rank.
I have another table users who list my players with their experience.

I want to use the experience of players to display the rank corresponding to the experience.

Example :

[b]rank table[/b]
|    id    |    grade    |    point    |
__________________________
|    1    |   Niveau 1  |     300    |
|    2    |   Niveau 2  |     600    |

users table
|    id    |    name    |    experience    |
______________________________
|    1    |      test      |     330             |


So if my player has an experience of 330 I need to display a level of 2 because above 300 and below 600.

PHP Code:
/*
// This works if I enter the experience value directly
$this->db->select('*')
            ->from('rank')
            ->where('point >=', 1200);
*/

// This not work I can't arrive to get the experience user
$this->db->select('*')
            ->
from('rank')
            ->
where('point >='"SELECT 'experience' FROM 'users' WHERE 'name' =  $username");


$query $this->db->get();

if(
$query->num_rows()>0)
{
    return 
$query->row_array();




RE: Sub request in where - php_rocs - 12-28-2018

@xenos92,

Will the rank table always be dynamic or are there set from and to values for each level? Is this a MySQL database?


RE: Sub request in where - badger - 12-28-2018

how about
Code:
function rankedplayers()
 ranks = this->db->query(select * from ranks table order by point asc);
 players = this->db->query(select * from players order by name);
 foreach players as player
   prior = 'invalid';
   foreach ranks as rank
if (player->points < rank->point){
prior = rank->grade;
break;
}
}
player->rank = prior;
 }
 return players;
}



RE: Sub request in where - xenos92 - 12-28-2018

Thank you badger Wink