Skip to main content
April 12, 2010
Answered

Displaying a subset of records is limiting the information I can show

  • April 12, 2010
  • 1 reply
  • 2167 views

Hello All,

I'm developing my photo gallery's TOC page and when I began, I had lots of data I wanted to display and through the use of lookup tables and the proper logic in my SELECT query, I could show a vertical list of galleries that matched a specific category. With each was listed two sample thumbnails, title, description, model, photographer and make-up artist names. I even was able to set the page title and header to display the proper information.

An additoinal SELECT query counted the number of galleries in the main table that were relevant to the present query string value.

Another SELECT query counted the number of photos in each different gallery photoset and displayed that information in EACH gallery's title.

The description is in reverse to the flow of the code, keep in mind. Here is the code (the $spec variable is that of the query string.):

//get the total number of photos in a gallery photoset
$conn = dbConnect('query');
$getTotal = "SELECT COUNT(*)
            FROM photos, galleries
            WHERE galleries.g_spec = '$spec%'
            AND galleries.g_id = photos.g_id ";
$total = $conn->query($getTotal);
$row = $total->fetch_row();
$totalPix = $row[0];

// get the number of galleries
$conn = dbConnect('query');
$getGtotal = "SELECT COUNT(*)
            FROM galleries
            WHERE galleries.g_spec = '$spec%' ";
$g_total = $conn->query($getGtotal);
$row = $g_total->fetch_row();
$galTotal = $row[0];

//get gallery thumbnails, name, description, subject, category, model, photographer, mua
$conn = dbConnect('query');
$sql = "SELECT g_thumb1, g_thumb2, g_name, g_desc, subject_id, category_id, model_name, photographer_name, mua_name
        FROM galleries, gallery_spec, model_names, photographer_names, mua_names
        WHERE galleries.g_spec = '$spec%'
        AND gallery_spec.g_spec = galleries.g_spec
        AND model_names.model_id = galleries.g_model
        AND photographer_names.photographer_id = galleries.g_photographer
        AND mua_names.mua_id = galleries.g_mua
        ORDER BY g_id DESC ";
$result = $conn->query($sql) or die(mysqli_error());
$galSpec = $result->fetch_assoc();

*** end of code.

Anyhow, this worked wonderfully. However, as I add galleries, this list will get quite long. So, I wanted to add navigation which would break up the number of galleries into set quantities.

When I introduced parameters to LIMIT the number of queries displayed based on a variable called SHOWMAX, my displayed data got all botched. admittedly, I'm adapting code from David Powers' book PHP Solutions and am learning as I go. I gave up on displaying all of the data shown above and went the simple route, figuring I'd add more features as I learned. This necessitated restructuring my database a bit.

Here is the code I added that allows for navigation based on a limit of 3 entries per page:


// set maximum number of records per page
define('SHOWMAX', 3);

// get the number of galleries

$conn = dbConnect('query');
$getGtotal = "SELECT COUNT(*)
            FROM galleries ";
$g_total = $conn->query($getGtotal);
$row = $g_total->fetch_row();
$galTotal = $row[0];

// set the current page
$curPage = isset($_GET['curPage']) ? $_GET['curPage'] : 0;

// retrieve subset of galleries
$conn = dbConnect('query');

// calculate the start row of the subset
$startRow = $curPage * SHOWMAX;

$sql = "SELECT *
        FROM galleries
        ORDER BY g_id DESC
        LIMIT $startRow,".SHOWMAX;
$result = $conn->query($sql) or die(mysqli_error());
$galSpec = $result->fetch_assoc();

*** end of code.

Along with some navigation code in the body:

<div id="header4">
    <p>Displaying <?php echo $startRow+1;
          if ($startRow+1 < $galTotal) {
            echo ' to ';
            if ($startRow+SHOWMAX < $galTotal) {
              echo $startRow+SHOWMAX;
              }
            else {
              echo $galTotal;
              }
            }
          echo " of $galTotal";
          if ($curPage > 0) {
                      echo '<a href="'.$_SERVER['PHP_SELF'].'?curPage='.($curPage-1).'"> Back </a>';
                      }
          if ($startRow+SHOWMAX < $galTotal) {
                      echo '<a href="'.$_SERVER['PHP_SELF'].'?curPage='.($curPage+1).'"> Next </a>';
                      }
          ?>

I can now display my gallery information in groups of 3 per page.

THE PROBEM IS that when I add additional queries, say to get the subject and category information, model, photograpger and or make-up artist information, it doesn't always come up, sometimes it messes up the display, and in all cases, this additional information disappears when I navigate from subset to subset. This is why I have been asking about carrying values from one page to the next.

I am absolutely stonewalled and am going to have to put the galleries into service sans navigation until I can figure it out. I'm willing to even hire someone for advice. I just can not make this work. Yes, I realize it's complex for someone of my skill level, but I have no choice, I have to make this work.

Alternate forms of navigation/showing subsets of records would be greatly appreciated. ANY help, suggestions or guidance will be greatly appreciated.

Thank you for reading through all this.

Most sincerely,

wordman

This topic has been closed for replies.
Correct answer bregent

B,

The info in the title bar and page header both contain two bits of info, subject and category, both of which are pulled from the DB. I could hard code them in an includes file with 11 different conditional clauses (thats how many combos I have) and avoid the DB for these data altogether, but that seems like it's not the right way to do it. My fantasy is to pull all relevant page data from the DB.

I can't pass it page to page, even though that is what I would like to do (our earlier discussions about query strings). I believe the way the adapted code works is to pull all the records at once and then scroll through the array. I guess this because that seems why the title and header info go absent, I don't think the page re-queries on navigating, I think it just loads info from the array.

So, I added a separate query to pick this info from the DB, and it bombs the page. Take it out, page works. No matter where in the code I put it (and I have found out this does matter) it bombs the page.

Splitting headache, anyone?

Many thanks!

Sincerely,

wordman


Again you are misunderstanding. I'm not suggesting hardcoding any values. I am only asking if you are/were trying to pass the actual subject and category descriptions, or the id's of category and subject so you can requery the database on each page.

> I don't think the page re-queries on navigating, I think it just loads  info from the array.

I don't think so. Just look at the SQL. It's using the limit and offset values which means to me that it's only returning the rows for the current page. What array are you talking about?

>So, I added a separate query to pick this info from the DB, and it bombs  the page.

I really believe you are going about this all wrong. Right now you are searching for solutions to specific problems without having a solid foundation in the technologies involved. Trying to build as you learn is a recipe for disaster and frustration. Stop what you are doing for a while. Work on some php and SQL tutorials that are unrelated to this project. Work through them until you understand what each line of code is doing.

Right now you are headed down a rat hole. You may get most of it to work, but you will be plagued by bugs and scripts that are difficult to maintain.

1 reply

Participating Frequently
April 12, 2010

In your first query (the one that you say does not function properly with paging) you are ordering the results by g_id. Is this column name used in any of the other tables in that query?

I would also add more order by criteria to ensure that order of the result set is returned consistently. Paging relies on consistent ordering and SQL results are not guaranteed to be ordered unless you specify in the order by clause.

April 12, 2010

Bregent,

I was going to delete this thread but as we're already here, I'm simply going to say that I need to take a step back from this and look at it from a different angle. The code I am using, which was adapted fromt he code in David Powers' book, 'PHP Solutions' is being asked to do WAY more for me than it does for David's gallery in the book. I'm in way over my head...and have no choice but to try a new approach.

Any ideas where I could get some info on paginating subsets of records?

Many thanks for your kind help!

Sincerely,

wordman

Participating Frequently
April 12, 2010

I like David's technique because it only returns the records for the current page. Have you discussed this with him?

I use ASP and ADO recordset paging typically returns all rows in the query. You then point the recordset cursor to the row that is to be displayed on the current page. For example:

Query returns 1000 rows

Display 100 rows per page

For page 1 you retrieve all 1000 rows

Your pointer is set to display from position 1

Your loop is set to display 100 rows

For page 2 you retrieve all 1000 rows

Your pointer is set to  display from position 101

Your loop is set to display 100 rows

In ADO you set the PageSize and the AbolutePage property of the recordset. Not sure how you would accomplish this in php/mysql but I'm sure you can.