SitePoint Sponsor

User Tag List

Results 1 to 19 of 19

Thread: PHP Challenge

  1. #1
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK all...I would like some input on a database setup / outputting issue.

    Please visit: (on the development site all images are displayed using a single "template" page)
    http://www.plusoneonline.com/pics/lauren_pics_aol.shtml

    I have been adding quite a few picture galleries for my client lately and would like to start putting them into a DB. Some of the necessary fields will be

    path to large image
    path to thumbnail image
    gallery that image is associated with
    name of the person submitting images
    header (where and when pictures were taken)
    description of individual images (not all galleries have this, but I would like to keep it in there for the future)


    We'll use an example...

    I create a gallery. In that gallery I have 12 images (actually 24 counting the thumbnail) I want to be able to spit out a table 3 cells wide by 4 cells tall. Each cell would contain a thumbnail with a link to the larger image. The layout would be like the link above. The actual code will be different as I already have a basic dynamic (but non-database) setup for each page.

    I could just create each page as static (like I have now), but it would be really cool if I could do it DB as it would save me much time in the future.

    I don't have the slightest idea how to go about it as each table row needs to have 3 images, then spit out the next row of 3 images, then if there is more than 12 thumbs, give links to the next set of images.

    this will be a tough one I think...anyone have thoughts?
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  2. #2
    SitePoint Evangelist mad-onion's Avatar
    Join Date
    Aug 2000
    Location
    Land of the long white cloud
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have two methods for doing this, both work, although neither is particuarly good. Freddy or someone will probably have a better method..

    Code:
    echo '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
    while($r=mysql_fetch_array($result)){
      if($col==1){
    echo ' <tr>
     <td>'.$r[image];
    $col=2;
    }elseif($col==2){
    echo '</td>
        <td>'.$r[image];
    $col=3;
    }elseif($col==3){
    echo '</td>
        <td>'.$r[image].'</td>
      </tr>';
    $col=1;
    }
    
    }
    echo '</table>';
    That is pretty poor quality code and is probably wridden with bugs but you might get the idea...i am just waiting for freedy to come and enlighten us with his genius method for doing this!..

    Cheers
    SiteOptions >> Services :: Products :: Contact
    Developers of PHP, C++, Visual Basic, MySQL, and more!

  3. #3
    SitePoint Member
    Join Date
    Aug 2000
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Given a page number $page (starts counting from 0), you must display images $page * 12 to ($page * 12) + 11, thus displaying 12 images.

    Before we display a row, we must check if there's a image in the row. We can build a for loop that checks for that condition, and when true, displays the row. You'd have some code like this:

    Code:
    (some code here)
    
    $imagesquery = mysql_query("SELECT id " .
                          "FROM images " .
                          "WHERE field1 = $var1 " .
                          "ORDER BY field2 DESC " .
                          # grab only images that'll appear in the page
                          "LIMIT ($page * 12), ($page * 12 + 11)");
    
    (process results and store them in array $images)
    
    echo '<table>';
    
    // loop over rows, and check if there's at least one image in the row
    
    for($row = 1; $images[$row]; $row += 3) {
        echo '<tr>';
    
        // now loop over the images in the row
        for($imageid = $row; $imageid <= $row + 2; $imageid++) {
            echo '<td>';
            echo '<img src="showThumb.php?id=' . $images[$imageid]['id'] . '">';
            echo '</td>';
        }
    
        echo '</tr>';
    }
    echo '</table>';
    Of course, the code will need some tweaking to fit your site, but that's the general idea.

    Cheers,

    Fabio Dias

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright so mad-onion your solution wasn't all that bad. However I woudl take this approach

    //Set the number or records per page
    $limit = 12;

    //Check to see if $offset has value if not set it to 0
    if(!isset($offset)) $offset = 0;

    //Get a total number of records or images in this case.
    $countq = mysql_query("SELECT count(*) as totalcount from tablename");

    //Assign the totalcount to var $totalcount
    $totalcount = mysql_reqult($countq,0);

    //Run the real query
    $result = mysql_query("SELECT * from tablename LIMIT $offset, $limit");

    //Start the table
    print '<table width="600"><tr>';

    //Set a counter to 1
    $count = 1;
    //Set $cols to be the number of records to show in each row
    $cols = 3;

    while($row = mysql_fetch_array($result)) {
    if (is_int($count / $cols)) {
    printf('<td>%s</td></tr><tr>%s', $row["thumbnail"], "\n");
    }
    else {
    printf('<td>%s</td>%s', $row["thumbnail"], "\n");
    }
    $count++;
    }

    //Close the table
    print '</tr></table>';

    //Make the navigation for extra pages
    print get_nav($offset, $limit, $totalcount);



    ########Navigation function#########
    function get_nav($offset, $limit, $totalnum) {
    global $PHP_SELF;
    if ($totalnum > $limit) {
    // calculate number of pages needing links
    $pages = intval($totalnum/$limit);

    // $pages now contains int of pages needed unless there is a remainder from division
    if ($totalnum%$limit) $pages++;

    $return .= sprintf('<table><tr><td>Page </td>');
    for ($i=1; $i <= $pages; $i++) { // loop thru
    $newoffset=$limit*($i-1);
    if ($newoffset != $offset) {
    $return .= sprintf('<td><a href="%s?offset=%s">%s</a></td>%s', $PHP_SELF, $newoffset, $i, "\n");
    }
    else {
    $return .= sprintf('<td>%s</td>%s', $i, "\n");
    }
    }
    $return .= sprintf('</tr></table>');
    }

    else {
    $return = "";
    }
    return $return;
    }




    <Edited by freddydoesphp on 01-20-2001 at 02:31 PM>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sheesh...i definately need to learn PHP better. I can follow you guys on the coding, but I don't think I could come up with any of that.

    One quesion on your code Freddy...if there are say 7 images on a gallery (not divisible by 3), would that chunk of code print out the remaining table cells for that row? If it didn't then there would be a problem with display...
    Code:
    image     image     image
    
    image     image     image
    
    image     cell      cell
    OK...given the fields I listed above, what would be a good table setup?

    I am thinking one table for the list of galleries:

    po_image_galleries (which would contain)
    id
    title
    header (where and when pictures were taken)
    name of person submitting images

    and another table for the images

    po_image_list (which would contain)
    gid (to match up with id in gallery listing)
    id
    path to thumbnail
    path to large image
    description of image
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Database looks good here is small fix


    //Start the table
    print '<table width="600"><tr>';

    //Set a counter to 1
    $count = 1;
    //Set $cols to be the number of records to show in each row
    $cols = 3;

    //Check for remainder
    if ($totalcount%$cols != 0) $remainder = $cols - ($totalcount%$cols);

    while($row = mysql_fetch_array($result)) {
    if (is_int($count / $cols)) {
    printf('<td>%s</td></tr><tr>%s', $row["thumbnail"], "\n");
    }
    else {
    printf('<td>%s</td>%s', $row["thumbnail"], "\n");
    }
    $count++;
    }

    //If there is a remainder make another cell with the correct colspan
    if ($remainder) {
    printf('<td colspan=%s>&nbsp;</td>', $remainder);
    }


    //Close the table
    print '</tr></table>';

    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    SitePoint Evangelist mad-onion's Avatar
    Join Date
    Aug 2000
    Location
    Land of the long white cloud
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another work of genius from fedddy...aka phpgod
    SiteOptions >> Services :: Products :: Contact
    Developers of PHP, C++, Visual Basic, MySQL, and more!

  8. #8
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have discovered a little problem (probably an easy fix)

    When you use printf how can you use percentages in your TD tag? When I use this code it gives me an error:

    printf ('<td width="33%"><img src="../images/%s" width="120" border="0"></td></tr>%s', $row["thumb_image"], "\n");

    When I take out the width="33%" it works. So I would assume it is the % symbol that is fouling things up...how to get around that and still use printf?
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  9. #9
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To use % in your printf statement just use fore example 33%%

    Just add an extra % sign.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  10. #10
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Freddy...the display images script is working well. I just have a few bugs to work out. Some of them I have already fixed, but some of them I can't figure out.

    1) The page is displaying 12 images (which is what I want) even when there are less images in the gallery. I started with a test gallery of 4 images and it displayed each image 3 times. The links work, the display page works. I think it might have something to do with $countq. When I insert a pritn statement to test the value of $countq, I get "Resource ID #3" on the page. Yet when I test $totalcount (which is based on $countq) I get the correct count for that gallery.

    2) I can't get the value of $gid to follow me into the get_nav function. I print out the value just above the function and just below and get the corect value, but inside it is no dice. Is it because you have it set to global?

    ok...those are my questions. I am pretty excited about this because this section is the last one that I am working on for this site. Even with these little bugs, I am almost done with it. Thanks to you all.

    Below is the complete code for your perusal (sorry about the length)

    <?php

    //Set the number of records per page
    $limit = 12;

    //Set $cols to be the number of records to show in each row
    $cols = 3;

    //Set a counter to 1
    $count = 1;

    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    if (!$conn) {
    echo "<P>Can't connect to database.</P>";
    exit();
    }

    if (! @mysql_select_db($dbname) ) {
    echo( "<P>Unable to locate the database at this time.</P>" );
    exit();
    }

    //Check to see if $offset has value if not set it to 0
    if(!isset($offset)) $offset = 0;

    //Get a total number of records or images in this case.
    $countq = mysql_query("SELECT count(*) as totalcount from po_gallery_images WHERE gid=$gid");

    //Assign the totalcount to var $totalcount
    $totalcount = mysql_result($countq,0);

    //Run the real query
    $result = mysql_query("SELECT po_gallery_list.header, thumb_image, po_gallery_images.id, text from po_gallery_images, po_gallery_list WHERE gid=$gid LIMIT $offset, $limit");

    //Start the table
    echo "<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" border=\"0\"><tr align=\"center\" valign=\"top\">";

    //Check for remainder if ($totalcount%$cols != 0) $remainder = $cols - ($totalcount%$cols);
    if ($totalcount%$cols != 0) $remainder = $cols - ($totalcount%$cols);

    while($row = mysql_fetch_array($result)) {
    if (is_int($count / $cols)) {
    printf ('<td width="33%%"><a href="display.php3?gid=%s&id=%s"><img src="../images/%s" width="120" border="0"></a>', $gid, $row["id"], $row["thumb_image"]);
    if ($row["text"] == "0") {
    printf ('</td></tr>%s', "\n");
    } else {
    printf ('<br><br>%s</td></tr>%s', $row["text"], "\n");
    }
    print '<tr align="center"><td colspan="3"><hr width="95%"></td></tr><tr align="center">';
    } else {
    printf('<td width="33%%"><a href="display.php3?gid=%s&id=%s"><img src="../images/%s" width="120" border="0"></a>', $gid, $row["id"], $row["thumb_image"]);
    if ($row["text"] == "0") {
    printf ('</td>%s', "\n");
    } else {
    printf ('<br><br>%s</td>%s', $row["text"], "\n");
    }
    }
    $count++;
    }

    //If there is a remainder make another cell with the correct colspan
    if ($remainder) {
    printf('<td colspan=%s>&nbsp;</td>', $remainder);
    }

    //Close the table
    print '</tr></table>';

    //Make the navigation for extra pages
    print h($offset, $limit, $totalcount);

    ########Navigation function#########
    function get_nav($offset, $limit, $totalnum) {
    global $PHP_SELF;
    if ($totalnum > $limit) {
    // calculate number of pages needing links
    $pages = intval($totalnum/$limit);

    // $pages now contains int of pages needed unless there is a remainder from division
    if ($totalnum%$limit) $pages++;

    $return .= sprintf('<table><tr><td>Page </td>');
    for ($i=1; $i <= $pages; $i++) { // loop thru
    $newoffset=$limit*($i-1);
    if ($newoffset != $offset) {
    $return .= sprintf('<td><a href="display_thumbs?offset=%s&gid=%s" class="mainpage" style="font-size: 12pt;">%s</a></td>%s', $newoffset, $gid, $i, "\n");
    }
    else {
    $return .= sprintf('<td>%s</td>%s', $i, "\n");
    }
    }
    $return .= sprintf('</tr></table>');
    }

    else {
    $return = "";
    }
    return $return;
    }

    print '<br><br><a href="picsmain.php3" class="mainpage" style="font-size: 12pt;">Back to Gallery Listing</a>';

    ?>
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  11. #11
    SitePoint Evangelist AlexC's Avatar
    Join Date
    Oct 2000
    Location
    Sheffield, UK
    Posts
    437
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by mad-onion
    Another work of genius from fedddy...aka phpgod
    Sure you dont mean freddy?

    <grin>

    when freddy [who does php] dies, can i have its brain? - maybe i should have that as my sig <evil grin>
    Nearly 7 years old!

  12. #12
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Andy, the reason you get the resource id is because that is all that $countq holds you vae to fetch the result to get the vaule, that is why we use the mysql_result() line. I think the reason you are getting thrown off is your count query should have the exact same number returned as the real query what you get when you print $totalcount should be the same number as when you print mysql_num_rows($result)

    As for the $gid and the function you must either pass a var as an arg to the function or you must use global $var inside the function.


    I think your query is what is causing the problem, I think you should try running the query from the command line or PHPMyAdmin whatever you use, and substitute a number in for $gid, if you are receiving multiple rwos for each record you have a problem in your query.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  13. #13
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK Freddy...I have the pages working right now. I didn't want to fool with it anymore so I will just do two queries. One to get the header text for the gallery and the other to get all of the pictures, text and links for each image.

    I still need to figure out how to get the value for GID into the get_nav function. I don't know how to do that.

    What I mean is this: I already have the value of $gid from the query string of the previous page. I want to use $gid inside the function. How do I do that?
    <Edited by creole on 01-23-2001 at 05:03 PM>
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  14. #14
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There two ways to get in to the function I will put both in here and bold them, but take one out and only use one of the two methods. Notcie I put $gid in the function call and in the function args or the parantheses part of the function. Or you can declare it global in the function, either will work.

    print get_nav($offset, $limit, $totalcount, $gid);


    function get_nav($offset, $limit, $totalnum, $gid) {
    global $PHP_SELF, $gid;
    if ($totalnum > $limit) {
    // calculate number of pages needing links
    $pages = intval($totalnum/$limit);

    // $pages now contains int of pages needed unless there is a remainder from division
    if ($totalnum%$limit) $pages++;

    $return .= sprintf('<table><tr><td>Page </td>');
    for ($i=1; $i <= $pages; $i++) { // loop thru
    $newoffset=$limit*($i-1);
    if ($newoffset != $offset) {
    $return .= sprintf('<td><a href="display_thumbs?offset=%s&gid=%s" class="mainpage" style="font-size: 12pt;">%s</a></td>%s', $newoffset, $gid, $i, "\n");
    }
    else {
    $return .= sprintf('<td>%s</td>%s', $i, "\n");
    }
    }
    $return .= sprintf('</tr></table>');
    }

    else {
    $return = "";
    }
    return $return;
    }

    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  15. #15
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's it? I was expecting some big long essay on global or local variables...

    thanks again F

    you da man
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  16. #16
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    darnit...now I get a

    Fatal error: Unable to open f:\php/pics/display_thumbs in Unknown on line 0

    This is the string that I passed to that page..

    php/pics/display_thumbs?offset=12&gid=004

    What's weird is when I type in the query string manually, It works just fine. I think it might have something to do with the function. This is what I have for the function now.

    print get_nav($offset, $limit, $totalcount, $gid);

    <EDIT>
    Never mind...I was missing the page extension...i had typed in display_thumbs instead of display_thumbs.php3. It works perfectly now. WOOHOO

    Thanks again all...especially Freddy.
    </EDIT>

    <Edited by creole on 01-23-2001 at 08:42 PM>
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  17. #17
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Shouldn't it be

    $return .= sprintf('<td><a href="display_thumbs.php?offset=%s&gid=%s" class="mainpage" style="font-size: 12pt;">


    notice the .php after display_thumbs

    Or you could and should just do it this way

    NOTE the $PHP_SELF being stuck in instead of the filename

    $return .= sprintf('<td><a href="%s?offset=%s&gid=%s" class="mainpage" style="font-size: 12pt;">%s</a></td>%s', $PHP_SELF, $newoffset, $gid, $i, "\n");
    <Edited by freddydoesphp on 01-23-2001 at 08:44 PM>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  18. #18
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah...I tried the PHP self and it kept throwing php.exe into the URL string...
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  19. #19
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is because you are working CGI mode, I assume on your win98 machine. A fix for this to either put $PHP_SELF = $PATH_INFO;

    at the top of your pages or to create a file called let's say prepend.php3, you can really name it anything you want put
    <?
    $PHP_SELF = $PATH_INFO;
    ?>

    in it then make a .htaccess file in the root folder you are working in and put the following line it

    php_value auto_prepend_file "c:\whateverthepatheistoprepend.php3"
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


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
  •