0

Hey guys its my first question here I think.

So I'm using MyBB Forum software.

I have a PHP code that fetches some data from a database no issues in there its working properly.

$query = $db->query("SELECT * FROM housing");
while($result = $db->fetch_array($query))
{    
    $housingvar .= "<td>". $result['username']. "</td>";
    $price .= "<td>". $result['Price']. "</td>";
    $city .= "<td>". $result['City']. "</td>";
    $tax .= "<td>". $result['Tax']. "</td>"; 
    $adrz .= "<td>". $result['Adress']. "</td>";
}

This is the code. I'm using. These variables are then plugged in an HTML table on the forum but everything is in the same column and I dont know how to make a new row for every data. I'm sure this is possible but dont know how.

I cant use echo in my PHP code either. Since I'm using these variables in a pure HTML template.

<tr>
    {$housingvar}
         {$price} 
          {$city}
            {$adrz}
            {$tax}
    </tr>
8
  • Does your templating software provide its own looping mechanism? You can't collect each row's value into a single variable, because they have to be split onto different rows of the table. Commented May 1, 2017 at 21:36
  • I'm not really sure about that.. Commented May 1, 2017 at 21:39
  • If it doesn't, then there doesn't seem to be a way to do what you want. If the variables all have to be between a single <tr> and </tr>, there's no way to get multiple rows. Surely the designers of your template provided a way to generate tables of data from PHP arrays. Commented May 1, 2017 at 21:40
  • @Barmar How would you normally loop in PHP. Can you give me a small example if you don't mind? Commented May 1, 2017 at 22:12
  • The answers show the normal way to loop over the results of a query. But they won't work for the way you're templating this. Commented May 1, 2017 at 22:15

2 Answers 2

1

<tr> for table row and <td> for table data.
However you have to wrap your while statement with table tag:

$output = "";
$output .= "<table>";
while ($result = $db->fetch_array($query)) {
     // Starting a new table row
     $output .= "<tr>";
        $output .= "<td>".htmlentities($result['username']). "</td>";
        /* Append the rest of the fields */
     // End of row
     $output .= "</tr>";
}
$output .= "</table>";
{ $output }
Sign up to request clarification or add additional context in comments.

3 Comments

This answer doesn't escape any of the data being output, and can lead to invalid HTML and security issues. Use htmlspecialchars() or similar.
Hey so the thing is I cant use PHP on my HTML file. So I did this but what variable would I call from my HTML file exactly? @AbdAlrahmanHashem
@Brad The concern was not about escaping, but I just edited it thanks.
0

To create a new row in an HTML table, you use the <tr> tag. I would try the following:

echo "<tr>";
   $price = $result['Price'];
   echo "<td> $price </td>";
    //other code here
echo "</tr>";

1 Comment

I cant use echo. I'm using these variables directly on my templates which has a pure HTML structure. <tr> {$housingvar} {$price} {$city} {$adrz} {$tax} </tr>

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.