database retrieval problem while formatting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mpar612

    database retrieval problem while formatting

    Hi everyone,

    The code posted below retrieves all of the rows from a database table.
    It paginates them by limiting to 20 results per page. It is supposed
    to print 5 rows of results with 4 results per row. Everything works
    fine, except the first row returned from the database is never printed.
    It doesn't matter how the results are ordered or how many rows are in
    the db, the first row that is supposed to be printed is not printed.

    Does anyone have any thoughts? I have been going nuts over this for
    the past day.

    Any feedback would be great. Thanks!

    <?php

    $limit = 20;
    $query_count = "SELECT * FROM lounge_products ";
    $result_count = mysql_query($qu ery_count);
    $totalrows = mysql_num_rows( $result_count);

    $PHP_SELF = $_SERVER['PHP_SELF'];

    if(!isset($_GET['page'])){ // Checks if the $page variable is empty
    (not set)
    $page = 1; // If it is empty, we're on page 1
    } else{
    $page = $_GET['page'];
    }

    $limitvalue = $page * $limit - ($limit );
    // Ex: (page2 * 5(items per page) = 10) - 5 = 5 <- data starts at 5

    $query = "SELECT * FROM lounge_products ORDER BY artist_name DESC
    LIMIT $limitvalue, $limit";
    $result = mysql_query($qu ery) or die("Error: " . mysql_error());

    if(mysql_num_ro ws($result) == 0){
    echo("Nothing to Display!");
    }
    echo("<table>") ;

    while($row = mysql_fetch_arr ay($result)){

    $counter = 0;
    $cells_per_row = 4;
    while($row=mysq l_fetch_array($ result))
    {
    $counter++;
    print $counter;
    if(($counter % $cells_per_row) == 1) { echo '<tr>'; }
    echo '<td width=60 valign=top>' . ('<img src="'.$row[image] .'"
    height="60" width="60"><br> '.$row[artist_name]) . '</td>';
    if(($counter % $cells_per_row) == 0) { echo '</tr>'; }
    }
    // just in case we haven't closed the last row
    // this would happen if our result set isn't divisible by
    $cells_per_row
    if(($counter % $cells_per_row) != 0) { echo '</tr>'; }

    } //closing while loop

    echo("</table>");

    if($page != 1){
    $pageprev = $page - 1; //decrementing $page-- does not work on
    all php configurations.


    echo("<a href=\"$PHP_SEL F?page=$pagepre v\">PREV</a");
    }else{
    echo("PREV");
    }

    $numofpages = $totalrows / $limit;


    #echo "<br>", $totalrows;
    #exit;

    for($i = 1; $i <= $numofpages; $i++){
    /* This for loop will add 1 to $i at the end of each pass until
    $i is greater
    than $numofpages. */
    if($i == $page){
    echo($i." ");
    }else{
    echo("<a href=\"$PHP_SEL F?page=$i\">$i</a");
    }
    } //code above has been decoded lozza


    if(($totalrows % $limit) != 0){
    if($i == $page){
    echo($i." ");
    }else{
    echo("<a href=\"$PHP_SEL F?page=$i\">$i</a");
    }
    }

    if(($totalrows - ($limit * $page)) 0){
    $pagenext = $page + 1; //incrementin $page like $page++ does
    not work in all enviroments

    echo("<a href=\"$PHP_SEL F?page=$pagenex t\">NEXT</a>");
    }else{
    echo("NEXT");
    }

    mysql_free_resu lt($result);

    ?>

  • Gordon Burditt

    #2
    Re: database retrieval problem while formatting

    >The code posted below retrieves all of the rows from a database table.
    >It paginates them by limiting to 20 results per page. It is supposed
    >to print 5 rows of results with 4 results per row. Everything works
    >fine, except the first row returned from the database is never printed.
    It doesn't matter how the results are ordered or how many rows are in
    >the db, the first row that is supposed to be printed is not printed.
    >
    >Does anyone have any thoughts? I have been going nuts over this for
    >the past day.
    >
    >Any feedback would be great. Thanks!
    >
    ><?php
    >
    $limit = 20;
    $query_count = "SELECT * FROM lounge_products ";
    $result_count = mysql_query($qu ery_count);
    $totalrows = mysql_num_rows( $result_count);
    >
    $PHP_SELF = $_SERVER['PHP_SELF'];
    >
    if(!isset($_GET['page'])){ // Checks if the $page variable is empty
    >(not set)
    $page = 1; // If it is empty, we're on page 1
    } else{
    $page = $_GET['page'];
    }
    >
    $limitvalue = $page * $limit - ($limit );
    // Ex: (page2 * 5(items per page) = 10) - 5 = 5 <- data starts at 5
    >
    $query = "SELECT * FROM lounge_products ORDER BY artist_name DESC
    >LIMIT $limitvalue, $limit";
    $result = mysql_query($qu ery) or die("Error: " . mysql_error());
    >
    if(mysql_num_ro ws($result) == 0){
    echo("Nothing to Display!");
    }
    echo("<table>") ;
    >
    while($row = mysql_fetch_arr ay($result)){
    ***ABOVE YOU FETCH THE FIRST ROW***
    $counter = 0;
    $cells_per_row = 4;
    while($row=mysq l_fetch_array($ result))
    ***ABOVE YOU FETCH THE SECOND ROW, WIPING OUT THE ONLY COPY OF THE FIRST ROW***
    {
    $counter++;
    > print $counter;
    if(($counter % $cells_per_row) == 1) { echo '<tr>'; }

    Comment

    • mpar612

      #3
      Re: database retrieval problem while formatting

      That was it. Thanks!

      Comment

      Working...