SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pagination System not working the way i expected it to be

    Hi all i am trying to attempt to create my own pagination system i am trying to figuring out how to do a pagination system for my gallery

    this is my code

    PHP Code:
    <form method="post" action="">
    <input name="cid" type="hidden" value="<? echo $cid?>">
    </form>
    <?php
            $cid
    =($_GET['cid']);
            
    $imagethumbnail=$_POST['imagethumbnail'];
            if(
    $_GET['cid'])
                {
                
    $cid=($_GET['cid']);
                
    $imagethumbnail=$_POST['imagethumbnail'];
                
    $cid=$_GET['cid'];
                
    //$pid=$_GET['pid'];
                //echo $pid;
                
    include("dbconnect.php");
                
    $gQuery="SELECT * FROM gallery WHERE cid='$cid'";
                
    //excute the query
                //echo $gQuery;
                
    $rs=$con->query($gQuery);
                if(!
    $rs)
                {
                    
    $dbError="Error:Failed to executing query".mysqli_error($con);
                }
                else
                {
                    
    $count=$rs->num_rows;
                    if(
    $count>0)
                    {
                        
    print_r($data);
                        
    $data=$rs->fetch_assoc();
                        
    $imagethumbnail=$data['imagethumbnail'];
                    }
                    else
                    {
                        
    print_r($data);
                        echo 
    "<br/>Project Cannot be Found";
                    }
                }
            } 
                        include(
    "dbconnect.php");
                        
    //$uQuery="SELECT * FROM gallery WHERE cid='$cid'";
                        
    $limit=3;
                        
    $page=$_GET['page'];
                        
    $totalrows=mysqli_num_rows(mysqli_query("select * from gallery");
                        if(empty(
    $page))
                        {
                            
    $page='1';
                        }
                        
    $start=($page-1)*$limit;
                        
    $start=round($start,0);
                        
    $rs=mysqli_query("select * from gallery LIMIT $start,$limit");
                        while(
    $r=mysqli_fetch_aray($result))
                        {    
                            if(!
    $rs
                        { 
                            echo 
    "Error:".mysqli_error($con); 
                        } 
                        else 
                        { 
                                
    $display 5;
                                
    $pg = (isset($_REQUEST['pg']) &&
                                
    ctype_digit($_REQUEST['pg'])) ?
                                
    $_REQUEST['pg'] : 1;
                                
    $start $display $pg $display;
                                
    $count=$rs->num_rows
                                if(
    $count>0
                                { 
                                     while(
    $data=$rs->fetch_assoc()) 
                                    {
                                    echo 
    "<tr><td><a href=gallery/".$data['image']."><img src=gallery/".$data['imagethumbnail']." width=\"100px\" height=\"100px\"></a></td></tr>";
                                    }
                                }
                                
    $totalpage=$totalrows/$limit;
                                
    $totalpages=ceil($totalpages);
                                if(
    $page==1)
                                {
                                    
    $actualpage='[1]';
                                }
                                else
                                {
                                    
    $actualpage="[$page]";
                                }
                                if(
    $page<$totalpages)
                                {
                                    
    $nv=$page+1;
                                    
    $pv=$page-1;
                                    
    $nextpage="<a href=?page=$nv>></a>";
                                    
    $prevpage="<a href=?page=$nv><</a>";
                                    
    $firstpage="<a href=?page=$nv></a>";
                                    
    $finalpage="<a href=?page=$totalpage\"></a>";    
                                } 
                                if(
    $page=='1')
                                {
                                    
    $nv=$page+1;
                                    
    $nextpage="<a href=?page=$nv>></a>";
                                    
    $prevpage="<";
                                    
    $firstpage="";
                                    
    $finalpage="<a href=?page=$totalpage\"></a>";
                                }
                                elseif(
    $page==$totalpages)
                                {
                                    
    $pv=$page-1;
                                    
    $nextpage=">";
                                    
    $prevpage "<a href=?page=$pv><</a>";
                                    
    $firstpage "<a href=\"?page=1\"></a>";
                                    
    $finalpage "";
                                }
                                if(
    $totalpage=='1' || $totalpage=='0')
                                {
                                    
    $nextpage ">";
                                    
    $prevpage "<";
                                    
    $firstpage "";
                                    
    $finalpage "";
                                }
                            }

    echo 
    "$firstpage $prevpage $actualpage $nextpage $finalpage<br>Pages: $totalpages";//echoes the pages at the botton of the file                    }
    ?>
    Doesnt seem to work how i want it to do i want it to have a pagination system of what is in the cid so cid is the category number so doesnt matter how many images there are in the gallery system i want it so its 3 pictures per page

    this is my code before i made the changes above

    PHP Code:
    <form method="post" action="">
    <input name="cid" type="hidden" value="<? echo $cid?>">
    </form>
    <?php
            $cid
    =($_GET['cid']);
            
    $imagethumbnail=$_POST['imagethumbnail'];
            if(
    $_GET['cid'])
                {
                
    $cid=($_GET['cid']);
                
    $imagethumbnail=$_POST['imagethumbnail'];
                
    $cid=$_GET['cid'];
                
    //$pid=$_GET['pid'];
                //echo $pid;
                
    include("dbconnect.php");
                
    $gQuery="SELECT * FROM gallery WHERE cid='$cid'";
                
    //excute the query
                //echo $gQuery;
                
    $rs=$con->query($gQuery);
                if(!
    $rs)
                {
                    
    $dbError="Error:Failed to executing query".mysqli_error($con);
                }
                else
                {
                    
    $count=$rs->num_rows;
                    if(
    $count>0)
                    {
                        
    print_r($data);
                        
    $data=$rs->fetch_assoc();
                        
    $imagethumbnail=$data['imagethumbnail'];
                    }
                    else
                    {
                        
    print_r($data);
                        echo 
    "<br/>Project Cannot be Found";
                    }
                }
            } 
                        include(
    "dbconnect.php");
                        
    //$uQuery="SELECT * FROM gallery WHERE cid='$cid'";
                        
    $rQuery="SELECT * FROM `gallery` LIMIT 0 , 3";
                        
    $rs=mysqli_query($con,$rQuery);
                        
    //$rs=mysqli_query($con,$uQuery); 
                        
    if(!$rs
                        { 
                            echo 
    "Error:".mysqli_error($con); 
                        } 
                        else 
                        { 
                            
    $display 5;
                            
    $pg = (isset($_REQUEST['pg']) && ctype_digit($_REQUEST['pg'])) ?
                              
    $_REQUEST['pg'] : 1;
                            
    $start $display $pg $display;
                            
    $count=$rs->num_rows
                            if(
    $count>0
                            { 
                                while(
    $data=$rs->fetch_assoc()) 
                                {
                                    echo 
    "<tr><td>\"<img src=gallery/".$data['imagethumbnail']." width=\"100px\" height=\"100px\"></td></tr>";
                                }
                            } 

                        }
    ?>
    What am i doing wrong?

  2. #2
    SitePoint Addict
    Join Date
    Sep 2005
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How-To Paginate w/PHP/MySQL

    First, I have to point out that your technique of querying the database with a GET variable is very dangerous. You can research "sql injections" to find out why. I recommend you change your code:
    Change
    $cid=$_GET['cid'];
    $gQuery="SELECT * FROM gallery WHERE cid='$cid'";
    To
    $cid=(int)($_GET['cid']);
    $gQuery="SELECT * FROM gallery WHERE cid='$cid'";

    This makes sure that "cid" is only a number or will be set to 0 (which is safe).

    Now, how to paginate. In general, you have several things to do:
    1. Figure out how many pages there are
    2. Figure out what records you need to retrieve
    3. Build your First, Previous, Next and Last links
    4. Display your data

    To figure out how many pages there are you can either run your complete query and then get the record count with mysql_num_rows($result) or you can write a separate query to retrieve the record count in a more efficient way "SELECT count(*) AS count FROM gallery...".

    Either way, you only have to do this once because you can then store the number of records with each navigation link. This means that if any additional records are added, they won't get picked up so you have to decide to either run the query to find the record count with every page load or pass it along with the nav links.

    When your page is loaded you need to check to see if there is a $_GET["page"] variable set. This is the variable you pass to determine what page you are on. If it is not set then you are obviously on page 1.

    When you build your navigation links include the page number variable and the total number of records variable in the links. You can build the variables to use with the example below:

    if ($totalRecords < 1){
    $totalRecords = 0;
    $startRecord = 0;
    $endRecord = 0;
    $firstPage = 0;
    $lastPage = 0;
    $pageNum = 0;
    }
    else{
    $pageNum = 1;
    if (isset($_REQUEST["page"]))
    $pageNum = $_REQUEST["page"];
    $startLimit = ($pageNum - 1) * $maxRows; // 0 offset to the record to start from
    $endLimit = $startLimit + $maxRows;
    $startRecord = $startLimit + 1;
    $firstPage = 1;
    $lastPage = (int)ceil($totalRecords / $maxRows);
    $prevPage = $pageNum - 1;
    if ($prevPage < 1)
    $prevPage = 1;
    $nextPage = $pageNum + 1;
    if ($nextPage > $lastPage)
    $nextPage = $lastPage;
    $endRecord = $startRecord + $maxRows - 1;
    if ($endRecord > $totalRecords)
    $endRecord = $totalRecords;


    If you are on page 3 ($maxitems = 3 items per page):
    previous page link = gallery.php?cid=1&page=$prevPage&reccount=$totalRecords;
    next page link = gallery.php?cid=1&page=$nextPage&reccount=$totalRecords;
    first page link = gallery.php?cid=1&page=$firstPage&reccount=$totalRecords;
    last page link = gallery.php?cid=1&page=$lastPage&reccount=$totalRecords;

    Now, when you display your data, only query the database for the actual records you need:

    "SELECT * FROM gallery WHERE cid='$cid' LIMIT $startlimit, $maxrows".

    Loop through all records returned.

    Things to note:
    You'll need to put logic around your navigation links because you don't want to display them if they are irrelavent (i.e. don't display the "first page" link if you are on the first page).

    Good luck with your project.
    PHP Shopping Cart Software Easy Ecommerce Shopping Cart Script.
    PHP Super Cart is 100% template driven.

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here is a function that i made for myself:
    PHP Code:
        function PaginateData($start,$perPage,$total,$page){
            if(
    $total >= 1){
                echo 
    "<b>";
                
    //for previous
                
    $previous $start $perPage;
                if(
    $start and $start < ($total $perPage))
                    echo 
    " <a href=\"$page&start=$previous\" class=\"A2\" title=\"Previous\">&laquo; Previous</a> ";
                else if(
    $start and $start >= ($total $perPage))
                    echo 
    " <a href=\"$page&start=$previous\" class=\"A2\" title=\"Previous\">&laquo; Previous</a> ";
                
    //complete previous
                
    echo "Page : | ";
                
    $l 1;
                for(
    $i 0$i $total$i $i $perPage){
                    if(
    $i != $start)
                        
    $a[$l] = "<a class=\"A2\" href=\"$page&start=$i\">$l</a>&nbsp;";
                    else
                        
    $a[$l] = "$l&nbsp;";
                    
    $l++;
                }
                foreach(
    $a as $p=>$pp)
                    echo 
    $pp;
                
    //for next
                
    echo " | ";
                
    $next $start $perPage;
                if((
    $start == 0) and $total $perPage)
                    echo 
    " <a class=\"A2\" href=\"$page&start=$next\" title=\"Next\">Next &raquo;</a>";
                else if(
    $start and $start < ($total $perPage))
                    echo 
    " <a class=\"A2\" href=\"$page&start=$next\" title=\"Next\">Next &raquo;</a>";    
                
    //complete next
            
    }
            echo 
    "</b>";
        } 
    And i used like this:
    PHP Code:
        //paging
        
    $perPage 40;
        
    $total 0;
        if(isset(
    $_REQUEST['start'])){$start $_REQUEST['start'];}
        else{
    $start 0;} 
    And finnaly i called the function like this:
    PHP Code:
    $url "show_visitinfo.php?ProjectID=$prjid&fdate=$FDate&tdate=$TDate";
    PaginateData_($start,$perPage,$total,$url
    This may help you.

    I have not studied your code.

  4. #4
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    where do i put $url?

  5. #5
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have tried to do this pagination system into mine but i got back

    page 1 out of 0 and its not showing 3 images only its showing all of them this is my code

    PHP Code:
    <link href="billmayer.css" rel="stylesheet" type="text/css">
    <?php 
    include("includes/header.inc.php");
    ?>
    <div id="imga">
    <div align="center">
    <table border="1">
    <tr>
        <td>Categories</td>
    </tr>
    <?php

                                    
    include("dbconnect.php");
                                    
    $cQuery="SELECT * FROM categories";
                                    
    $result=mysqli_query($con,$cQuery);
                                    if(!
    $result)
                                    {
                                        echo 
    "Error:".mysqli_error($con);
                                    }
                                    else
                                    {
                                        
    $count=$result->num_rows;
                                        if(
    $count>0)
                                        {
                                            while(
    $data=$result->fetch_assoc())
                                            {
                                                echo 
    "<tr><td><a href=\"?cid=".$data['cid']."\">".$data['CategoryName']."</tr></td>";
                                            }
                                        }
                                        else
                                        {
                                        echo 
    "There is no Categories in the database";
                                        }
                                    }
                                    
    ?>
    </table>
    </div>
    </div>
    <div id="indexpageCPanel2">
    <div id="indextext2">
    <form method="post" action=""> 
    <input name="cid" type="hidden" value="<? echo $cid?>"> 
    </form> 
    <?php
            $cid
    =($_GET['cid']); 
            
    $imagethumbnail=$_POST['imagethumbnail']; 
            if(
    $_GET['cid']) 
                { 
                
    $cid=($_GET['cid']); 
                
    $imagethumbnail=$_POST['imagethumbnail']; 
                
    $cid=$_GET['cid']; 
                
    //$pid=$_GET['pid']; 
                //echo $pid; 
                
    include("dbconnect.php"); 
                
    $gQuery="SELECT * FROM gallery WHERE cid='$cid'"
                
    //excute the query 
                //echo $gQuery; 
                
    $rs=$con->query($gQuery); 
                if(!
    $rs
                { 
                    
    $dbError="Error:Failed to executing query".mysqli_error($con); 
                } 
                else 
                { 
                    
    $count=$rs->num_rows
                    if(
    $count>0
                    { 
                        
    print_r($data); 
                        
    $data=$rs->fetch_assoc(); 
                        
    $imagethumbnail=$data['imagethumbnail']; 
                    } 
                    else 
                    { 
                        
    print_r($data); 
                        echo 
    "<br/>Project Cannot be Found"
                    } 
                } 
            } 
                                            if (isset(
    $_GET['pageno'])) {
                                       
    $pageno $_GET['pageno'];
                                    } else {
                                       
    $pageno 1;
                                    }
                        
    //$query = "SELECT count(*) FROM table WHERE ...";
                        /*$result = mysql_query($query, $db) or trigger_error("SQL", E_USER_ERROR);
                        $query_data = mysql_fetch_row($result);
                        */
                        
    error_reporting(E_ALL);
                        
    $rows_per_page 3;
                        
    $lastpage  ceil($num_rows/$rows_per_page);
                        include(
    "dbconnect.php");
                        if (isset(
    $_GET['pageno'])) {
                           
    $pageno $_GET['pageno'];
                        } else {
                           
    $pageno 1;
                        } 
    // if
                        
    $query "SELECT count(*) FROM gallery";
                        echo 
    $query;
                        
    $rs=mysqli_query($con,$query);
                        if(!
    $rs)
                        {
                            echo 
    "Error:".mysqli_error($con);
                        }
                        else
                        {
                            
    $query_data mysqli_fetch_row($result);
                            
    $numrows $query_data[0];
                            
    //$uQuery="SELECT * FROM gallery WHERE cid='$cid'"; 
                            
    $rQuery;
                            
    $rQuery="SELECT * FROM gallery where cid='$cid'"
                            
    $rs=mysqli_query($con,$rQuery); 
                            
    //$rs=mysqli_query($con,$uQuery); 
                            
    if(!$rs
                            { 
                                echo 
    "Error:".mysqli_error($con); 
                            } 
                            else 
                            { 
                                if(
    $count>0
                                { 
                                    
    $query_data mysqli_fetch_row($rs);
                                    
    $num_rows $query_data[0];
                                    while(
    $data=$rs->fetch_assoc()) 
                                    { 
                                        echo 
    "<tr><td>\"<img src=gallery/".$data['imagethumbnail']." width=\"100px\" height=\"100px\"></td></tr>"
                                    } 
                                } 
        
                            } 
                        }
                        
    $rows_per_page 3;
                        
    $lastpage      ceil($numrows/$rows_per_page);
                        
    $limit ='LIMIT ' .($pageno 1) * $rows_per_page .',' .$rows_per_page;
                        
    $query "SELECT * FROM gallery $limit";
                        echo 
    " ( Page $pageno of $lastpage ) ";
        
    ?> 
    </div></div>
    <?php 
    include("includes/footer.inc.php");
    ?>
    3 Images per page but its not doing that what i am doing wrong?

  6. #6
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can anyone help me?

  7. #7
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by william232 View Post
    Can anyone help me?
    Hello what exactly you want to do with pagination? did you check out what i have done in my site (the site i have given in your mail)? do you mean that like this?

  8. #8
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well i want my script to get the cid and get 3 rows per page on the cid but when i do that its showing all the images cid is category id and i have 3 categories

    So i want it select everything where cid='$cid'";
    and then select the first 3 rows within that cid;
    if there are more than 3 rows of the image in the database then go to next page.


    By the rajug where do i put the url is the url same as the query?

  9. #9
    SitePoint Wizard spence_noodle's Avatar
    Join Date
    Jan 2004
    Location
    uk, Leeds (area)
    Posts
    1,264
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Try this.

    You may have to change the code to suit your database, etc...

    Near to the top of the page place this there:
    PHP Code:
    // category id
    $cid = (int)($_GET['cid']);

    // category id form the pagination back
    $backpage = (int)($_GET['back']);

    // get page number
    if(!isset($_GET['page']))
    {
         
    $page 1;

    else 
    {
        
    $page $_GET['page'];
    }

    // Define the number of results per page
    $max_results 20;
                    
    // Figure out the limit for the query based
    // on the current page number.
    $from = (($page $max_results) - $max_results); 
    Place this code at the very end of your mysql_query for your results:
    PHP Code:
    ORDER BY whatever DESC LIMIT $from$max_results

    // so it would look something like this
    $result mysql_query("SELECT blah, blah FROM my_table WHERE blah=blah ORDER BY whatever DESC LIMIT $from$max_results"); 
    Place this after your list of results:
    PHP Code:
    if($max_results)
    {
                                
    $result_final .= "<div>
                                <p align='left'>"
    ;
                                
                                
    // Figure out the total number of results in DB:
                                
    $total_results mysql_result(mysql_query("SELECT COUNT(*) as Num FROM my_table WHERE blah=".addslashes($cid)." "),0);

                                
    // Figure out the total number of pages. Always round up using ceil()
                                
    $total_pages ceil($total_results $max_results);

                                if (
    $total_pages >= 1)
                                {  
    // build links if more than one page

                                    // Build Page Number Hyperlinks
                                    
    $result_final .=  "page: ".$page.' of '.$total_pages.";
                                    
                                    // Build first page link
                                    if((
    $page) > $max_results)
                                    {
                                        // check if the page sent
                                        // does it equal the amount of total_pages?
                                        if(
    $page == $total_pages)
                                        {
                                            
    $start = ($page - $max_resultsl);
                                            
    $end = ($page * 1);
                                            
    $result_final .=  "<a href=\"".$_SERVER['PHP_SELF']."?cid=$cid&back=$backpage&page=1\" title='First Page'>&laquo; first</a>";
                                        } else { 
                                            
    $start = ($page $max_resultsl);
                                            
    $end = ($page 1);
                                            
    $result_final .=  "<a href=\"".$_SERVER['PHP_SELF']."?cid=$cid&back=$backpage&page=1\" title='First Page'>&laquo; first</a>";
                                        }
                                    } else {
                                        if(
    $total_pages $max_results){
                                            
    $start 1;
                                            
    $end $total_pages;                                        
                                        } else {
                                            
    $start 1;
                                            
    $end $max_results;
                                        }
                                    }
                                                                
                                    
    // Build Previous Link
                                    
    if($page 1){
                                        
    $prev = ($page 1);
                                        
    $result_final .=  "<a href=\"".$_SERVER['PHP_SELF']."?cid=$cid&back=$backpage&page=$prev\" title='Previous Page'>‹</a>";
                                    }

                                    
    // start from / end to number of pages
                                    
    for($i $start$i <= $end$i++)
                                    {                                    
                                        
    // Build the number of pages as numbers
                                        
    if(($page) == $i){
                                            
    $result_final .= "$i";
                                        } else {
                                            
    $result_final .=  "<a href=\"".$_SERVER['PHP_SELF']."?cid=$cid&back=$backpage&page=$i\" title='Page ".$i."'>$i</a>";
                                        }
                                    }

                                    
    // Build Next Link
                                    
    if($page $total_pages)
                                    {
                                        
    $next = ($page 1);
                                        
    $result_final .=  "<a href=\"".$_SERVER['PHP_SELF']."?cid=$cid&back=$backpage&page=$next\" title='Next Page'>›</a>";
                                    }
                                    
                                    
    // Build last page link
                                    
    if($total_pages $max_results)
                                    {
                                        
    $result_final .=  "<a href=\"".$_SERVER['PHP_SELF']."?cid=$cid&back=$backpage&page=$total_pages\" title='Last Page'>last &raquo;</a>";
                                    }
                                    
                            
    $result_final .=  "</p>
                            </div>
                            <br />"
    ;
                            }
                            else
                            {
            
    $result_final .=  "\n";

    I hope this helps more

    spence
    "Don't you just love it when you solve a programming bug only to create another."

  10. #10
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Spence but i used the code you gave me i got this error

    Error:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC LIMIT 0, 3' at line 1
    Warning: mysql_result(): supplied argument is not a valid MySQL result resource in C:\Program Files\xampp\htdocs\BillMayersArtGalleryCms\Public_html\gallerytest.php on line 131

    And the line i have is

    PHP Code:
    $total_results=mysql_result($dQuery=("select Count(*) as Num FROM gallery where cid=".addslashes($cid).""),0); 
    What am i doing wrong?

  11. #11
    SitePoint Wizard spence_noodle's Avatar
    Join Date
    Jan 2004
    Location
    uk, Leeds (area)
    Posts
    1,264
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Ok, try this:

    PHP Code:
    $total_results=mysql_result(mysql_query("select Count(*) as Num FROM gallery where cid='".addslashes($cid)."' "),0); 
    spence
    "Don't you just love it when you solve a programming bug only to create another."

  12. #12
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now i get this error

    Error:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC LIMIT 0, 3' at line 1
    Warning: Wrong parameter count for mysql_result() in C:\Program Files\xampp\htdocs\BillMayersArtGalleryCms\Public_html\gallerytest.php on line
    Using

    PHP Code:
    $total_results=mysql_result(mysqli_query($con,("select Count(*) as Num FROM gallery where cid='".addslashes($cid)."' "),0)); 
    What am i doing wrong?

  13. #13
    SitePoint Wizard spence_noodle's Avatar
    Join Date
    Jan 2004
    Location
    uk, Leeds (area)
    Posts
    1,264
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Mmmm...any chance you can post your page again so we can view what you've done?
    "Don't you just love it when you solve a programming bug only to create another."

  14. #14
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yea sure this is my code

    PHP Code:
    <link href="billmayer.css" rel="stylesheet" type="text/css">
    <?php 
    include("includes/header.inc.php");
    ?>
    <div id="imga">
    <div align="center">
    <table border="1">
    <tr>
        <td>Categories</td>
    </tr>
    <?php

                                    
    include("dbconnect.php");
                                    
    $cQuery="SELECT * FROM categories";
                                    
    $result=mysqli_query($con,$cQuery);
                                    if(!
    $result)
                                    {
                                        echo 
    "Error:".mysqli_error($con);
                                    }
                                    else
                                    {
                                        
    $count=$result->num_rows;
                                        if(
    $count>0)
                                        {
                                            while(
    $data=$result->fetch_assoc())
                                            {
                                                echo 
    "<tr><td><a href=\"?cid=".$data['cid']."\">".$data['CategoryName']."</tr></td>";
                                            }
                                        }
                                        else
                                        {
                                        echo 
    "There is no Categories in the database";
                                        }
                                    }
                                    
    ?>
    </table>
    </div>
    </div>
    <div id="indexpageCPanel2">
    <div id="indextext2">
    <form method="post" action=""> 
    <input name="cid" type="hidden" value="<? echo $cid?>"> 
    </form> 
    <?php
            $cid 
    = (int)($_GET['cid']);
            
    // category id form the pagination back
            
    $backpage = (int)($_GET['back']);
            
    // get page number
            
    if(!isset($_GET['page']))
            {
                 
    $page 1;
            } 
            else 
            {
                
    $page $_GET['page'];
            }
            
    // Define the number of results per page
            
    $max_results 3;        
            
    // Figure out the limit for the query based
            // on the current page number.
            
    $from = (($page $max_results) - $max_results); 
            
    $cid=($_GET['cid']); 
            
    $imagethumbnail=$_POST['imagethumbnail']; 
            if(
    $_GET['cid']) 
                { 
                
    $cid=($_GET['cid']); 
                
    $imagethumbnail=$_POST['imagethumbnail']; 
                
    $cid=$_GET['cid']; 
                
    //$pid=$_GET['pid']; 
                //echo $pid; 
                
    include("dbconnect.php"); 
                
    $gQuery="SELECT * FROM gallery WHERE cid='$cid'"
                
    //excute the query 
                //echo $gQuery; 
                
    $rs=$con->query($gQuery); 
                if(!
    $rs
                { 
                    
    $dbError="Error:Failed to executing query".mysqli_error($con); 
                } 
                else 
                { 
                    
    $count=$rs->num_rows
                    if(
    $count>0
                    { 
                        
    print_r($data); 
                        
    $data=$rs->fetch_assoc(); 
                        
    $imagethumbnail=$data['imagethumbnail']; 
                    } 
                    else 
                    { 
                        
    print_r($data); 
                        echo 
    "<br/>Project Cannot be Found"
                    } 
                } 
            }  
                        
    $query "SELECT count(*) FROM gallery"
                        
    $rs=mysqli_query($con,$query); 
                        if(!
    $rs
                        { 
                            echo 
    "Error:".mysqli_error($con); 
                        } 
                        else 
                        { 
                            
    $query_data mysqli_fetch_row($result); 
                            
    $numrows $query_data[0]; 
                            
    //$uQuery="SELECT * FROM gallery WHERE cid='$cid'"; 
                            
    $rQuery
                            
    $rQuery="SELECT * FROM gallery where cid='$cid' DESC LIMIT $from$max_results"
                            
    $rs=mysqli_query($con,$rQuery); 
                            
    //$rs=mysqli_query($con,$uQuery); 
                            
    if(!$rs
                            { 
                                echo 
    "Error:".mysqli_error($con); 
                            } 
                            else 
                            { 
                                if(
    $count>0
                                { 
                                    
    $query_data mysqli_fetch_row($rs); 
                                    
    $num_rows $query_data[0]; 
                                    while(
    $data=$rs->fetch_assoc()) 
                                    { 
                                        echo 
    "<tr><td>\"<img src=gallery/".$data['imagethumbnail']." width=\"100px\" height=\"100px\"></td></tr>"
                                    } 
                                }      
                            } 
                        }
                        
    $result_final .= "<div>
                                <p align='left'>"
    ;
                                    
    // Figure out the total number of results in DB:
                                //$total_results=mysql_result($dQuery=("select Count(*) as Num FROM gallery where cid=".addslashes($cid).""),0);
                                //$total_results=mysql_result(mysqli_query($con,("select Count(*) as Num FROM gallery where cid='".addslashes($cid)."' "),0));
                                
    $total_results=mysql_result(mysqli_query($con,("select Count(*) as Num FROM gallery where cid='".addslashes($cid)."' "),0));
                                
    // Figure out the total number of pages. Always round up using ceil()
                                
    $total_pages ceil($total_results $max_results);

                                if (
    $total_pages >= 1)
                                {  
    // build links if more than one page

                                    // Build Page Number Hyperlinks
                                    
    $result_final .=  "page: ".$page.' of '.$total_pages."";
                                    
                                    
    // Build first page link
                                    
    if(($page) > $max_results)
                                    {
                                        
    // check if the page sent
                                        // does it equal the amount of total_pages?
                                        
    if($page == $total_pages)
                                        {
                                            
    $start = ($page $max_resultsl);
                                            
    $end = ($page 1);
                                            
    $result_final .=  "<a href=".$_SERVER['PHP_SELF']."?cid=$cid&back=$backpage&page=1\" title='First Page'>&laquo; first</a>";
                                        } else { 
                                            
    $start = ($page $max_resultsl);
                                            
    $end = ($page 1);
                                            
    $result_final .=  "<a href=".$_SERVER['PHP_SELF']."?cid=$cid&back=$backpage&page=1\" title='First Page'>&laquo; first</a>";
                                        }
                                    } else {
                                        if(
    $total_pages $max_results){
                                            
    $start 1;
                                            
    $end $total_pages;                                        
                                        } else {
                                            
    $start 1;
                                            
    $end $max_results;
                                        }
                                    }
                                                                
                                    
    // Build Previous Link
                                    
    if($page 1){
                                        
    $prev = ($page 1);
                                        
    $result_final .=  "<a href=\"".$_SERVER['PHP_SELF']."?cid=$cid&back=$backpage&page=$prev\" title='Previous Page'></a>";
                                    }

                                    
    // start from / end to number of pages
                                    
    for($i $start$i <= $end$i++)
                                    {                                    
                                        
    // Build the number of pages as numbers
                                        
    if(($page) == $i){
                                            
    $result_final .= "$i";
                                        } else {
                                            
    $result_final .=  "<a href=\"".$_SERVER['PHP_SELF']."?cid=$cid&back=$backpage&page=$i\" title='Page ".$i."'>$i</a>";
                                        }
                                    }

                                    
    // Build Next Link
                                    
    if($page $total_pages)
                                    {
                                        
    $next = ($page 1);
                                        
    $result_final .=  "<a href=\"".$_SERVER['PHP_SELF']."?cid=$cid&back=$backpage&page=$next\" title='Next Page'></a>";
                                    }
                                    
                                    
    // Build last page link
                                    
    if($total_pages $max_results)
                                    {
                                        
    $result_final .=  "<a href=\"".$_SERVER['PHP_SELF']."?cid=$cid&back=$backpage&page=$total_pages\" title='Last Page'>last &raquo;</a>";
                                    }
                                    
                            
    $result_final .=  "</p>
                            </div>
                            <br />"
    ;
                            }
                            else
                            {
            
    $result_final .=  "\n"
                        } 
    ?>
    Thats my Whole Page

  15. #15
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can anyone help me?

  16. #16
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,104
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I use a fully modified version of this and it works great

    http://www.phpbuilder.com/board/arch...-10284854.html

    hope it helps, Merry Christmas

  17. #17
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still Having Problem i am getting the error


    Error:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''' at line 2
    Warning: mysql_result(): supplied argument is not a valid MySQL result resource in C:\Program Files\xampp\htdocs\BillMayersArtGalleryCms\Public_html\gallerytest.php on line 140

    $total_results=mysql_result($rs,0);

  18. #18
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if there is an error in your sql, maybe you should look at the sql?

  19. #19
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How can i fix that the query is

    the first one is:

    PHP Code:
    $rQuery="SELECT imagethumbnail FROM gallery where cid='$cid' ORDER BY id DESC LIMIT $from$max_results"
    the second one is

    PHP Code:
    $cQuery="select Count(*) 
                                as Num FROM gallery where cid='"
    .addslashes($cid)."ORDER BY cid DESC LIMIT $from$max_results"

  20. #20
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    echo the problem sql.

    get it working in phpmyadmin, then duplicate it in php.

  21. #21
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is what i got back

    SELECT imagethumbnail FROM gallery where cid='' ORDER BY id DESC LIMIT 0, 3

    select Count(*) as Num FROM gallery where cid='ORDER BY cid DESC LIMIT 0, 3Error:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''ORDER BY cid DESC LIMIT 0, 3' at line 2

    How can i fix that?

  22. #22
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its been fixed.

  23. #23
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am having the same problem, how did you fix it please?

  24. #24
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is what i have you may need to change it but this is what i got and feel free to change it the way you want it to work aswell.

    Here is what i have

    PHP Code:
    $cid=($_GET['cid']);
                                if($_GET['cid'])
                                {
                                    $cQuery="SELECT * FROM gallery WHERE cid='$cid'";
                                    $result=mysqli_query($con,$cQuery);
                                    if(!$result)
                                    {
                                        echo "Error:".mysqli_error($con);
                                    }
                                    else
                                    {
                                        $count=$result->num_rows;
                                        if($count>0)
                                        {
                                            while($data=$result->fetch_assoc())
                                            {
                                                $cid=$data['cid'];
                                                $alt=$data['alt'];
                                                $description=$data['description'];
                                                $imagethumbnail=$data['imagethumbnail'];
                                            }
                                        }
                                        else
                                        {
                                        echo "<br/>There is no Gallery Informartion Found<br/>";
                                        }
                                    }
                                }?><form method="post" action="">
    <input type="hidden" name="cid" value="<?php $cid ?>"></form>
    <?php
    $num 
    $_GET['num'];//Get the numeration of the page
    if(empty($num)){//if the numeration is empty
    $num 1;//the numeration is 1
    };
    $limit 3;//Sets the limit of results to display in each page, change if you want.
    /*
    The query will start selecting the numeration, for example 2, less 1, that would be 1
    * the limits of results to show per page, 2 in this case so it would be 1*2 = 2, it will
    start from 2 ;) if the limit would be 5 and the numeration would be 3 if would be (3-1)*5 = 10
    */
    $start = ($num-1)*$limit;
    $start round($start,0);//rounds the result
    /*
    This query will select the contrene FROM the start and with a limit of 2, in this case,
    because the variable $limit is 2
    You can add a WHERE something= '$something' for example, or delete the ORDER by `id`, or change it,
    etc
    */
    $query "SELECT * FROM gallery where cid='$cid' ORDER by `id` LIMIT $start$limit";
    $result mysqli_query($con,$query);//now it makes the query and names it as result
    /*
    While will repeat this query and mysql_fect_array allow me array the content
    */
    echo "<table>";
    while (
    $data mysqli_fetch_array($result)){
    echo 
    "<tr><td><a href=\"showart.php?id=".$data['id']."\"><img src=gallery/".$data['imagethumbnail']." width=\"100px\" height=\"100px\" alt=".$data['alt']."></a></td><td>".$data['description']."</td></tr><br/>";//Echoes the content
    };
    echo 
    "</table>";
    $totalpages mysqli_num_rows(mysqli_query($con,"SELECT * from gallery where cid='$cid'"));//Get the total number of results
    /*
    Total resutls/ the limit, in this example is 2, so if there are 10 total result and the limit is 2
    there will be 5 pages.
    */
    $totalpages $totalpages $limit;
    $totalpages round($totalpages,0);
    $c 0;//The variable c is 0
    echo "<br>";//make a <br> to separate the results from the [1][2]...
    while($c<$totalpages){//while c is < than the total pages
    $page $c 1;//sets the variable $page as 0 + 1 = 1
    if($_GET['num']==$page){//Gets the number of the page and if its the same that the page
    echo "[$page] ";//its only echoes the page, not the url to this page
    }else{//else
    echo "<a href=?cid=".$cid."&num=$page>[$page] </a>";//it echoes the url to the page
    }
    $c $c+1;
    }
    echo 
    "<br>".$totalpages." Pages in total.";//echoes the total pages


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
  •