I have the following code, to split the resulting array up by the Category field.
$query = "SELECT * FROM livestock
WHERE Category = 'monitor'
OR Category ='bearded'
OR Category ='chameleon'
OR Category ='skink'
OR Category ='small'
OR Category ='medium'
OR Category ='terrestrial'
OR Category ='arboreal'
OR Category ='leopard'";
$result = mysqli_query($con, $query) or die(mysqli_error($con));
$rows = array();
while ( $row = mysqli_fetch_array($result)){
$rows[$row['Category']][] = $row;}
I then echo this out to the page in several places using the following code:
foreach($rows['MONITOR'] as $row)
{
$commondraft = $row['Name'];
$current = $row['Category'];
if($current==$previous){
echo"<a href='/stocklist/".$row['id']."/".commonName($commondraft)."' class='row'>";
echo"<div class='common'>".$row['Name']."</div>";
echo"<div class='descr'>".$row['Description']."</div>";
echo"<div class='sex'>".$row['Sex']."</div>";
echo"<div class='age'>".$row['Age']."</div>";
echo"<div class='size'>".$row['Size']."</div>";
echo"<div class='origin'>".$row['Origin']."</div>";
echo"<div class='scientific'>".$row['Scientific']."</div>";
echo"<div class='prices'>".$row['Price']."</div>";
echo"</a>";
}
$previous = $current;
}
echo"</div>";
But each time i echo this out using the foreach() loop it misses the first record.
How can i work a way around this and what's causing it?
Thanks in advance
if($current==$previous)? What is$previousset to at the start of the loop?Categorychanged. So thanks! :D