Results 1 to 5 of 5

Thread: PHP image gallery problems - please help

  1. #1
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question PHP image gallery problems - please help

    Hi everyone I have been working on a php script for populating an image gallery. I have been having strange problems as have not been able to figure out what I am doing wrong. Any help would be great.

    This is what I get from running the script.
    query($query); $num_results = $result->num_rows; if ($num_results < 8) { $grid_fill = $num_results; } else { $grid_fill = 8; } $num_pages = $num_results / 8; echo '
    '; for ($counts = 0; $counts < $grid_fill; ++$counts) { $row = $result->fetch_assoc(); $restitle = stripslashes($row['imagenm']); $resdsc = stripslashes($row['imagedsc']); $resindex = ; echo '
    $restitle $resdsc

    '; } echo '
    '; ?>
    While here is the code that I wrote for the script... passwords and such removed
    PHP Code:
    <?php 
        $pagenum 
    $_GET['page'];
        
    $category $_GET['category'];
        
        if (
    $pagenum == NULL) {
            
    $pagenum 1;
        }
        
        
        
        @ 
    $db = new mysqli('server''user''password''database'); 
        if (
    mrsqli_connect_errno()) { 
       echo 
    'Error: Could not connect to database. Please try again later.';
       die(
    'Could not connect: ' mysql_error());
        } 
        echo 
    'Connected successfully';
        
        
    $query 'select imageid, imagenm, imagedsc from portfolio';
        if (
    $category != NULL ) {
            
    $query $query 'where imagetp = ' $category;
        }
        
    $query $query ' order by imagedtad';
        
    $result $db->query($query);
        
        
    $num_results $result->num_rows;
        
        if (
    $num_results 8) {
            
    $grid_fill $num_results;
            }
        else {
            
    $grid_fill 8;
            }
        
        
    $num_pages $num_results 8;
        
        echo 
    '<div class=\"content\">
        <ul class=\"grid group\">
            '
    ;
        
        for (
    $counts 0$counts $grid_fill; ++$counts) {
            
    $row $result->fetch_assoc();
            
    $restitle stripslashes($row['imagenm']);
            
    $resdsc stripslashes($row['imagedsc']);
            
    $resindex = ;
            echo 
    '<li>
                <div class=\"infooverlay\">
                    <div class=\"valign\">
                        <div class=\"info\">
                            <span class=\"title\">$restitle</span>
                            <span class=\"dsc\">$resdsc</span>
                        </div>
                    </div>
                </div>
                <a href=\"$resindex\"  >
                    <img alt=\"$restitle\" src=\"portfolio/thumbnails/$resindex.png\">
                </a>
            </li>'
    ;
            }
        
        echo 
    '
        </ul>
    </div>'
    ;
        
    ?>

  2. #2
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    The main things I see that could be causing the query to fail are on these lines
    PHP Code:
    $query 'select imageid, imagenm, imagedsc from portfolio';
        if (
    $category != NULL ) {
            
    $query $query 'where imagetp = ' $category;
        }
        
    $query $query ' order by imagedtad'
    You should be using capitals for the query SELECT WHERE, FROM and ORDER BY. Also it is best to put `backticks` around the table names and table columns (not single quotes, backticks are above the tab key). Then if $category is not an integer you need to put single quotes around it.
    On this line
    PHP Code:
     $query $query 'where imagetp = ' $category
    you need to add a space between the ' and where, like this
    PHP Code:
     $query $query ' where imagetp = ' $category
    Otherwise when it is joined to the first part of the query it is reading it like this - from portfoliowhere imagetp = because you don't have the backticks.
    I did notice that you misspelled the mysqli on this line, although that is not your real issue.
    PHP Code:
    if (mrsqli_connect_errno()) 
    Then after this line
    PHP Code:
    $result $db->query($query); 
    Put this
    PHP Code:
    echo mysqli_error(); 
    I have never used mysqli so I don't know for sure that what I just said is 100% correct or it might be mysql_error().
    Putting that line in will tell you if the query fails and why, then once the full code is perfected you can remove it for the live site.

  3. The Following User Says Thank You to fastsol1 For This Useful Post:

    zchrykng (06-28-2011)

  4. #3
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the quick response. I tried your suggestions, and could not get it to work. So I have decided to do what I should have done from the beginning, build the script piece by piece testing after each addition. Rather than writing the entire thing in one shot. If I can get it working, or need more help I will post here again.

    Thanks,
    Zach

  5. #4
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Only escape double quotes if you are using double quotes to encapsulate the string. In double quotes variables will be parsed however in single quotes they will not.

    so this

    PHP Code:
     echo '<li>
                <div class=\"infooverlay\">
                    <div class=\"valign\">
                        <div class=\"info\">
                            <span class=\"title\">$restitle</span>
                            <span class=\"dsc\">$resdsc</span>
                        </div>
                    </div>
                </div>
                <a href=\"$resindex\"  >
                    <img alt=\"$restitle\" src=\"portfolio/thumbnails/$resindex.png\">
                </a>
            </li>'

    should be

    PHP Code:
    echo "<li>
                <div class=\"infooverlay\">
                    <div class=\"valign\">
                        <div class=\"info\">
                            <span class=\"title\">
    $restitle</span>
                            <span class=\"dsc\">
    $resdsc</span>
                        </div>
                    </div>
                </div>
                <a href=\"
    $resindex\"  >
                    <img alt=\"
    $restitle\" src=\"portfolio/thumbnails/$resindex.png\">
                </a>
            </li>"

    or

    PHP Code:
    echo '<li>
                <div class="infooverlay">
                    <div class="valign">
                        <div class="info">
                            <span class="title">' 
    $restitle '</span>
                            <span class="dsc">' 
    $resdsc .'</span>
                        </div>
                    </div>
                </div>
                <a href="' 
    $resindex '">
                    <img alt="' 
    $restitle '" src="portfolio/thumbnails/$resindex.png">
                </a>
            </li>'

    Corrections to my coding/thoughts welcome.

  6. The Following User Says Thank You to bluewalrus For This Useful Post:

    zchrykng (06-28-2011)

  7. #5
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    Oh yeah, I saw that before but forgot to mention it in my post, oh well.

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
  •