I'm trying to see if there is a loop I can use in order to pull off this code a little more elegantly. I was previously using a foreach($row as $cell) with $row = mysqli_fetch_row($result) but because of that I couldn't access the data from the 'user' column in order to check it against the session variables in the if statment. Here's the code:$sql = SELECT * FROM tbl_name ORDER BY subject, description LIMIT {$startpoint},{$limit};$result = $mysqli->query($sql);$num_rows = mysqli_num_rows($result);if($num_rows>0){$field_num = $mysqli->field_count;echo <h1>HERE ARE SOME RESULTS:</h1>;echo <table border='0'>;    while($row = mysqli_fetch_array($result)){    echo<tr>;    echo<td>.$row['subject'].</td>;    echo<td>.$row['description'].</td>;    echo<td>.$row['user'].</td>;    if($row['user'] == $_SESSION['firstname']. .$_SESSION['lastname']){    echo<td>You can delete this</td>;    }    else{    echo<td>Code didn't work</td>;    }    echo </tr>\n;}mysqli_free_result($result);}else{echo 'There are no results!';}?>Is there a loop I can run that can also allow me to access data from each row so I don't have to print out a new line of code for each column but keep the if statement in tact? Thanks!
Trying to Find a Loop to use for a specific MySQL query
php;mysql
Saving lines of code in search of elegance is not always (in fact hardly ever in my experience) a worthwhile exercise if it comes at the expense of clarity and maintainability. Your current code is better than any solution that iterates over table columns because it is clear and easy to change. For example, combining the data of two columns into one or changing column order are both trivial.You might consider moving this display logic to a template. Here is an example of a twig template:{% if results %}<h1>HERE ARE SOME RESULTS</h1><table>    {% for row in results %}    <tr>        <td>{{ row.subject }}</td>        <td>{{ row.description }}</td>        {% if row.user == user %}        <td>You can delete this</td>        {% else %}        <td>Code didn't work</td>        {% endif %}    </tr>    {% endfor %}</table>{% else %}<p>No results</p>{% endif %}The PHP becomes very simple:$bindings = array(    'results' => $mysqli->query('SELECT...'),    'user' => $_SESSION['firstname'] . ' ' . $_SESSION['lastname']);$twig->render('template.html', $bindings);