Results 1 to 7 of 7

Thread: 10 items per page (MySQL & PHP)

  1. #1
    Join Date
    May 2006
    Posts
    266
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 10 items per page (MySQL & PHP)

    Hi i would like to show a max of 10 items per page and i was wondering how is this done.

    If you can help that would be great,
    Code:
    $news = "SELECT * FROM news WHERE `group1` LIKE '%".$_GET['g']."%' ORDER BY RAND()";
    
    $result = mysql_query($news);
    
    while($row = mysql_fetch_array($result))
    {
        echo "<div class='newslist'><a href='http://www.twitoosearch.com/?p=News&g=view&q={$row['NB']}&tags={$row['group1']}&group={$row['group1']}'><img class='podimg' border='0' alt='{$row['newsname']}' src='{$row['image']}' width='100' height='100' /></a></div>";
    }
    Any help welcomed
    The web in one word.

  2. #2
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Hi, you would need to put a limit on your MySQL query

    PHP Code:

    $rowsPerPage 
    10//number of results you want to display

    $num 1//set the offset to start w/the num. of results (good for paging)

    $offset = ($num 1) * $rowsPerPage// to offset the limit count

    $news "SELECT * FROM news WHERE `group1` LIKE '%".$_GET['g']."%' ORDER BY RAND() LIMIT $rowsPerPage$offset"// notice I added the limit

    $result mysql_query($news);

    while(
    $row mysql_fetch_array($result))
    {
        echo 
    "<div class='newslist'><a href='http://www.twitoosearch.com/?p=News&g=view&q={$row['NB']}&tags={$row['group1']}&group={$row['group1']}'><img class='podimg' border='0' alt='{$row['newsname']}' src='{$row['image']}' width='100' height='100' /></a></div>";

    and the limit can be changed to whatever you want

    HTH

    -Josh
    - Josh

  3. #3
    Join Date
    May 2006
    Posts
    266
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thank for the help,
    Just a few questions,
    Will this show a next button if there are news items that they have not seen, and a back button.

    Like i don't want to show a next button if there is nothing to next to.
    The web in one word.

  4. #4
    Join Date
    May 2006
    Posts
    266
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Tryed the code and seem not to of worked very well, as far as it did not show the limit and when i said 12 it showed 10 and when i said 10 it show 3
    The web in one word.

  5. #5
    Join Date
    Aug 2007
    Location
    Ohio
    Posts
    79
    Thanks
    0
    Thanked 15 Times in 15 Posts

    Default

    Quote Originally Posted by queerfm View Post
    Tryed the code and seem not to of worked very well, as far as it did not show the limit and when i said 12 it showed 10 and when i said 10 it show 3
    What your looking for is known around the developing community as pagination. There are countless articles on how to achieve pagination using PHP and MySQL. I'd recommend you check out a couple of these articles:

    PHP Pagination
    Pagination through a database
    Pagination - what it is and how to do it
    Pagination with PHP
    How to add paging (Pagination) with PHP and MySQL
    PHP pagination script
    PHP Pagination Script
    Perfect PHP Pagination

    Surely one of those can help you.

    Note: Is your query fetching random rows? If it is, pagination is impossible. It's just common sense.

  6. #6
    Join Date
    May 2006
    Posts
    266
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    No its not well it should not be.

    I would like it to be by last entry
    The web in one word.

  7. #7
    Join Date
    May 2006
    Posts
    266
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the help, i have it working kinda
    http://www.twitoosearch.com/?p=Podcasts&g=Technology


    however as you can see it seems to be showing the next and back button next to the logos when it should be showing it at the bottom.

    here is some of the code
    Code:
    while($row = mysql_fetch_array($SearchResult))
    {
        echo "<div class='podcastlist'><a href='http://www.twitoosearch.com/?p=Podcasts&g=view&q={$row['1']}&tags={$row['2']}&group={$row['3']}'><img class='podimg' border='0' alt='{$row['4']}' src='{$row['image']}' width='100' height='100' /></a></div>";
    
    }
    
    $Nav="";
    If($page > 1) {
    $Nav .= "<br /><A HREF=\"?p=Podcasts&g=" .urlencode($g) . "&page=" . ($page-1) . "\"><< Prev</A>";
    }
    For($i = 1 ; $i <= $NumberOfPages ; $i++) {
    If($i == $page) {
    $Nav .= "<B>$i</B>";
    }Else{
    $Nav .= "<A HREF=\"?p=Podcasts&g=" .urlencode($g) . "&page=" . $i . "\">$i</A>";
    }
    }
    If($page < $NumberOfPages) {
    $Nav .= "<A HREF=\"/?p=Podcasts&g=" .urlencode($g) . "&page=" . ($page+1) . "\">Next >></A>";
    }
    Echo "<div class='searchr'>" . $Nav;
    Echo "</div>";
    ?>
    Part of the CSS

    Code:
    div.podcastlist{
    	background-image:URL(http://www.twitoosearch.com/images/div.png);
    	width:120px;
    	float:left;
    	position:inherit;
    	height:120px;
    	margin-right:0.5em;
    	margin-bottom:0.5em;
    }
    
    div.podcastlist:hover{
    	background-image:URL(http://www.twitoosearch.com/images/div_hover.png);
    	width:120px;
    	height:120px;
    
    }
    
    div.searchr{
    	width:100%;
    }
    Thx
    Last edited by queerfm; 03-31-2008 at 12:49 PM. Reason: added css code
    The web in one word.

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
  •