Search code examples
phpmysqlarrayslistfield-names

List field names where value of field is '1' and id=$id


I have a table 'jobs' with fields id,p1,p2,p3 where some entries under p1,p2,p3 have a value of '1'. How might I get a simple list of fields (p1,p2, or 3) which have a value of '1' in rows of table jobs where id=$id.


Solution

  • $row = mysqli_fetch_assoc($query_result);
    $list = '';//or array
    foreach ($row as $key=>$val) {
        if ($key != 'id' and $val == 1) { //or if substr($key, 0, 1) == 'p' and $val == 1)
            $list .= $key.', ';
        }
    }