1

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

Kevin Stich
  • 773
  • 1
  • 8
  • 24
Reza Sedagheh
  • 43
  • 1
  • 8
  • 1
    Please update to the new MySQL driver - `mysqli`. [See here.](http://stackoverflow.com/questions/548986/mysql-vs-mysqli-when-using-php) It's deprecated in PHP 5.5 and removed in 7.0 – Kevin Stich Dec 01 '16 at 19:35

3 Answers3

0

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>'; 
 } ?> 
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • I alredy used before but it does not work, here you can see: $sql2 = "SELECT * FROM blogcontents LIMIT 4 OFFSET 6"; $result2 = mysql_query($sql2) or die("Error Query [" . $sql2 . "]"); for ($j = 0; $j < $num_rows-1; $j++) { $row = mysql_fetch_array($result2); echo '

    ' . $row['btitle'] . '

    '; } ?>
    – Reza Sedagheh Dec 01 '16 at 19:40
  • as you see in the picture, the selected records again retrieve from the latest records! but I just want the rest of records... – Reza Sedagheh Dec 01 '16 at 19:46
  • I don't be able to read your language for me all the sign are same .. but try adjust the param for limit and offeset .. anyway i have update the asnwer with some suggestion – ScaisEdge Dec 01 '16 at 19:48
0

Explain By Image

Please look at the picture I explained my question there...

Community
  • 1
  • 1
Reza Sedagheh
  • 43
  • 1
  • 8
0

The result_array() function will format the mysql result into php array.

    function result_array($sql)
    {
        $result = array();
        $query = mysql_query($sql) or die("Error: " . mysql_error());
        while ($data = mysql_fetch_array($query)) {
            $result[] = $data;
        }
        $rows = count($result);
        if ($rows) {
            $total_global_rows = count($result);
            $total_inner_rows = count($result[0]);
            $count_total_inner_rows = $total_inner_rows / 2;

            for ($i = 0; $i < $total_global_rows; $i++) {
                for ($j = 0; $j < $count_total_inner_rows; $j++) {
                    unset($result[$i][$j]);
                }
            }
        }
        return $result;
    }

    $sql = "SELECT * FROM blogcontents ORDER BY bid DESC LIMIT 10";

    $blogPosts = result_array($sql);

    //print till 6th and break the loop
    foreach ($blogPosts as $key => $row) {
        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>';
        if ($key > 5) {
            break;
        }
    }

    //print from 7th to the end
    foreach ($blogPosts as $key => $row) {
        if ($key > 5) {
            echo $row['btitle'];
        }
    }
mainuljs
  • 151
  • 3