SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2000
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi again,
    If you read my post about why my keyword viewing script wasnt't working, through the great help of sitepoint members, I have now fixed it, and I am looking to advance further with this script.

    What I would like to do is, if the script displays more than a certain number of keywords, it should show them on different pages (e.g. just like search engines do).
    I know that this use I MySQL query using the function 'LIMIT', but I am not sure how to implement it in my script, being a beginner.
    I'd be very grateful if someone could help me out.

    The code to the script in its present state is:

    Code:
    <table cellspacing=1 cellpadding=1 bgcolor="ffffff" width="420">
    <tr bgcolor="ffffff" width=580>
    <td width=300 bgcolor=000099>
    <a href="<?php echo($PHP_SELF); ?>?sort=keywords"><font class="tenptwhite">
    Keywords</a></font></td>
    <td width=120 bgcolor=000099>
    <a href="<?php echo($PHP_SELF); ?>?sort=popularity"><font class="tenptwhite">Popularity</font></A>
    </td>
    <td width=100></td>
    </tr>
    
    <?php
    mysql_connect("localhost", "root", "");
    if (!dbcnx) {
    echo ("<br>Unable to connect to the database at this time.<br>Sorry AJ!" );
    exit(); 
    }
    mysql_select_db("eliter");
    if ($sort == "popularity") {
    $result = mysql_query("select * from keywords order by popularity desc" );
    while ( $details = mysql_fetch_array($result) ) {
    $keyword = $details["keywords"];
    $popularity = $details["popularity"];
    echo ("
    <tr bgcolor=ffffff width=400>
    <td width=300>
    <a href='http://212.1.157.4/search?query=$keyword&where=web'>$keyword</a><br>
    </td>
    <td width=120 align=center>
    <font class=tenpt>$popularity</font><br>
    </td>
    <td width=100 align=middle>
    <a href='editkeyword.php?keyword=$keyword' class='nineptblue'>
    <img src='../edit.gif' border='0'> edit</a></td>
    ");
    }
    } elseif  ($sort == "keywords") {
    $result = mysql_query(" select * from keywords order by keywords asc" );
    while ( $details = mysql_fetch_array($result) ) {
    $keyword = $details["keywords"];
    $popularity = $details["popularity"];
    echo ("
    <tr bgcolor=ffffff width=400>
    <td width=300>
    <a href='http://212.1.157.4/search?query=$keyword&where=web'>$keyword</a><br>
    </td>
    <td width=120 align=center>
    <font class=tenpt>$popularity</font><br>
    </td>
    <td width=100 align=middle>
    <a href='editkeyword.php?keyword=$keyword' class='nineptblue'>
    <img src='../edit.gif' border='0'> edit</a></td>
     "); 
    } 
    } else {
    $result = mysql_query("select keywords, popularity from keywords");
    while ( $details = mysql_fetch_array($result) ) {
    $keyword = $details["keywords"];
    $popularity = $details["popularity"];
    echo ("
    <tr bgcolor=ffffff width=400>
    <td width=300>
    <a href='http://212.1.157.4/search?query=$keyword&where=web'>$keyword</a><br>
    </td>
    <td width=120 align=center>
    <font class=tenpt>$popularity</font><br>
    </td>
    <td width=100 align=middle>
    <a href='editkeyword.php?keyword=$keyword' class='nineptblue'>
    <img src='../edit.gif' border='0'> edit</a></td>
    "); 
    } }
    ?>
    (I HOPE THIS IS EASIER)

    Can someone please help?!!!
    -AJ

    [Edited by AJ on 10-30-2000 at 03:39 PM]

  2. #2
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AJ...would you mind editing your post and putting some of your code on two lines? It makes reading it very difficult...
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2000
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have made it a little easier I think.
    Sorry for the inconvenience, but someone?!!!
    Cheers
    -AJ

  4. #4
    psycho
    Join Date
    May 2000
    Location
    London
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well.

    i should think you can do this :

    a select query, then mysql_num_rows (look in the mysql manual).

    then, add how many results there are with PHP, and limit it to the first 10. then do an if query

    if ($numrows== < 10 ) {

    echo("<a href='$PHP_SELF?page=2'>page 2</a>");

    or something along those lines.

    sorry i cant be of more help....heh

    j

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually what you need to do is take the total number of rows returned from a count query, then divide it by the number of records you want to show per page to get the number of pages you will have. Then you will need to create the page links and attach the original variables plus the new offset to the ned, then you run your normal query but have LIMIT $offset, $limit at the end. Here is a working example.
    //Set this to be the number of records per page
    $limit = 15;

    //get the count of records
    $numresults = mysql_query("SELECT count(ID) as count from listings where (City = '$city') AND (Type = '$type') AND (status = '1')",$db);
    $numrows = mysql_result($numresults, 0, count);



    // next determine if offset has been passed to script, if not use 0
    if (!isset($offset)) $offset=0;

    //Run the actual query
    $result = mysql_query("SELECT * from listings where (City = '$city') AND (Type = '$type') AND (status = '1') order by Name LIMIT $offset, $limit",$db);


    if ($numrows > $limit) {
    // calculate number of pages needing links
    $pages=intval($numrows/$limit);
    // $pages now contains int of pages needed unless there is a remainder from division
    if ($numrows%$limit) {
    // has remainder so add one page
    $pages++;
    }
    print '<table align="center" cellspacing="0" cellpadding="4" border="0"><tr><td><span class="basic">Page </span></td>';

    //Create the links
    for ($i=1; $i <= $pages; $i++) { // loop thru
    $newoffset=$limit*($i-1);
    if ($newoffset != $offset) {
    printf('<td><a href="%s?offset=%s&show_listings=1&city=%s&type=%s">%s</a></td>%s', $PHP_SELF, $newoffset, $city, $type, $i, "\n");
    }
    else {
    print "<td><span class=offset>". $i ."</span></td>";
    }
    }
    print '</tr></table>';
    }
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2000
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    FreddydoesPHP,
    Thanks very much for that.
    I've got it working sweet as a nut now.
    Cheers!
    -AJ


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
  •