There is a table contains columns like
username,settings1,settings2,settings3
username is a unique and settings1,2,3 is contain 0 or 1 values.
$query = $this->db->query("SELECT * FROM tbl_users WHERE username = 'test'");
$queryrow = $query->row();
so i want to select the row that matching to username and loop through columns and check which column contain 0's and 1's
i can do this by writing a if statement for every column though, like
if($queryrow->settings1=="1"){
..
}
there is like 7 columns in table so instead of writing 7 if statements, any other way to do this? im using codeigniter 2.0.2
Regards
// use active record to work even less and be safer
$query = $this->db->get_where('tbl_users', array('username' => 'test'));
if($query->num_rows() < 1){
//prevent errors
return false; // or do something more apropriate
}
$data = $query->row();
$data->setting1; // contains setting1 value
$data->setting2; // contains setting2 value
$data->setting3; // contains setting3 value
You can outputing an array from table using activerecord
$qry = $this->db->get_where('tbl_users',array('username' => 'test'));
$res = $qry->result_array();
// Since you mention each username is a unique, then this should outputing the array of settings
var_dump($res[0]);
You can iterate objects
// use active record to work less and be safer
$this->db->where('username', 'test');
$query = $this->db->get('tbl_users');
foreach ($query->result() as $row)
{
// iterate the row object
foreach($row as $key => $item)
{
if($row->$key == "1")
{
// your code
}
}
}
If all you need to do in this loop is checking those values, you should do a SELECT to have only those items in $row, and avoid having "username"
$this->db->select('setting1, setting2, setting3');
Otherwise, if you want to keep username, the IF statement could be rather
if(in_array($key, array('setting1', 'setting2', 'setting3')) && $row->$key == "1")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.