6 Replies Latest reply on Jul 17, 2007 11:33 AM by MikeL7

    PHP photos display

    MikeL7 Level 1
      I have made a simple photo album and could use some ideas how to display the pics 2 or 3 wide. My code displays in a table with either all pics horizontal or vertical in a row, but would like to put a </tr> in after every 2 or 3 pics.
      Code:

      <?php

      include ("db.php");

      $picturesql = "SELECT * FROM images ORDER BY img_id DESC;";
      $pictureres = mysql_query($picturesql);

      while($picrow = mysql_fetch_assoc($pictureres)) {
      $picture .= "<td><img src='pics/" . $picrow['name'] . "' width='250' height='175'><br />" . $picrow['description'] . "</td>";
      }

      echo "<table cellpadding='10'><tr>";
      echo $picture;
      echo "</tr></table>";
      ?>
        • 1. Re: PHP photos display
          Level 7
          MikeL7 wrote:
          > would like to put a </tr> in after every 2 or 3 pics.

          echo "<table cellpadding='10'><tr>";
          // initialize cell counter
          $pos = 0;
          // specify number of pictures per row
          $cols = 2;

          while($picrow = mysql_fetch_assoc($pictureres)) {
          // increment the counter
          $pos++;

          $picture .= "<td><img src='pics/" . $picrow['name'] . "' width='250'
          height='175'><br />" . $picrow['description'] . "</td>";

          // if at end of row and records remain, insert row tags
          if ($pos/$cols === 0 && is_array($picrow)) {
          $picture .= '</tr><tr>';
          }
          }

          // fill in any empty cells in final row
          while ($pos%$cols) {
          $picture .= '<td> </td>';
          $pos++;
          }

          echo $picture;
          echo "</tr></table>";

          You can find a full description of this technique in chapter 12 of my
          book, "PHP Solutions" ( http://foundationphp.com/phpsolutions/).

          --
          David Powers, Adobe Community Expert
          Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
          Author, "PHP Solutions" (friends of ED)
          http://foundationphp.com/
          • 2. Re: PHP photos display
            Level 7
            .oO(David Powers)

            > // if at end of row and records remain, insert row tags
            > if ($pos/$cols === 0 && is_array($picrow)) {

            Hmm. Shouldn't this be

            if ($pos%$cols === 0 && is_array($picrow)) {
            ...

            ?

            But then there's another problem. I don't think the array check is going
            to work. $picrow will always be an array while inside the loop, so there
            will be an entirely empty row inserted at the end of the table if the
            number of images is a multiple of $col.

            Micha
            • 3. Re: PHP photos display
              Level 7
              Michael Fesser wrote:
              > Hmm. Shouldn't this be
              >
              > if ($pos%$cols === 0 && is_array($picrow)) {
              > ...

              Yes, it should.

              > But then there's another problem. I don't think the array check is going
              > to work. $picrow will always be an array while inside the loop, so there
              > will be an entirely empty row inserted at the end of the table if the
              > number of images is a multiple of $col.

              I suspect that you're right. The version that I use in "PHP Solutions"
              uses a do... while loop, which doesn't have that problem. Instead of
              rewriting the OP's entire script, I just did a quick adaptation.
              However, I think that this would probably fix the problem:

              echo "<table cellpadding='10'><tr>";
              // initialize cell counter
              $pos = 0;
              // specify number of pictures per row
              $cols = 2;

              do {
              $picrow = mysql_fetch_assoc($pictureres);
              // increment the counter
              $pos++;

              $picture .= "<td><img src='pics/" . $picrow['name'] . "' width='250'
              height='175'><br />" . $picrow['description'] . "</td>";

              // if at end of row and records remain, insert row tags
              if ($pos/$cols === 0 && is_array($picrow)) {
              $picture .= '</tr><tr>';
              }
              } while ($picrow);

              // fill in any empty cells in final row
              while ($pos%$cols) {
              $picture .= '<td> </td>';
              $pos++;
              }

              echo $picture;
              echo "</tr></table>";

              --
              David Powers, Adobe Community Expert
              Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
              Author, "PHP Solutions" (friends of ED)
              http://foundationphp.com/
              • 4. Re: PHP photos display
                Level 7
                David Powers wrote:
                > if ($pos/$cols === 0 && is_array($picrow)) {

                I forgot to change that line, which should be:

                if ($pos%$cols === 0 && is_array($picrow)) {

                --
                David Powers, Adobe Community Expert
                Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
                Author, "PHP Solutions" (friends of ED)
                http://foundationphp.com/
                • 5. Re: PHP photos display
                  Level 7
                  .oO(David Powers)

                  >Michael Fesser wrote:
                  >
                  >> But then there's another problem. I don't think the array check is going
                  >> to work. $picrow will always be an array while inside the loop, so there
                  >> will be an entirely empty row inserted at the end of the table if the
                  >> number of images is a multiple of $col.
                  >
                  >I suspect that you're right. The version that I use in "PHP Solutions"
                  >uses a do... while loop, which doesn't have that problem.

                  Ah, OK. Makes more sense.

                  I was sure that you would have stumbled upon that problem when writing
                  your book, so I was a bit confused.

                  Micha
                  • 6. Re: PHP photos display
                    MikeL7 Level 1
                    Thanks alot for the help the line with the if ($pos%$cols === 0 && is_array($picrow)) made it work perfect, and i made the $cols variable editable from a select box so the user can choose how many pics in a row, with three the default.