1

Hi im new to frontend world and i made a small sql database filled with some data , i could get the data from database to my php code as Associative Array but the problem is that i cant fill them inside my table , they appear upper and outside the table. with any help i'll be thankful.

<?php
include("page.html");
include("databaseConnection.php");

$sth = $conn->prepare("SELECT ID, userName from passwords");
$sth->execute();

$result[] = $sth->fetchAll();

if(isset($_POST["showData"])){

echo "<table style='border: 2px solid black'>
<tr>
<th style='border: 1px solid red'>ID</th>
<th style='border: 1px solid black'>User Name</th>
</tr> <tbody>";

foreach ($result as $key => $value) {
    foreach ($value as $key1 => $value1) {
        echo "<tr>
        <td>".print_r($value1['ID'])."</td>
        <td>".print_r($value1['userName'])."</td>
        </tr>";
    }

}
echo "</tbody></table>";


}
?>

enter image description of the result here

3 Answers 3

1

You are using print_r inside a string concatenation.

foreach ($value as $key1 => $value1) {
    echo "<tr>
    <td>".print_r($value1['ID'])."</td>
    <td>".print_r($value1['userName'])."</td>
    </tr>";
}

Remove the print_r statements and the values will show up inside the table.

Sign up to request clarification or add additional context in comments.

1 Comment

thx alot i removed one foreach aswell and the print_r it Works! , have a nice day Sir!.
0

Your tbody tag should be parent of all your tr elements and your header in thead. Check this struct bellow:

<table>
 <thead>
  <tr>
     <th>Month</th>
     <th>Savings</th>
  </tr>
 </thead>
 <tbody>
  <tr>
     <td>January</td>
     <td>$100</td>
  </tr>
  <tr>
     <td>February</td>
     <td>$80</td>
  </tr>
 </tbody>
 <tfoot>
  <tr>
     <td>Sum</td>
     <td>$180</td>
  </tr>
 </tfoot>
</table>

1 Comment

thx for answer i've tryed this but also didnt work the result always appears outside the table
0

A quick&dirty solution,not tested :-

<?php
include("page.html");
include("databaseConnection.php");

$sth = $conn->prepare("SELECT ID, userName from passwords");
$sth->execute();

$result[] = $sth->fetchAll();

if(isset($_POST["showData"])){

echo "<table style='border: 2px solid black'>
<tr>
<th style='border: 1px solid red'>ID</th>
<th style='border: 1px solid black'>User Name</th>
</tr> <tbody>";
foreach ($result as $key => $value) {
    foreach ($value as $key1 => $value1) {
?>
<tr>
<td><? echo $value1['ID']; ?></td>
<td><? echo $value1['userName']; ?></td>
</tr>
<?php
    }

}
echo "</tbody></table>";
}
?>

9 Comments

This does not provide an answer to a question, should be a comment. Please see When should I comment?. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker.
sorry...but i am new to stackoverflow community...ok i will delete this answer (comment)
@Krysan sure i'll :)
@SamBoHamdan well I am also a beginner in PHP,but I had solved this problem when I was learning PHP...
nice i hope that u can help me 2 @Krysan , u can find the pic in the Question
|

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.