-1

I have a table used for checkboxes where values are either 1 or 0.

I'm having an issue printing column names for the selected row where the field value is equal to 1 (excluding any row equal to 0) and separate by comma, using implode.

I've tried several approaches but without the use of mysql_num_fields I haven't been able to print the field names with the array (mysql_fetch_array)

Here is what I am currently working with. Any help is much appreciated. And yes, I will be moving to mysqli.

$query_columns = mysql_query("SELECT field1, field2, fields3 FROM table1 WHERE user_id = '" .  $id . "'");

    $numberfields = mysql_num_fields($query_columns);

        for ($i=0; $i<$numberfields ; $i++ ) {
            $var = mysql_field_name($query_columns, $i);
            $row_title .= $var;
        }

    echo $row_title;
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Klav
  • 405
  • 1
  • 9
  • 19
  • Let me see if I understood your problem: if an user with ID 1 checked fields 1 and 3, the select would return (1, 0, 1). And you want to print "field1, field3". Am I correct? – FlavioEscobar Jul 10 '14 at 15:03
  • 1
    is $row_title false or null, echo wont print that instead try ( $row_title ) ? $row_title : 'false'; – ArtisticPhoenix Jul 10 '14 at 15:06
  • @FlavioEscobar yes that is correct. So right now I am printing all fields rather than only fields with values 1 – Klav Jul 10 '14 at 15:16
  • @ArtisiticPhoenix, but my values are 1 & 0, not false or null. Null would only account for an empty field, correct or does it include 0? I didn't think it did – Klav Jul 10 '14 at 15:17
  • sometimes php treats '0' ( String 0 ) 0, false, null the same, empty certainly does. I posted a more efficient answer. Also you have no commas in $row_title, I've never used mysql_field_name, lol, anyway your values should be strings, the column or field names, right? – ArtisticPhoenix Jul 10 '14 at 15:18
  • @Jamie -- are you sure you even have a result for the query? mysql_num_rows() should be at least 1 if you do, – ArtisticPhoenix Jul 10 '14 at 15:23
  • @ArtisiticPhoenix Thanks, yes it returns all field names. the default is 0 so all fields are not null – Klav Jul 10 '14 at 15:26
  • @Jamie there is no way to tell the value without getting the result, so if you want only the 1's you have no choice but to use mysql_fetch or similar, can you put the output you are getting now? I imagine its all the fields mushed together "field1field2fields3" – ArtisticPhoenix Jul 10 '14 at 15:30
  • @ArtisiticPhoenix Thanks a bunch! Your answer was the key. I had assumed I needed mysql_field_name to print name but mysql_fetch_assoc did the trick. My output is as intended now, field names where values are 1. e.g., 'Field 3, Field 5, Field 6' – Klav Jul 10 '14 at 15:35
  • you can also do print_r($row) and with and echo "
    "; ( to preserve whitespace ) before it get a nice print out of the row.  I'll add it to my answer
    – ArtisticPhoenix Jul 10 '14 at 15:42

3 Answers3

1

echo wont print null or false values you can see that this simple example;

    echo '--------------<br>';
    echo false; 

    echo null;
    echo '<br>--------------';

Also if you just want to print the field names, ie not their values? ( I haven't been able to print the field names )
Why not just do this ( assuming you have at least one result ):

$row = mysql_fetch_assoc($query_columns);

echo implode(', ', array_keys($row));

And if you don't want the 0 value names just use filter to remove them, then print:

$row = mysql_fetch_assoc($query_columns);

echo implode(', ', array_keys( array_filter( $row )));

If you want a nice print out of the $row data just do this:

echo "<pre>";
print_r($row); // or var_export($row)
echo "</pre>";
ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
0

Not sure if I get right what you want. But try that:

$row = mysql_fetch_row($query_columns);

And then you can create if in your for loop

for ($i=0; $i<$numberfields ; $i++ ) {
    if($row[$i] == 1) { 
        $var = mysql_field_name($query_columns, $i);
        $row_title .= $var;
    }
}
Piotr
  • 671
  • 6
  • 17
0
Hope I have understood your question correctly.

$query_columns = mysql_query("SELECT field1, field2, fields3 FROM table1 WHERE user_id = '" .  $id . "'");

    $rows = mysql_fetch_assoc($query_columns);

    $result = '';
    $i=0;
    $count = count($rows);
    foreach($rows as $key => $value) {
        if($value === 1) {
            $result .= '$key';
        }
        if($i!=0 && $i<$count-1)
             $result .= ', ';
        $i++;
    }
    echo $result;
Abhishek Salian
  • 928
  • 2
  • 10
  • 27