Results 1 to 3 of 3

Thread: paging an array of records

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    447

    paging an array of records

    The original script is found on:
    http://www.sitepoint.com/article/perfect-php-pagination

    PHP Code:
    <?php
    require_once "Paginated.php";
    require_once 
    "DoubleBarLayout.php";
    ?>
    <html>
    <head>
    <title>Pagination</title>

    <!-- Just a little style formatting. Has no bearing on example -->
    <style type="text/css">
        body {
            font-family: Verdana;
            font-size: 13px;
        }
        
        a {
            text-decoration: none;
        }
        
        a:hover {
            text-decoration: underline;
        }
    </style>
    <!-- End style formatting -->
    </head>

    <body>

        <?php
        
    //create an array of names in alphabetic order. A database call could have retrieved these items
        
    $names = array("Andrew""Bernard""Castello""Dennis""Ernie""Frank""Greg""Henry""Isac""Jax""Kester""Leonard""Matthew""Nigel""Oscar");
        
        
    $page $_GET['page'];
        
        
    //constructor takes three parameters
        //1. array to be paged
        //2. number of results per page (optional parameter. Default is 10)
        //3. the current page (optional parameter. Default  is 1)
        
    $pagedResults = new Paginated($names10$page);
        
        echo 
    "<ul>";

        while(
    $row $pagedResults->fetchPagedRow()) {    //when $row is false loop terminates
            
    echo "<li>{$row}</li>";
        }
        
        echo 
    "</ul>";
        
        
    //important to set the strategy to be used before a call to fetchPagedNavigation
        
    $pagedResults->setLayout(new DoubleBarLayout());
        echo 
    $pagedResults->fetchPagedNavigation();
        
    ?>
    </body>
    </html>
    It consists an array of 15 names.
    WHat I needed to do is have a query and set the result into an array and replace the $names above. I replaced it with a function. And within the function called getQuery($bigWords) I placed both the query and the while loop of $results.
    See next posting.
    Compare bible texts (and other tools):
    TheWheelofGod

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    447

    Re: paging an array of records

    $bigWords is an array of words > 4 letters. The rest of the words have previously sorted out.
    PHP Code:
    function getQuery($bigWords){

            
    $query="SELECT * FROM bible WHERE 1=1 AND";
        
            
    //to sort out all words with length less than 4 like AND, OR, BUT...
            
    for ($i=0$i count($bigWords); $i++){
                if(
    $bigWords[$i]!=""){
                    
    $query.=" CASE WHEN text_data LIKE '%" .$bigWords[$i]. "%' THEN 1 ELSE 0 END\n";
                    if(
    $i!=count($bigWords)-1){
                        
    $query.=" +";
                        }else{
                        
    //removes the OR from the last line and replaces with the following
                        //for results of words > 3
                        
    $query.= " > 3";
                        }
                    }
                }
            
    $query .= " ORDER BY id";
            
    $theresult = array();
            
    //$theresult[] = "";
            //$theresult[] = $query;
            
    $result mysql_query($query);

        while(
    $row mysql_fetch_assoc($result)){
            
    $strText $row['text_data'];
            
    $COLORS = array('red','Teal','blue','Magenta','green','PaleGreen','orange','purple','Pink','YellowGreen','Sienna','aqua','Gray','LightBlue','MediumTurquoise','DarkRed');
            for(
    $m=0$m count($bigWords); $m++){
                
    $strText preg_replace("/(".$bigWords[$m].")/i""<span class=\"\" id=\"\" style=\"color:".$COLORS[$m]."; font-weight:bold;\">$1</span>"$strText);
                }
            
    $theresult[] = "<br />\n<span class=\"goToBookChapter\" style=\"font-weight: bold;\">".$row['book_title']." ".$row['chapter'].":".$row['verse']."</span><br />\n".$strText;
            }
            return 
    $theresult;
            echo 
    "<br /><br />".$query."<br />";

    Compare bible texts (and other tools):
    TheWheelofGod

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    447

    Re: paging an array of records

    I forgot to mention the problem. The pagination of 10 records per page is not happening.
    I've been having problems in this for WEEKS!!! With no one to help me.
    I don't understand how the while loop for paging should be set. This is what I have so far:
    PHP Code:
    while($row $pagedResults->fetchPagedRow()){
            echo 
    "<span style='color: red;'>".$c."</span><br />";
            
    //echo "{$row}";
            
    print(getQuery($bigWords));
            
    //echo getQuery($bigWords);
            //echo "hi";
            
    $c++;
            } 
    I get:
    Code:
    1
    Array2
    Array3
    Array4
    Array5
    Array6
    Array7
    Array8
    Array9
    Array10
    Array
    HEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEELP!!!!!
    Compare bible texts (and other tools):
    TheWheelofGod

Posting Permissions

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



Click Here to Expand Forum to Full Width