SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    paging recordsets

    This formula which I have doesn't work. First is how to set the limit the page with ten records per page: mysql_num_rows seems to show a result of ten. Then how to set in the select/options the pages:
    PHP Code:
    <?php
    //paging
    $page = isset($_GET['page']) ? $_GET['page'] : 1;  //or you can use the ternary operator 

    $perpage 10;// number of records per page
    $end $page*$perpage// present page
    $start = ($page*$perpage)-$perpage
    /******************************************************************************/

    $sql="SELECT * FROM dbname WHERE 1=1 AND";

    $j=0;
    for (
    $i=0$i count($searchTheseArr); $i++){
        if(
    strlen($searchTheseArr[$i]) > 4){
            
    $newSearchTheseArr[$j] = $searchTheseArr[$i];
            
    $j++;
        }
    }
    for (
    $j=0$j count($newSearchTheseArr); $j++){
        
    $sql.=" text_data LIKE '%" .$newSearchTheseArr[$j]. "%'";
        if(
    $j!=count($newSearchTheseArr)-1){
            
    $sql.=" OR";
        }else{
            
    $sql.= " LIMIT " $start ", " $end;
        }
    }    

    $result mysql_query($sql) OR exit( 'Error: ' mysql_error());
    //echo mysql_num_rows($result);
    for ($j=0$j count($newSearchTheseArr); $j++){
        echo 
    $newSearchTheseArr[$j]."<br />";
        }
    echo 
    mysql_num_rows($result)."<br />";?>
    <span style="font-weight: bold;">PAGE <?php echo $page?></span>
    <span style="">Go to: <select id='selectTextPage' name='selectTextPage' size= "1" style="width: 100px;" onChange='changeTextPage(this.value);' >
    <?php
    $myarray 
    explode('?'$_SERVER["REQUEST_URI"]);
    foreach(
    $myarray as $value){
        
    $urlsnip $value;
    }
    foreach(
    $myarray as $value){
        
    $Pageurlsnip $value;
    }
    ?>
    <?php
    $total 
    mysql_num_rows(mysql_query($sql));// or die(mysql_error());
    if($perpage == 0){
        
    $perpage 1;
    }

    $numOfPages $total $perpage;
    //echo "<option selected>Select the Page</option>";
    for ($i=1$i <= $numOfPages$i++) {
        
    $Pageurlexpl explode("?"$Pageurlsnip);

        echo 
    "<option ";?>value="searchLinkFiles/linkTextareaSearchResult.php?<?php
        
    for ($k=0$k<=count($Pageurlexpl)-2$k++){
          echo 
    $Pageurlexpl[$k]."&";
        }    
        echo 
    "page=" $i;
        echo 
    "&searchthese=";
        for (
    $j=0$j count($newSearchTheseArr); $j++){
            echo 
    $newSearchTheseArr[$j]."%20";
        }
    ?>"><?php 
        
    echo "Page " $i "</option><br />\n";
    }
    ?>
        </select></span>
        </span><br /><?php
    while($row mysql_fetch_array($result)){
            echo 
    "<span id='regular[]' style='padding: 5px;'>";
            echo 
    "<span style='font-weight: bold;'>".$row["book_title"]." ".$row["chapter"].":".$row["verse"]." ";
            echo 
    "</span>\n";    
                
    $strText $row['text_data'];
                    
    $strText preg_replace("/(".$newSearchTheseArr[$j].")/i""<span class='' id='' style='color:blue; font-weight:bold;'>$1</span>"$strText);
                    echo 
    $strText."<br />\n";
            echo 
    "<br />\n";
    }

    mysql_close($con);
    ?>
    Compare bible texts (and other tools):
    TheWheelofGod

  2. #2
    Strokin' Morango dele454's Avatar
    Join Date
    Oct 2005
    Location
    Cape town, South Africa
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    have you tried reading this article http://www.sitepoint.com/article/perfect-php-pagination

  3. #3
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dele454 View Post
    I will have to look into this.

    But what I have right now... is there any syntax error?
    Here's a shortened version of the code:
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>

    <body>
    <?php 
    $con 
    mysql_connect("","root","");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    mysql_select_db("kjv"$con);

    $sql "SELECT count(*) AS totalrows FROM bible WHERE 1=1 AND";
    $sql .= " CASE WHEN text_data LIKE '%angel%' THEN 1 ELSE 0 END + CASE WHEN text_data LIKE '%appeared%' THEN 1 ELSE 0 END + CASE WHEN text_data LIKE '%flame%' THEN 1 ELSE 0 END + CASE WHEN text_data LIKE '%midst%' THEN 1 ELSE 0 END + CASE WHEN text_data LIKE '%looked%' THEN 1 ELSE 0 END + CASE WHEN text_data LIKE '%behold%' THEN 1 ELSE 0 END + CASE WHEN text_data LIKE '%burned%' THEN 1 ELSE 0 END + CASE WHEN text_data LIKE '%consumed%' THEN 1 ELSE 0 END > 3";
    //$sql .= " LIMIT 0, 10";
    $result mysql_query($sql) OR exit( 'Error: ' mysql_error());
            function 
    highlight $str$words )
            {
            
                if ( 
    is_array$words ) ) {
                                    
    rsort($words);
                    
    $words array_map'preg_quote'$words );
                    
    $words join$words'|' );
                } else {
                    
    $words preg_quote( (string)$words );
                }
                
    //$re = '<span style="color: red; font-weight:bold;">$1</span>';
                    
    $color[0] = "red";
                    
    $color[1] = "blue";
                
    //for($c=0; $c < 8; $c++){
                    //echo $color[$c]."<br />";
                    
    $re '<span style="color: '.$color[0].'; font-weight:bold;">$1</span>';
                    
    //}
                
    return preg_replace'/\b(' $words ')\b/i'$re$str );
            }
            
    echo 
    $row['totalrows']."<br />";
    $recCount 1;
    while(
    $row mysql_fetch_array($result)){
            echo 
    "<span id='regular[]' style='padding: 5px; background-color: #D2C5A0; display: block; border: 1px solid #7C7C7C; color: black;'>";
            echo 
    "<span style='font-weight: bold; color: black; background-color: #B4B3A9; display: block; border: 1px solid #7C7C7C;'>"."<span style='font-weight: normal;'> ".$recCount."</span> ".$row["book_title"]." ".$row["chapter"].":".$row["verse"]." ";
            echo 
    "</span>\n";    

            
    $str   $row['text_data'];

            
    //var_dump( highlight( $str, $newSearchTheseArr));
            
    echo highlight$str$newSearchTheseArr);
            echo 
    "</span><br />\n";
            
    $recCount++;
    }  

    mysql_close($con);
    ?>
    </body>
    </html>
    It's returning "1:" instead of the total count of the query.
    Compare bible texts (and other tools):
    TheWheelofGod

  4. #4
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dele454 View Post
    Ok I tried to combine this into my page and got confused.
    PHP Code:
    <?php
    $searchThese 
    $_GET['searchthese'];
    $searchTheseArr $searchThese;
    $searchTheseArr=str_replace(":"""$searchTheseArr);
    $searchTheseArr=str_replace(";"""$searchTheseArr);
    $searchTheseArr=str_replace(","""$searchTheseArr);
    $searchTheseArr=str_replace("."""$searchTheseArr);
    $searchTheseArr=str_replace("?"""$searchTheseArr);
    //common words
    $searchTheseArr=str_replace("shall"""$searchTheseArr);
    $searchTheseArr=str_replace("shalt"""$searchTheseArr);
    $searchTheseArr explode(" "$searchTheseArr);

    require_once 
    "Paginated.php";
    require_once 
    "DoubleBarLayout.php";
    /******************************************************************************/
    //connection
    $con mysql_connect("","root","");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    mysql_select_db("kjv"$con);
    /******************************************************************************/
    $sql="SELECT * FROM bible WHERE 1=1 AND";
    $j=0;
    //to sort out all words with length less than 4 like AND, OR, BUT...
    for ($i=0$i count($searchTheseArr); $i++){
        if(
    strlen($searchTheseArr[$i]) > 4){
            
    $newSearchTheseArr[$j] = $searchTheseArr[$i];
            
    //j renumbers the words of length 4 and up        
            
    $j++;
            }
        }
    //sql searches only length 4 and up
    for ($j=0$j count($newSearchTheseArr); $j++){
        
    $sql.=" CASE WHEN text_data LIKE '%" .$newSearchTheseArr[$j]. "%' THEN 1 ELSE 0 END";
        if(
    $j!=count($newSearchTheseArr)-1){
            
    $sql.=" +";
            }else{
            
    //removes the OR from the last line and replaces with the following    
            
    $sql.= " > 3";
            }
        }
    $sql .= " ORDER BY id";
    echo 
    $sql."<br />\n";

    $result mysql_query($sql) OR exit( 'Error: ' mysql_error());
    echo 
    "<span style='padding: 5px; background-color: #D2C5A0; display: block; border: 1px solid #7C7C7C;'>Searching for:<br />\n";
    for (
    $j=0$j count($newSearchTheseArr); $j++){
        echo 
    "\t<span style='color: red;font-weight:bold;'>".$newSearchTheseArr[$j]."</span>\n\t\t";
        if(
    $j != count($newSearchTheseArr)-1){
            echo 
    " + \n\t\t";
            }
        }
    echo 
    "\t</span><br />\n";

    /***************************************************************test****************************************************/
    function highlight $str$words )
            {
                if ( 
    is_array$words ) ) {
                                    
    rsort($words);
                    
    $words array_map'preg_quote'$words );
                    
    $words join$words'|' );
                } else {
                    
    $words preg_quote( (string)$words );
                }
                
    //$re = '<span style="color: red; font-weight:bold;">$1</span>';
                    
    $color[0] = "red";
                    
    $color[1] = "blue";
                
    //for($c=0; $c < 8; $c++){
                    //echo $color[$c]."<br />";
                    
    $re '<span style="color: '.$color[0].'; font-weight:bold;">$1</span>';
                    
    //}
                
    return preg_replace'/\b(' $words ')\b/i'$re$str );
            }
    function 
    bookTitles($searchTheseArr){
        
    $booktitlesarray = array();
        echo 
    $sql;
        
    //$result = mysql_query($sql);
        //if($result){
            //while($row = mysql_fetch_assoc($result)){
                
    $booktitlesarray[] = "";
                
    $booktitlesarray[] .= "\t<span style='font-weight: bold; color: black; background-color: #B4B3A9; display: block; border: 1px solid #7C7C7C;'>\n";  
                
    $booktitlesarray[] .= "\t\t<span id='regular[]' style='padding: 5px; background-color: #D2C5A0; display: block; border: 1px solid #7C7C7C; color: black;'>\n";
                
    $booktitlesarray[] .= "\t\t<span style='font-weight: normal;'> ";
                
    $booktitlesarray[] .= $recCount;
                
    $booktitlesarray[] .= "\t\t</span> ";

                
    $booktitlesarray[] .= "\t\t\t<span class='goToBookChapter' style='font-weight: bold;'>";
                
    $booktitlesarray[] .= "\t\t\t".$row['book_title']." ".$row['chapter'].":".$row['verse'];
                
    $booktitlesarray[] .= "\t\t\t</span><br />\n";

                
    $str $row['text_data'];
                
    $booktitlesarray[] .= highlight$str$newSearchTheseArr);
                
    $booktitlesarray[] .= "\t\t</span>\n";
                
    $booktitlesarray[] .= "\t</span><br />\n";
            
    //}
        //}
        
    return $booktitlesarray;
    }

        
    $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(bookTitles($searchTheseArr), 10$page);
        echo 
    "<div>\n";
        while(
    $row $pagedResults->fetchPagedRow()){
            
    $recCount 1;
            echo 
    "{$row}\n";
                
    $recCount++;        
        }
        echo 
    "</div>\n";
                        
        
    $queryVars .= "";
        
    //for the url
            
    for ($j=0$j count($newSearchTheseArr); $j++){
                if(
    $j!= count($newSearchTheseArr)-1){
                    
    $queryVars .= $newSearchTheseArr[$j]."+";
                }else{
                    
    $queryVars .= $newSearchTheseArr[$j];
                }
            }
        
    //important to set the strategy to be used before a call to fetchPagedNavigation
        
    $pagedResults->setLayout(new DoubleBarLayout());
        echo 
    $pagedResults->fetchPagedNavigation($queryVars);
        
    mysql_close($con);
    ?>
    First thing in this mess, I was expecting the following to return the list of Array. It was working at first but when I started eliminating things that I thought would've been useless then I got lost. I'm getting no results.
    PHP Code:
    function bookTitles($searchTheseArr){
        
    $booktitlesarray = array();
        echo 
    $sql;
        
    //$result = mysql_query($sql);
        //if($result){
            //while($row = mysql_fetch_assoc($result)){
                
    $booktitlesarray[] = "";
                
    $booktitlesarray[] .= "\t<span style='font-weight: bold; color: black; background-color: #B4B3A9; display: block; border: 1px solid #7C7C7C;'>\n";  
                
    $booktitlesarray[] .= "\t\t<span id='regular[]' style='padding: 5px; background-color: #D2C5A0; display: block; border: 1px solid #7C7C7C; color: black;'>\n";
                
    $booktitlesarray[] .= "\t\t<span style='font-weight: normal;'> ";
                
    $booktitlesarray[] .= $recCount;
                
    $booktitlesarray[] .= "\t\t</span> ";

                
    $booktitlesarray[] .= "\t\t\t<span class='goToBookChapter' style='font-weight: bold;'>";
                
    $booktitlesarray[] .= "\t\t\t".$row['book_title']." ".$row['chapter'].":".$row['verse'];
                
    $booktitlesarray[] .= "\t\t\t</span><br />\n";

                
    $str $row['text_data'];
                
    $booktitlesarray[] .= highlight$str$newSearchTheseArr);
                
    $booktitlesarray[] .= "\t\t</span>\n";
                
    $booktitlesarray[] .= "\t</span><br />\n";
            
    //}
        //}
        
    return $booktitlesarray;

    This is all to intention of listing the results into an Array and let:
    PHP Code:
    $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(bookTitles($searchTheseArr), 10$page); 
    Sort it to 10 results per page.
    Compare bible texts (and other tools):
    TheWheelofGod

  5. #5
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dele454 View Post
    I'm trying to use this model and fill in what I need to:
    PHP Code:
    <?php
    require_once "Paginated.php";
    require_once 
    "DoubleBarLayout.php";
    ?>
    <html>
        <head>
            <title>Pagination</title>
            <style type="text/css">
                body {
                    font-family: Verdana;
                    font-size: 13px;
                }
                a {
                    text-decoration: none;
                }
                a:hover {
                    text-decoration: underline;
                }
            </style>
        </head>
        <body>
        <?php
        $con 
    mysql_connect("","root","");
        if (!
    $con){
            die(
    'Could not connect: ' mysql_error());
        }
        
    mysql_select_db("kjv"$con);
        
    /*
        function bookTitles(){
            $booktitlesarray = array();
            $query = "SELECT * FROM bible WHERE 1=1 AND";
            $query .= " CASE WHEN text_data LIKE '%" .$newSearchTheseArr[$j]. "%' THEN 1 ELSE 0 END";
            $query .= " ORDER BY id";
            //echo $query;
            $result = mysql_query($query);
            if($result){
                while($row = mysql_fetch_assoc($result)){
                    $booktitlesarray[] = "<span class='goToBookChapter' style='font-weight: bold;'>".$row['book_title']." ".$row['chapter'].":".$row['verse']."</span><br />".$row['text_data']."<br />";
                }
            }
            return $booktitlesarray;
        } 
        */
        /*******************************************************************************************************/
        //new
        
    $searchThese "the who and but Moses flock Jethro father priest Midian flock backside desert mountain Horeb angel appeared flame midst looked behold burned consumed Moses aside great sight burnt";
        
    $searchTheseArray explode(' '$searchThese);
         
        
    # Where the good words go
        
    $bigWords = array();
        
        
    # Cycle through each word
        
    foreach ($searchTheseArray as $word) {
          
    # if it's 4 chars or less
          
    if (strlen($word) > && !in_array($word,$bigWords)) {
            
    # add it to the small words
            
    $bigWords[] = $word;
          }
        }
        
        
    print_r($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";
        echo 
    "<br /><br />".$query."<br />";
        echo 
    "Words searched for:<br />\n";
        
    $bigWords array_values($bigWords);
        
        
    $COLORS = array('red','Teal','blue','Magenta','green','PaleGreen','orange','purple','Pink','YellowGreen','Sienna','aqua','Gray','LightBlue','MediumTurquoise','DarkRed');
        
    //to make $k start the $COLORS from 0
        
    $k 0;
        for (
    $j=0$j count($bigWords); $j++){
                if(
    $bigWords[$j]!=""){
                    echo 
    "<span style='font-weight: bold; color: ".$COLORS[$k].";'>".$bigWords[$j]."</span>\n";
                    if(
    $j!=count($bigWords)-1){
                        echo 
    " + ";
                        }else{
                        
    //removes the OR from the last line and replaces with the following
                        //for results of words > 3
                        
    echo ".";
                        }
                    
    $k++;    
                }
        }
        
    $result mysql_query($query);
        
    $pagedResults = new Paginated($row10$page);
        
    $page $_GET['page'];
        while(
    $row $pagedResults->fetchPagedRow(mysql_fetch_assoc($result))){
            echo 
    "<br />\n";
            echo 
    "<span class=\"goToBookChapter\" style=\"font-weight: bold;\">".$row['book_title']." ".$row['chapter'].":".$row['verse']."</span><br />\n";
        
            
    $strText $row['text_data'];
            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);
                }
            echo 
    "<br />\n";
            echo 
    $strText."<br />\n";
            }
            
    //important to set the strategy to be used before a call to fetchPagedNavigation
            
    $pagedResults->setLayout(new DoubleBarLayout());
            echo 
    $pagedResults->fetchPagedNavigation();    
        
    mysql_close($con);    
        
    /*******************************************************************************************************/
        /*
        $names = $strText; 
        //$names = bookTitles(); 
        $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($names, 10, $page);
        echo "<div>\n";
        while($row = $pagedResults->fetchPagedRow()){
            echo "<span>{$row}</span><br />\n";
        }
        echo "</div>\n";
        mysql_close($con);
        //important to set the strategy to be used before a call to fetchPagedNavigation
        $pagedResults->setLayout(new DoubleBarLayout());
        echo $pagedResults->fetchPagedNavigation();
        */
        
    ?>
    </body>
    </html>
    I don't think this is the right way of writing:
    PHP Code:
        while($row $pagedResults->fetchPagedRow(mysql_fetch_assoc($result))){ 
    Compare bible texts (and other tools):
    TheWheelofGod


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
  •