How can I export values from a database and echo all it separated by an <hr />, except the first and the last results??
$stmt = $db->prepare("SELECT `title`, `content` FROM `news` ORDER BY title ASC");
$stmt->execute(array($category));
foreach($stmt as $row){
echo "<p>" . $row['title'];
echo $row['content'] . "</p> <hr />";
}
it outputs:
<p>title1
content1</p>
<hr>
<p>blabla
blabla</p>
<!--My question is how to remove this hr-->
<hr>
I tried to use a for cicle, but it didn't work.
thanks in advance
You could try something like this:
$stmt = $db->prepare("SELECT `title`, `content` FROM `news` ORDER BY title ASC");
$stmt->execute(array($category));
$numResults = $stmt->rowCount();
$counter = 0;
foreach($stmt as $row){
echo "<p>" . $row['title'];
if (++$counter == $numResults) {
// last row
echo $row['content'] . "</p>";
} else {
// not last row
echo $row['content'] . "</p> <hr />";
}
}
That will count the rows up till the last one, and skip writing the HR for that one.