0

I am trying to display data from a MySQL table using PHP inside html page, wrapping data in HTML table.

I have 11 columns in a MySQL table and x rows, among which i need just 6 columns for all rows to get printed as HTML table. Which i am able to do.(Successfully)

My Approach:

$result = mysqli_query($connect, "SELECT order_id, order_name, order_from, order_to, order_pickup, order_dropoff FROM users");

// Printing results in HTML
echo "\n";
while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) {
    echo "\t<tr id='$line[order_id]'>\n";
    foreach ($line as $col_value) {
        echo "\t\t<td>$col_value</td>\n";
    }
    echo "\t</tr>\n";
}
echo "\n";

Output:

enter image description here

My Issue: Among these 6 columns i want 1 column named order_id to be the id of the row (tr), so now i want only remaining five as table-cell's td's. I don't want the value of order_id column as table cell.

3
  • 5
    Then don't echo it? I advice using if Commented Jan 6, 2016 at 16:09
  • divy3993, please check my answer too, just to see if It works as expected. Else there is not need and I'll delete my answer. Commented Jan 6, 2016 at 16:30
  • @Naruto check my answer. Commented Jan 6, 2016 at 16:33

4 Answers 4

2

Check also a column name in a $line:

foreach ($line as $col_name => $col_value) {
    if ($col_name != 'order_id') {
        echo "\t\t<td>$col_value</td>\n";
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

What does => does here?
=> indicates key-value syntax for a foreach.
2

Ok there are a number of solutions to this. Generally when writing this kind of code one often separates the database layer from the template layer and asses the information to the template layer.

For the sake of your example you can choose to remove the foreach and individually define the separate column values which in my opinion is usually the best way to go as you already know all the columns and have more control in when to echo them:

$result = mysqli_query($connect, "SELECT order_id, order_name, order_from, order_to, order_pickup, order_dropoff FROM users");

// Printing results in HTML
echo "\n";

while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) {
    echo "\t<tr id='$line[order_id]'>\n";

    echo "\t\t<td>$line['title']</td>\n";
    echo "\t\t<td>$line['second title']</td>\n";

    echo "\t</tr>\n";
}

echo "\n";

Another approach as I believe a user already mentioned is to use an if statement. You can do this on the key of the array:

$result = mysqli_query($connect, "SELECT order_id, order_name, order_from, order_to, order_pickup, order_dropoff FROM users");

// Printing results in HTML
echo "\n";

while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) {
    echo "\t<tr id='$line[order_id]'>\n";

    foreach ($line as $col_name => $col_value) {
        if ($col_name === 'order_id') {
            continue;
        }
        echo "\t\t<td>$col_value</td>\n";
    }

    echo "\t</tr>\n";
}

echo "\n";

2 Comments

Yes your solution is helpful too since i know about my column names. Thank You Very Much!
Good to be of service, I have read some of the other comments and I think you are still trying to figure out how things work in PHP. There is this great site which can help you called [link][codecademy.com]. This should explain most of the basics including most of the syntax such as => in a foreach
1

Your solution is simple, you just need to check if the array key is order_id, if it is, then don't echo it :D

echo "\n";
while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) {

    echo "\t<tr id='$line[order_id]'>\n";
    foreach ($line as $key => $col_value) {
        if ($key != 'order_id')
        echo "\t\t<td>$col_value</td>\n";
    }
    echo "\t</tr>\n";
}
echo "\n";

1 Comment

You got me wrong, i want the order_id as id of the respective tr(which i am able to achieve sucessfull, which you removed here) but not to be printed as the td "Table-cell".
1

I'm sure there is a better answer from what I will post, but I will post it just to see how simple is the idea.

The disadvantage with my code is that it checks the if() in each loop statement increasing the complexity of your code.

Basically you set a bool variable to hold the first pass of each table row. This variable is set to false ($pass_one = false). Then when we start to getting the columns info we set the bool variable to true and skip the first element. In the next column iterates, we proceed as we should. After we've done with the column set, we change the $pass_one back to false and proceed to the other rows repeatedly.

<?php $result = mysqli_query($connect, "SELECT order_id, order_name, order_from, order_to, order_pickup, order_dropoff FROM users");

$pass_one = false; //you set it  to false

// Printing results in HTML
echo "\n";
while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) {
    echo "\t<tr id='$line[order_id]'>\n";
    foreach ($line as $col_value) {
        if (!$pass_one) { 
            $pass_one = true; // you pass the first echo, so you set the bool to true
            continue; // skip the first pass, (this statement goes to foreach() loop
        }
        echo "\t\t<td>$col_value</td>\n";
    }
    echo "\t</tr>\n";
    $pass_one = false; // we get a new <tr> so we set the $pass_one for that loop to false
}
echo "\n";

?>

Comments

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.