Results 1 to 1 of 1

Thread: While Loop mysql

  1. #1
    Join Date
    Mar 2009
    Posts
    42
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default While Loop mysql

    Does anyone know where I am going wrong here:

    I am trying to pull all data from mysql, but I am only getting the first entry?

    Any ideas?

    Code:
    <?php
    // define number of columns in table
    define('COLS', 2);
    // set maximum number of records per page
    define('SHOWMAX', 15);
    
    // prepare SQL to get total records
    $getTotal = "SELECT * FROM `products` WHERE id = '" . mysql_real_escape_string($_GET['id']) . "'";
    // submit query and store result as $totalPix
    $total = mysql_query($getTotal);
    $row = mysql_fetch_row($total);
    $totalPix = $row[0];
    // set the current page
    $curPage = isset($_GET['curPage']) ? $_GET['curPage'] : 0;
    // calculate the start row of the subset
    $startRow = $curPage * SHOWMAX;
    // prepare SQL to retrieve subset of image details
    $sql = "SELECT * FROM products LIMIT $startRow,".SHOWMAX;
    // submit the query (MySQL original)
    $result = mysql_query($sql) or die(mysql_error());
    // extract the first record as an array
    $row = mysql_fetch_assoc($result);
    // get the name for the main image
    if (isset($_GET['img/products/'])) {
      $mainImage = $_GET['img/products/'];
      }
    else {
      $mainImage = $row['filename'];
      }
    // get the dimensions of the main image
    $imageSize = getimagesize('img/products/'.$mainImage);
    ?>
    
    <?php
    
    // query db
    if (isset($_GET['id'])) {
        $sql = "SELECT * FROM `category` WHERE category_id = '" . mysql_real_escape_string($_GET['id']) . "'"; // from category get id
        if ($result = mysql_query($sql)) {
          if (mysql_num_rows($result)) {
            $row = mysql_fetch_assoc($result);
            echo "<h2>" . $row['category_id'] . "</h2>";
    		} else {
    		echo "Something is wrong!";
          }
        }
      }
    ?>
    	<div class="datadivleftcontent">
    
    <?php
    // query db
    if (isset($_GET['id'])) {
        $sql = "SELECT * FROM `products` WHERE id = '" . mysql_real_escape_string($_GET['id']) . "'"; // from decades get ID
        if ($result = mysql_query($sql)) {
          if (mysql_num_rows($result)) {
            $row = mysql_fetch_assoc($result);
    
            echo "<h4>" . $row['name'] . "</h4>"; // echo date results as format "h4"
    		echo "<p>" . $row['description'] . "</p>"; // echo content as p format "body text"
    
    /*
    echo "<a href='img/products/{$row['filename']}' >"; echo "<img src='img/products/thumbnails/{$row['filename']}' >"; "/></a>"; 
    */
    
    		} else {
    		echo "Something is wrong!";
          }
        }
      }
    ?>
    
    <a href="img/products/<?php echo $row['filename']; ?>" rel="lightbox" title="<?php echo $row['name']; ?>"><img src="img/products/thumbnails/<?php echo $row['filename']; ?>" alt="<?php echo $row['name']; ?>" title="<?php echo $row['name']; ?>" width="156" height="109" /></a>
    
    
    <?php
    
    $row = mysql_fetch_assoc($result);
    // increment counter after next row extracted
    $pos++;
    // if at end of row and records remain, insert tags
    if ($pos%COLS === 0 && is_array($row)) 
    echo '';
    
    
    // new loop to fill in final row 
    
    while ($pos%COLS) {
    echo '';
    $pos++;
    }
    
    ?>
    
    <?php echo $startRow+1;
    		  if ($startRow+1 < $totalPix) {
    		    echo ' to ';
    			if ($startRow+SHOWMAX < $totalPix) {
    			  echo $startRow+SHOWMAX;
    			  }
    			else {
    			  echo $totalPix;
    			  }
    			}
    		  echo " of $totalPix";
    		  ?> Prints
    
    
    
    <?php
    // create a back link if current page greater than 0
    if ($curPage > 0) {
    echo '| <a href="'.$_SERVER['PHP_SELF'].'?curPage='.($curPage-1).'">Previous Page</a>';
    }
    // otherwise leave the cell empty
    else {
    echo '|';
    }
    ?>
    
    <?php
    // pad the final row with empty cells if more than 2 columns
    if (COLS-2 > 0) {
    for ($i = 0; $i < COLS-2; $i++) {
    echo '';
    }
    }
    ?>
    
    <?php
    // create a forwards link if more records exist
    if ($startRow+SHOWMAX < $totalPix) {
    echo '<a href="'.$_SERVER['PHP_SELF'].'?curPage='.($curPage+1).'">Next Page</a>';
    }
    // otherwise leave the cell empty
    else {
    echo '';
    }
    ?>
    Last edited by john0611; 12-02-2009 at 02:13 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •