Page 1 of 1

Php buffer problem

Posted: Mon Dec 05, 2011 12:04 am
by shaunak1234

Code: Select all

<?php
$dbhost = '**********';
$dbuser = '**********';
$dbpass = '**********';
$rec_limit = 16;

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
mysql_select_db('*******');
/* Get total number of records */
$sql = "SELECT count(srno) FROM images ";
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];

if( isset($_GET{'page'} ) )
{
   $page = $_GET{'page'} + 1;
   $offset = $rec_limit * $page ;
}
else
{
   $page = 0;
   $offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);

$sql = "SELECT name ".
       "FROM images ".
       "LIMIT $offset, $rec_limit";

$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
?>
<table cellspacing="10" border="0" cellpadding="0">
<tr>
<?php
$i=0;
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
$i=$i+1;
if($i>4)
{
$n=$row['name'];
        echo "</tr><tr><th width='152px' height='152px'>";
        echo "<a href='http://wallpapers.everything.org.in/images/". $n ."'><img src='http://wallpapers.everything.org.in/images/" .$n."'></a>";     
        echo "</div></th>";
        $i=1;
}
        else{
        $n=$row['name'];
        echo "<th width='152px' height='152px'>";
        echo "<a href='http://wallpapers.everything.org.in/images/". $n ."'><img  src='http://wallpapers.everything.org.in/images/" .$n."'></a>";     
        echo "</div></th>";
        }
        
} 
echo "</tr></table>";
if( $page > 0 )
{
   $last = $page - 2;
        echo "<a href=\"$_PHP_SELF?page=$last\" style='background:black;color:white;'>Last 16 Records</a> |";
   echo "<a href=\"$_PHP_SELF?page=$page\" style='background:black;color:white;'>Next 16 Records</a>";
}
else if( $page == 0 )
{
   echo "<a href=\"$_PHP_SELF?page=$page\" style='background:black;color:white;'>Next 16 Records</a>";
}
else if( $left_rec < $rec_limit )
{
   $last = $page - 2;
   echo "<a href=\"$_PHP_SELF?page=$last\" style='background:black;color:white;'>Last 16 Records</a>";
}
mysql_close($conn);
?>
I need to show 16 images on a page and I achieved it through the above code,
all images are thumbnails! but most of them end up unloaded
I never understood php buffer how can I buffer the output so that everything loads up!

Re: Php buffer problem

Posted: Fri Dec 09, 2011 12:25 pm
by twinedev
What do you mean by "most of them end up unloaded"?

Do you mean that the source code shows that the <img tags are there, but when viewed in a browser they come up as missing images?

-Greg

Re: Php buffer problem

Posted: Fri Dec 09, 2011 1:46 pm
by twinedev
Ended up realizing the URL from the links in the code, and from what I am seeing, it is only generating source code for four images, and while there are several issues with the code, it should be looping properly to display all that it can get from the DB, so first thing I would check is how many rows are there total. After that, if there are more than 4, run the query manually against the database to see the results.

Also, there is nothing in this code that is using buffering, so you may be referring to something else. Not sure without seeing all of the code being used.

Some suggestions for the code:

1. use actual thumbnails instead of just displaying the full image at a smaller size, this will make the page load faster.
2. Use <td> instead of <th> TH is designed for the heading of a table
3. You are closing a <div> inside each table cell but it isn't opened
4. You are using a variable $_PHP_SELF which doesn't exist. what you are probably looking for is $_SERVER['PHP_SELF'] however as a general rule, since this is something that can be manipulated by the visitor, you should never directly output it
5. you are trying to use $_GET{'page'} when it should be $_GET['page']
6. Speaking of this, you might as well use actual page numbers, currently your code says that to get to page #2 of data, you are going to page=0 in the URL, Ok, maybe it is cause I'm a programmer, but seeing that would mess me up since it doesn't match logically ;-)
7. When you are on the last page of results, your code doesn't take into consideration that the last row may not have 4 images, should pad the table with empty table cells to finish the row for best results.

Here is a sample of pretty much the same code, just cleaned up, and done in a way that breaks the logic of the code up from the presentation quite a bit. By the time you get outputting code, all information is processed, so there it a lot less PHP code in the middle of your output. This also uses a system of ?page = the real page number (and if not given, defaults to page 1) Also many issues above have been addressed.

Code: Select all

<?php
    $dbhost = '**********';
    $dbuser = '**********';
    $dbpass = '**********';
    $rec_limit = 16;

    $conn = mysql_connect($dbhost, $dbuser, $dbpass)
        or die('Could not connect: ' . mysql_error());
    mysql_select_db('*******');

    /* Get total number of records */
    $sql = "SELECT count(`srno`) FROM `images` ";
    $rs_count = mysql_query($sql, $conn )
        or die('Could not get data: ' . mysql_error());

    $rec_count = mysql_result($rs_count,0);
    mysql_free_result($rs_count); unset($rs_count);

    $max_pages = ceil($rec_count/$rec_limit);
    
    $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
    if ($page > $max_pages) {
        $page = $max_pages;
    }
    elseif ($page<1) {
        $page = 1;
    }

    $offset = ($page-1) * $rec_limit;

    $ary_images = array();
    $sql = 'SELECT `name` FROM `images` LIMIT '.$offset.','.$rec_limit;
    $rs_names = mysql_query($sql)
        or die('Could not get data: ' . mysql_error());

    if (mysql_num_rows($rs_names)>0) {
        while ($ary_temp = mysql_fetch_assoc()) {
            $ary_images[] = $ary_temp['name'];
        }
        mysql_free_result($rs_names); unset($rs_names);
    }

    // nav setup
    
    if ($max_pages > 1) {
        if ($page > 1) {
            $first_link = '<a href="'.$_SERVER['REQUEST_URI'].'?page=1" style="background:black;color:white;">First</a>';
            $prev_link = '<a href="'.$_SERVER['REQUEST_URI'].'?page='.($page-1).'" style="background:black;color:white;">Prev</a>';
        }
        else {
            $first_link = 'First';
            $prev_link = 'Prev';
        }

        if ($page < $max_pages) {
            $last_link = '<a href="'.$_SERVER['REQUEST_URI'].'?page='.$max_pages.'" style="background:black;color:white;">Last</a>';
            $next_link = '<a href="'.$_SERVER['REQUEST_URI'].'?page='.($page+1).'" style="background:black;color:white;">Next</a>';
        }
        else {
            $last_link = 'Last';
            $next_link = 'Next';
        }

        if ($max_pages>2) {
            $nav_bar = $first_link.' | '.$prev_link.' | Page '.$page.' of '.$max_pages.' | '.$next_link.' | '.$last_link;
        }
        else {
            // Only two pages, so FIRST/LAST not needed since NEXT/PREV accomplish the same
            $nav_bar = $prev_link.' | Page '.$page.' of '.$max_pages.' | '.$next_link;
        }
    }
    else {
        $nav_bar = ''; 
    }
?>

<table cellspacing="10" border="0" cellpadding="0">
    <tr>
        <?php foreach($ary_images as $key=>$image): ?>
            <?php if ($key % 4 == 0) { echo '</tr><tr>'; } ?>
            <td width="152" height="152">
                <a href="/images/<?php echo rawurlencode($image); ?>"><img src="/images/<?php echo rawurlencode($image); ?>"></a>
            </td>
        <?php endforeach; ?>
        <?php if($key+1 % 4 != 0): // Didn't finish on complete row, so need to pad ?>
            <?php echo str_repeat('<td width="152" height="152">&nbsp;</td>',4-(($key+1)%4)); ?>
        <?php endif; ?>
    </tr>
</table>