$id ="123";
$q="select * from some table where field0 = '$id' and field1 != 'n' and field2 != 'n' etc etc...";
$result=mysql_query($q);
$array=mysql_fetch_array($result);
foreach ($array as $key => $value) {
echo "$key $value<br />\n";
}
I have a 2 part problem
I have a table that contains fields with a variety of different values. Some are equal to N some are equal to Y some are equal to textual values and some are numeric.
I want to select certain fields that equal a certain id and output the fieldname and value to the page.
The above outputs all of the fields in the DB including those that equal 'n' .
10 fieldsvalue
literalfieldname1 fieldsvalue
1 fieldsvalue
literalfieldname2 fieldsvalue
2 fieldsvalue
literalfieldname3 fieldsvalue
etc through to 160
What I would like to do is to just output
literalfieldname1 fieldsvalue
literalfieldname2 fieldsvalue
literalfieldname3 fieldsvalue
and stop when the db conditon is no longer met.
Ive tried
while (list ($key, $val) = each ($array)) {
echo "$key $value<br />\n";
}
and am trying to avoid having to do this
for ($i=0; $i <$num_results; $i++) {
$row = mysql_fetch_array($result);
if($row[field1] = "Y"){
echo"literalfieldname1: Yes";
}
else{
echo"literalfieldname1: $row[field1]";
}
if($row[field1] = "Y"){
echo"literalfieldname2: Yes";
}
else{
echo"literalfieldname2: $row[field2]";
}
//repeat 160 times!
}
Because if theres a more efficient alternative I'd rather use it.
The key thing in all this is that I want to output just the fieldname and its value.
Can anyone suggest an alternative?
Many thanks
Rob