1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

DB question

Discussion in 'Databases' started by matik, Feb 27, 2007.

  1. #1
    Hi!

    I have a table.. Structure is:
    -- 
    -- Table structure for table `database`
    -- 
    
    CREATE TABLE `database` (
      `ID` varchar(255) default NULL,
      `Quote` varchar(255) default NULL,
      `Name` varchar(255) default NULL,
      KEY `Name` (`Name`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
    PHP:
    And for example few entries..
    INSERT INTO `database` VALUES ('12714', '"Do what thou wilt" shall be the whole of the law.', 'Aleister Crowley');
    PHP:
    I'm using this code, to call my database values into my site.
    $result = mysql_query("SELECT * FROM database ORDER BY ID") 
    or die(mysql_error());  
    
    echo "<table border='0'>";
    echo "<tr> <th>ID</th> <th>Quote</th> <th>Author</th> </tr>";
    
    while($row = mysql_fetch_array( $result )) {
    
    	echo "<tr><td>"; 
    	echo $row['ID'];
    	echo "</td><td>"; 
    	echo "<cite>".$row['Quote']."</cite>";
        echo "</td><td>"; 
    	echo $row['Name'];
    	echo "</td></tr>"; 
    } 
    PHP:
    It throws me all entries in one page.. Can i have for example 10 entries in one page, and 10 in the other & so on...

    thanks in advance:)
     
    matik, Feb 27, 2007 IP
  2. JKL

    JKL Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Google for "php paging database".
     
    JKL, Feb 27, 2007 IP
  3. Bezzen

    Bezzen Peon

    Messages:
    244
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    SELECT * FROM database ORDER BY ID LIMIT 0 , 10

    would show you the first 10

    SELECT * FROM database ORDER BY ID LIMIT 10 , 10

    would show you the next 10 and so on.
     
    Bezzen, Feb 28, 2007 IP
  4. spachev

    spachev Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The above method is OK for small database. For a larger database, it will have performance problems. I suggest you always save the id first and the last record of each page and then use where id > last_id order by id limit 10 , same idea for paging the other way.
     
    spachev, Mar 8, 2007 IP
  5. Lovers_Rock

    Lovers_Rock Active Member

    Messages:
    121
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #5
    $max=50;
    if (!$page) $page=1;
    $alt=($page - 1) * $max;
    
    $q=mysql_query("Select * from home  where cat ='2' order by hit desc limit $alt,$max");
    $r= mysql_fetch_array($q);
    PHP:
    this like 123456789
         <?
    $ss=$nr2/$max;
    $ss=ceil($ss);
    
    for ($i=1;$i<=$ss;$i++) {
    	if ($page == $i) echo "<FONT class=\"link2 underline ucwords\">$i</font>&nbsp;";
    	else echo "<A href=$site/cat/$id-$i.html>[$i]</A>&nbsp;";
    }
    ?>
    PHP:
    Before I learn I looked for it to much. and I want to share it :)
     
    Lovers_Rock, Mar 8, 2007 IP
    matik likes this.
  6. matik

    matik Peon

    Messages:
    3,471
    Likes Received:
    169
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Lovers_Rock, thanks for thiis... rep added:D
     
    matik, Mar 11, 2007 IP