1

in the code below Im searching specific columns from a db table and placing them into a html table, inserting &nbsp for empty db fields to maintain uniformed structure. That part works fine, but Im having trouble figuring out the best way to go about getting the column names that are being searched and dynamically displaying them once at the very top of the html table? If my search changes, the column names displayed on the html table will change with it accordingly. Im fairly new to php/mysql and would appreciate any help guys.

$result = mysql_query('SELECT part, ers, make, model, years, oe, core, inlet, outlet FROM parts LIMIT 3');
echo '<table>';
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo '<tr>';
    foreach ($row as $name => $value) {
        if ( $value == "" ) $value="&nbsp;";
        echo '<td> '.$value.' </td>'; 
    }
    echo '</tr>'; 
}
echo "</table>";

1 Answer 1

1

How about something like this:

$result = mysql_query('SELECT part, ers, make, model, years, oe, core, inlet, outlet FROM parts LIMIT 3');
echo '<table>';

$cnt = 0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    if ($cnt == 0) {
        $columns = array_keys($row);
        echo '<tr><th>' . implode('</th><th>', $columns) . '</th></tr>';
    }

    $cnt++;
    echo '<tr>';

    foreach ($row as $name => $value) {
        if ( $value == "" ) $value="&nbsp;";
        echo '<td> '.$value.' </td>'; 
    }

    echo '</tr>'; 
}

echo "</table>";
Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much, i hope I can figure that out that fast one day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.