Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Apr 2005
    Location
    Lahore, Pakistan
    Posts
    150
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Little problem in PHP

    Hello,

    I am facing a little problem in PHP.

    I have a table which has three fields

    Table Name : images
    field1 Name:img_id
    field2 Name :image_path
    field3 Name:image_text

    The requirement is to show the Image and its respective text in a Table. And show only One image and its text at a time.

    Below Table there are two button links, "NEXT" and "PREVIOUS". When user press the "NEXT" button the page
    refreshes and it will show the NEXT IMAGE and its respective text. And when user press the PRevious button
    it goes onestep back.

    Kindly help that how can i do this.

    I spend many hours on this, but i am unable to write its algo and sequenced code. I am unable to write the sequence of code.

    Kindly help


    Thanks

  • #2
    Regular Coder
    Join Date
    Oct 2006
    Location
    Bristol
    Posts
    128
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think what you're talking about is 'pagination'. If you are then my solution is below:
    PHP Code:
    <?php
    //Connect to your DB



    $imagespp  1//Number of images per page


    if(!isset($_GET['page'])){
        
    $page 1;
    }else{
        if(
    is_numeric($_GET['page'])){
            
    $page $_GET['page'];
        }
    }

    //==============================================
    //Current Page

    $startfrom = (($page $imagespp) - $imagespp);

    $cursql "SELECT * FROM images LIMIT $startfrom, $imagespp";
    $curresult mysql_query($cursql);

        if(
    $curresult){
            while(
    $row mysql_fetch_array($curresult)){
                echo 
    $row['img_id'];
                echo 
    '<br/>';
                echo 
    $row['img_path'];
                echo 
    '<br/>';
                echo 
    $row['img_text'];
                echo 
    '<br/>';
            }
        }else{
            echo 
    'Current Page Query Failed';
        }

    //===============================================
    //Links to other Images

    $sql_total mysql_query("SELECT * FROM images");

    $totalimages mysql_num_rows($sql_total);

    $totalpages = ($totalimages $imagespp);


    //PREV LINK
    if($page 1){
        
    $prev = ($page 1);
        echo 
    '<a href="'.$_SERVER['PHP_SELF'].'?page='.$prev.'">PREV</a>';
    }

    //NEXT LINK
    if($page $totalpages) {
        
    $next = ($page 1);
        echo 
    '<a href="'.$_SERVER['PHP_SELF'].'?page='.$next.'">NEXT</a>';
    }
    ?>
    Alex

  • #3
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    I assume the problem you are having is how to make the Next and Previous work, not displaying the image.

    The way I'd structure it is have the links have an ID in them. Then make the next link as such:

    Code:
    <a href="image.php?action=next&id=123">Next</a>
    At the top of your PHP (image.php) file.

    PHP Code:
    if (isset($_REQUEST['next'])) {
        
    $sql "SELECT * FROM images WHERE id >= " $_REQUEST['id'] . " ORDER BY img_id ASC LIMIT 1, 1"// this gets the row that follows the row with ID specified in the URL
        // ... display image etc. ...

    You'd do the reverse for the Previous link (WHERE img_id <= id ORDER BY img_id desc LIMIT 1, 1)

    To know whether there is a previous or next image, just change the LIMIT to 1, 2. That way if you don't get more than 1 result, there is no next or previous image.


  •  

    Posting Permissions

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