Reza Sedagheh Reza Sedagheh - 4 days ago 6
MySQL Question

MySQL select region

I am designing a simple Blog, at the first I read 6 records (Image + Title) and at the rest of them I want to read 7 through 10.
for 6 first I am using this code:

$sql = "SELECT * FROM blogcontents ORDER BY bid DESC";
$result = mysql_query($sql) or die("Error Query [" . $sql . "]");
$num_rows = mysql_num_rows($result);

for ($i = 0; $i < 6; $i++)
{
$row = mysql_fetch_array($result);
echo '<li class="navblog"><a href="blogpage.php?id= ' . $row['bid'] . '"><div class="blogwrap"><img class="img-thumbnail img-responsive bphoto" src="images/blog/' . $row['btimg'] . '" alt="Smiley face" height="200" width="300"><span class="sizeoftitle">' . $row['btitle'] . '</span></div></a></li>';
}


But I don't know how can I read from 7 through 10.

enter image description here

Answer

You could use limit and offset (instead the loop limitation) for retrieve the rows you need a firts select with 6 rows

"SELECT * FROM blogcontents ORDER BY bid DESC limit 6 ";

and a second select with the others 4 rows starting form 5 (offeset is 0 based)

"SELECT * FROM blogcontents ORDER BY bid DESC limit 5,4 ";

or after your first loop retrive the others

 while (  $row = mysql_fetch_array($result))
  {
   echo '<li class="navblog"><a href="blogpage.php?id= ' . $row['bid'] . '"><div class="blogwrap"><img class="img-thumbnail img-responsive bphoto" src="images/blog/' . $row['btimg'] . '" alt="Smiley face" height="200" width="300"><span class="sizeoftitle">' . $row['btitle'] . '</span></div></a></li>';
 }

based on your code in comment this should work

$sql2 = "SELECT * 
          FROM blogcontents
           LIMIT  5, 4"; 
           $result2 = mysql_query($sql2) or die("Error Query [" . $sql2 . "]"); 

and you don't need for loop .. you can use while

while (  $row = mysql_fetch_array($result2)) { 
          echo '<p><a href="blogpage.php?id= ' . $row['bid'] . '">' . $row['btitle'] . '</a></p>'; 
 } ?> 
Comments