SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile One more Kevin Yank's book question. Chapter 7, split text into pages.

    Hi

    As you may have noticed, I am reading Kevin Yank's great book published at SitePoint. Chapter 7, Kevin explains how to split a text into an array of pages. I understood the concept, I understood the code. Fine isn't it? Not that much actually. Here is where I am stuck (I don't know if I have the right to post the book's code so I will try to explain it):

    Let's asy that instead of getting one joke, I want to diplsay a list of all the jokes titles stored in the db. I would use a while loop to display them:
    PHP Code:
    $sql mysql_query("SELECT titles FROM Jokes");
     while ( 
    $row mysql_fetch_array($sql) ) {

    //display the titles 
    Now what if there are say... 300 thousands titles available? Heh... very annoying to scroll down the page. So let's say that I would like to split this result into an array of pages, how could I do it? The RegExp showed in Kevin's book wouldn't be very useful since it is meant to split a text, not a number of db rows.

    Am I clear? Would anyone know how to do it?

    Thanks in advance for your time and your help

  2. #2
    SitePoint Evangelist
    Join Date
    Oct 2001
    Location
    Texas
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2001
    Location
    Texas
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by duuudie
    Hi

    As you may have noticed, I am reading Kevin Yank's great book published at SitePoint.
    Nope no one noticed

  4. #4
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Wbmstr84
    Nope no one noticed
    lol
    I wrote that because I have another thread about a similar question. This one is the second thread I initiate about Kevin's book.

    Thanks for the link, I will check it right now.

  5. #5
    SitePoint Evangelist
    Join Date
    Oct 2001
    Location
    Texas
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by duuudie
    I wrote that because I have another thread about a similar question. This one is the second thread I initiate about Kevin's book.
    Yep I did see the other one(s)!!

  6. #6
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep I did see the other one(s)!!


    I am trying to 'configure' this script that I found on hotscripts:
    PHP Code:
    // Set the variables for the database

    $Host      "localhost";
    $User      "";
    $Password  "";
    $DBName    ""// Database name
    $TableName ""// Name of table to display

    // Do not edit below this line

    $Link mysql_pconnect ($Host$User$Password);

    if ( 
    $Pagesize ) {
        if( !
    $start $start 1;
        
    pagination($start,$Link,$DBName,$TableName,$Pagesize);
        
    $PaginationQuery "LIMIT " $start ", " $Pagesize;
    }

    $Query "SELECT * FROM $TableName " $PaginationQuery;
    $Result mysql_db_query ($DBName$Query$Link);

    // Create a table with headers.
    print ("<TABLE BORDER=1>\n");
    print (
    "<TR>\n");
    for (
    $i 0$i mysql_num_fields($Result); $i++) {
        print 
    "<TD>".mysql_field_name($Result$i)."</TD>\n";
    }
    print (
    "</TR>\n");

    // Fetch the results from the database.
    while ($Row mysql_fetch_array ($Result)) {
         print (
    "<TR>\n");
        for (
    $i 0$i mysql_num_fields($Result); $i++) {
            print 
    "<TD>$Row[$i]</TD>\n";
        }
        print (
    "</TR>\n");
    }
    print (
    "</TABLE>\n");

    mysql_close ($Link);

    function 
    pagination($start,$Link$DBName$TableName$Pagesize) {

        if ( !
    $Pagesize ) return;

        
    $Query "SELECT count(*) as count FROM $TableName";
        
    $Result mysql_query($Query);
        
    $row mysql_fetch_array($Result);
        
    $numrows $row['count'];
        if(
    $start >= $Pagesize) {
           echo 
    "<a href=\"" $PHP_SELF "?Pagesize=$Pagesize&start=" . ($start $Pagesize) .
                
    "\">Previous</a> | \n";
        } else {
                echo 
    "Previous | \n";
        }
        if(
    $numrows > ($start $Pagesize)) {
           echo 
    "<a href=\"" $PHP_SELF "?Pagesize=$Pagesize&start=" . ($start $Pagesize) .
                
    "\">Next</a>\n";
        } else {
                echo 
    "Next | \n";
        }
        print 
    "Page " floor(($start $Pagesize)+1);
        print 
    " of " ceil(($numrows $Pagesize));
        print 
    " | " $numrows " Records";

    }





    ?> 
    that's my db connection script.
    PHP Code:
    <?php
     
    // Connect to the database server 
     
    $dbcnx = @mysql_connect('localhost''root'''); 
     if (!
    $dbcnx) { 
       die( 
    '<p>Unable to connect to the ' 
            
    'database server at this time.</p>' ); 
     } 

     
    // Select the database 
     
    if (! @mysql_select_db('firststep') ) { 
       die( 
    '<p>Unable to locate the firststep ' 
            
    'database at this time.</p>' ); 
     } 
    ?>
    I am having some hard time understanding how to put it all together. I find strange all this:
    PHP Code:
    pagination($start,$Link,$DBName,$TableName,$Pagesize); 
    why is it needed to include all these info? couldn't I just get rid of this? It seems that it would be easier.


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
  •