Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Richard Duffy
16,488 PointsHelp
<?php
include_once 'connect.php';
doDB();
$get = "SELECT `id`, `account_title` FROM `account_list`";
$query = mysqli_query($mysqli,$get)or die(mysqli_error($mysqli));
while($row = mysqli_fetch_array($query)){
$name = $row['account_title'];
$id = $row['id'];
}
$display_block = <<<END_OF_TEXT
<table border="5" cellpadding="10">
<th>Account Name</th>
<th>Account ID</th>
<tr>
<td><a href="show_account.php?account=$id">$name</a></td>
<td>$id</td>
</tr>
END_OF_TEXT;
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php echo $display_block;?>
</body>
</html>
This is only displaying one result, I need it to display all of the results. Any Suggestions?
Richard Duffy
16,488 PointsSorry I don't quite understand what you mean.
2 Answers
Gareth Borcherds
9,372 PointsYeah your while loop starts with { and ends with }. Yours is ending before you create the output code called $display_block =
Just move the } to be after the setting of that variable and change it to $display_block .= so that the period before the equal means to append the string so that you actually appends the strings together instead of over writing them.
Also, your display text is incomplete, you open a <table> tag, but you never close it. So after all the other changes, you could still have issues with your output HTML.
Gareth Borcherds
9,372 PointsYou also should be setting text for your output block a little differently... here is another way to write the table output better
<?php
include_once 'connect.php';
doDB();
$get = "SELECT `id`, `account_title` FROM `account_list`";
$query = mysqli_query($mysqli,$get)or die(mysqli_error($mysqli));
$display_block =
'<table border="5" cellpadding="10">
<tr>
<th>Account Name</th>
<th>Account ID</th>
</tr>'; //Beginning of table code needs to happen only once, so we do it out of loop.
while($row = mysqli_fetch_array($query)){
$name = $row['account_title'];
$id = $row['id'];
$display_block .= '<tr>
<td><a href="show_account.php?account='.$id.'">'.$name.'</a></td>
<td>$id</td>
</tr>';
}
$display_block .= '</table>'; //Ending of table code only needs to happen once, so we do it out of loop.
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php echo $display_block;?>
</body>
</html>
This is going to output a much cleaner html block for your table because now you are creating the html once for the items that appear only once in your table. Another way that I like to do things would actually be.
<?php
include_once 'connect.php';
doDB();
$get = "SELECT `id`, `account_title` FROM `account_list`";
$query = mysqli_query($mysqli,$get)or die(mysqli_error($mysqli));
$display_block = ''; //I like setting a blank variable because you don't want to try and append to a variable that doesn't exist in your loop
while($row = mysqli_fetch_array($query)){
$name = $row['account_title'];
$id = $row['id'];
$display_block .= '<tr>
<td><a href="show_account.php?account='.$id.'">'.$name.'</a></td>
<td>$id</td>
</tr>';
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table border="5" cellpadding="10">
<tr>
<th>Account Name</th>
<th>Account ID</th>
</tr>
<?php echo $display_block;?>
</table>
</body>
</html>
This way I moved all the static code to just be straight html and it keeps your PHP code doing dynamic stuff. Hope this helps :)
Richard Duffy
16,488 PointsWow thanks! It worked!
Hayden Taylor
5,076 PointsHayden Taylor
5,076 PointsA couple things here.
$name and $id need to be arrays to capture more than 1 value.
Another is $display_block is only be constructed to output once.
What you might want to do is add that table code to the html and just make a php loop which just adds the table cells with each name and id (assuming you made them an array) that way you can iter through each of the array indices.