#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    33
    Rep Power
    2

    Need help with retaining selection through page navigation


    Hi!

    I am a relative newbie to PHP and have learnt PHP on my own and therefore my programs may look very juvenile. But I am putting the piece of my code below where I need help with the logic. The following piece of code is expected the user to select an option from a drop-down menu and then show the data pertaining to that option only. So far so good. But the data is displayed in multiple pages with page navigation at the bottom, obviously I have used PHP_SELF to do it. Without the selection form, the logic of pages being displayed in the multiples of 12 records shows perfectly. When the selection part was added, it showed the first page properly but couldn't retain the selected value through out the navigation and therefore the second page on wards, it started showing the complete data (which is default when no option is selected) Can you please help me with this logic? I may have missed the bus and the mistake may have been really silly but I have spent long enough time and I am unable to see the hitch in it. Any help is appreciated. Following is the code.

    PHP Code:
    if($_SESSION['logged_in']){

    include(
    "database_connection.php");

    echo 
    "<p align='left'><strong><small>Hello ".$_SESSION['name']."!</small><br><br>VIEW CHILD DETAILS</strong></p><br>";
    echo 
    "<p align='left'><strong>NOTE: </strong>Click on the <strong>Child Id</strong> below the photo to view the detailes of this child.</p><br>";

    $phpself $_SERVER["PHP_SELF"];

    $rowsPerPage 12;
    if(isset(
    $_GET['page'])) {
    $pageNum$_GET['page'];
    } else {
    $pageNum 1
    }

    $previousRows =($pageNum 1) * $rowsPerPage;

    if(
    $_SESSION['name'] == "PUNE") {
      
    $_SESSION['selected'] = " LIKE '%'";
      
    $centers="SELECT name, usercode FROM users WHERE usercode !='ES'";
      
    $centers_result=mysql_query($centers);
      
    $options=array();
      
    $options[]="<option value=''></option>";
      while (
    $centers_rows=mysql_fetch_array($centers_result)) {
       
    $options[]="<option value='$centers_rows[name]'>$centers_rows[name]</option>";
      }
       if(isset(
    $_GET['center'])) {
        
    $_SESSION['selected'] = "='".$_GET['center']."'";
       }
      echo 
    "<form action='".$phpself."' method='get'>"
      echo 
    "Select Center: ";
      echo 
    "<select name='center' size='1'>".implode('\n'$options)."</select>";
      echo 
    "<input type='submit' name='submit' value='Ok' />";
      echo 
    "</form>";
    echo 
    $_SESSION['selected'];
    $query="SELECT child_id, child_name, child_photo FROM child WHERE child_place".$_SESSION['selected']." LIMIT $previousRows$rowsPerPage";
    } else {
    $query="SELECT child_id, child_name, child_photo FROM child WHERE child_place='".$_SESSION['name']."' LIMIT $previousRows$rowsPerPage";
    }

    $result=mysql_query($query);
    $a mysql_num_rows($result);

    if (!
    $result)
      {
      die(
    'record does not exist');
      } 

    if(
    $a 0) {
    echo 
    "<table width='100%' cellspacing='5' cellpadding='2' border='1'>";
    echo 
    "<tr>";

    $i=0;

    while (
    $row mysql_fetch_array($result)) {

    if(
    $i <= 2) {
    echo 
    "<td width='30%' align='center'><img src='http://www.bsskindia.org/es/upload/$row[child_photo]' border='1'><br><form action='childdetails.php' method='post'><input type='hidden' name='id_val' value='$row[child_id]'><input type='submit' name='submit' value='$row[child_id]'></form>$row[child_name]</td>";
    $i++;
    } else {
    echo 
    "</tr>";
    echo 
    "<tr>";
    echo 
    "<td width='30%' align='center'><img src='http://www.bsskindia.org/es/upload/$row[child_photo]' border='1'><br><form action='childdetails.php' method='post'><input type='hidden' name='id_val' value='$row[child_id]'><input type='submit' name='submit' value='$row[child_id]'></form>$row[child_name]</td>";
    $i=1;
    }

    }

    echo 
    "</tr>";
    echo 
    "</table>";
    } else {
    echo 
    "No records found!";
    }

    if(
    $_SESSION['name'] == "PUNE") {
    $query="SELECT COUNT(child_id) AS numrows FROM child WHERE child_place".$_SESSION['selected'];
    } else {
    $query="SELECT COUNT(child_id) AS numrows FROM child WHERE child_place='".$_SESSION['name'];
    }
    $result mysql_query($query) or die('Error, couldn\'t get count title=\"$page\"').mysql_error();
    $row mysql_fetch_assoc($result);
    $numrows $row['numrows'];

    $lastPage ceil($numrows/$rowsPerPage);

    if (
    $pageNum 1) {

    $page $pageNum 1;
    $prev "<a href=\"$phpself?page=$page\" title=\"Page $page\"> [Back]</a>";
    $first "<a href=\"$phpself?page=1\" title=\"Page 1\">[First Page] </a>";

    } else {

    $prev "&nbsp;[Back]&nbsp;";
    $first "&nbsp;[First Page]&nbsp;";

    }

    if (
    $pageNum $lastPage) {

    $page $pageNum 1;
    $next "<a href=\"$phpself?page=$page\" title=\"Page $page\">[Next] </a>";
    $last "<a href=\"$phpself?page=$lastPage\" title=\"Page $lastPage\"> [Last Page]</a>";

    } else {

    $next "&nbsp;[Next]&nbsp;";
    $last "&nbsp;[Last Page]&nbsp;";

    }

    if(
    $numrows 0) {
    echo 
    "<br><p align='center'><strong>".$first.$prev."&nbsp;&nbsp;< < Showing page <strong>$pageNum</strong> of <strong>$lastPage</strong> pages > >&nbsp;&nbsp;".$next.$last."</strong></p><br>";
    }

    } else {
    echo 
    "<br><br><br><p align='center'><strong>YOU ARE NOT LOGGED IN AND SO CAN NOT VIEW DATA!</strong></p><br>";
    echo 
    "<p align='center'><strong>LOGIN FIRST BY CLICKING ON Login ON THE LEFT.</strong></p>";

    Any help is greatly appreciated.
    Thanks in advance!

    Mozart66
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Sep 2006
    Posts
    2,035
    Rep Power
    535
    Hello mozart66,

    Two suggestions. First separate your logic from your HTML. I know it seems petty, but it will almost always lead to problems if you do not. I also know that it is faster using one loop to both get the data and display it, but if the code is buggy, it doesn't matter if it is fast. See below for a sample untested example. Note that this is not exactly how I would do it, but it is somewhat close, and you should do what makes sense.

    After you get it nice and simple, concentrate on your logic. What I kind of got out of your narrative is if on the first page, display a select menu, and use the value of that select menu to display some results on both the first page as well as subsequent pages. Seems like your problem is happening somewhere within the below code. Hopefully, now that it is just a small section, you can figure it out. A couple of suggestions. Don't store SQL in a session. If you need a variable which is part of your SQL, you do what you need to do, but be sure to escape it. Also, I do not understand your use of "LIKE %". Bottom line, Keep it Simple!

    PHP Code:
        if($_SESSION['name'] == "PUNE") {
            
    $_SESSION['selected'] = " LIKE '%'";
            if(isset(
    $_GET['center'])) {
                
    $_SESSION['selected'] = "='".$_GET['center']."'";
            }
            
    $query="SELECT child_id, child_name, child_photo FROM child WHERE child_place".$_SESSION['selected']." LIMIT $previousRows$rowsPerPage";
        } else {
            
    $query="SELECT child_id, child_name, child_photo FROM child WHERE child_place='".$_SESSION['name']."' LIMIT $previousRows$rowsPerPage";
        } 
    PHP Code:
    function view($data)
    {

        echo 
    "<p align='left'><strong><small>Hello ".$_SESSION['name']."!</small><br><br>VIEW CHILD DETAILS</strong></p><br>";
        echo 
    "<p align='left'><strong>NOTE: </strong>Click on the <strong>Child Id</strong> below the photo to view the detailes of this child.</p><br>";

        if(!empty(
    $options))
        {
            echo 
    "<form action='".$data['phpself']."' method='get'>"
            echo 
    "Select Center: ";
            echo 
    "<select name='center' size='1'>".implode('\n'$data['options'])."</select>";
            echo 
    "<input type='submit' name='submit' value='Ok' />";
            echo 
    "</form>";
            echo 
    $_SESSION['selected'];
        }

        if(
    $data['record_count'] > 0) {
            echo 
    "<table width='100%' cellspacing='5' cellpadding='2' border='1'>";
            echo 
    "<tr>";

            
    $i=0;

            foreach(
    $results as $row) {

                if(
    $i <= 2) {
                    echo 
    "<td width='30%' align='center'><img src='http://www.bsskindia.org/es/upload/$row[child_photo]' border='1'><br><form action='childdetails.php' method='post'><input type='hidden' name='id_val' value='$row[child_id]'><input type='submit' name='submit' value='$row[child_id]'></form>$row[child_name]</td>";
                    
    $i++;
                } else {
                    echo 
    "</tr>";
                    echo 
    "<tr>";
                    echo 
    "<td width='30%' align='center'><img src='http://www.bsskindia.org/es/upload/$row[child_photo]' border='1'><br><form action='childdetails.php' method='post'><input type='hidden' name='id_val' value='$row[child_id]'><input type='submit' name='submit' value='$row[child_id]'></form>$row[child_name]</td>";
                    
    $i=1;
                }

            }

            echo 
    "</tr>";
            echo 
    "</table>";
        } else {
            echo 
    "No records found!";
        }

    }

    function 
    notLoggedOn()
    {
        echo 
    "<br><br><br><p align='center'><strong>YOU ARE NOT LOGGED IN AND SO CAN NOT VIEW DATA!</strong></p><br>";
        echo 
    "<p align='center'><strong>LOGIN FIRST BY CLICKING ON Login ON THE LEFT.</strong></p>";
    }


    //Your logic goes here...................

    if($_SESSION['logged_in']) {notLoggedOn();} 

    else {

        include(
    "database_connection.php");

        
    $data=array();
        
    $data['phpself'] = $_SERVER["PHP_SELF"];

        
    $rowsPerPage 12;
        if(isset(
    $_GET['page'])) {
            
    $pageNum$_GET['page'];
        } else {
            
    $pageNum 1
        }

        
    $previousRows =($pageNum 1) * $rowsPerPage;

        
    $data['options']=array();
        if(
    $_SESSION['name'] == "PUNE") {
            
    $centers="SELECT name, usercode FROM users WHERE usercode !='ES'";
            
    $centers_result=mysql_query($centers);
            
    $data['options'][]="<option value=''></option>";
            while (
    $centers_rows=mysql_fetch_array($centers_result)) {
                
    $data['options'][]="<option value='$centers_rows[name]'>$centers_rows[name]</option>";
            }
        }

        if(
    $_SESSION['name'] == "PUNE") {
            
    $_SESSION['selected'] = " LIKE '%'";
            if(isset(
    $_GET['center'])) {
                
    $_SESSION['selected'] = "='".$_GET['center']."'";
            }
            
    $query="SELECT child_id, child_name, child_photo FROM child WHERE child_place".$_SESSION['selected']." LIMIT $previousRows$rowsPerPage";
        } else {
            
    $query="SELECT child_id, child_name, child_photo FROM child WHERE child_place='".$_SESSION['name']."' LIMIT $previousRows$rowsPerPage";
        }

        
    $result=mysql_query($query);
        
    $data['record_count'] = mysql_num_rows($result);

        if (!
    $result)
        {
            die(
    'record does not exist');
        } 

        
    $results=array();
        while (
    $row mysql_fetch_array($result)) {
            
    $results[]=$row;
        }
        
    $data['results']=$results;

        
    //Deal with your pagination

        //Display the page!
        
    view($data);


  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    33
    Rep Power
    2
    Originally Posted by NotionCommotion
    Hello mozart66,

    Two suggestions. First separate your logic from your HTML. I know it seems petty, but it will almost always lead to problems if you do not. I also know that it is faster using one loop to both get the data and display it, but if the code is buggy, it doesn't matter if it is fast. See below for a sample untested example. Note that this is not exactly how I would do it, but it is somewhat close, and you should do what makes sense.

    After you get it nice and simple, concentrate on your logic. What I kind of got out of your narrative is if on the first page, display a select menu, and use the value of that select menu to display some results on both the first page as well as subsequent pages. Seems like your problem is happening somewhere within the below code. Hopefully, now that it is just a small section, you can figure it out. A couple of suggestions. Don't store SQL in a session. If you need a variable which is part of your SQL, you do what you need to do, but be sure to escape it. Also, I do not understand your use of "LIKE %". Bottom line, Keep it Simple!

    PHP Code:
        if($_SESSION['name'] == "PUNE") {
            
    $_SESSION['selected'] = " LIKE '%'";
            if(isset(
    $_GET['center'])) {
                
    $_SESSION['selected'] = "='".$_GET['center']."'";
            }
            
    $query="SELECT child_id, child_name, child_photo FROM child WHERE child_place".$_SESSION['selected']." LIMIT $previousRows$rowsPerPage";
        } else {
            
    $query="SELECT child_id, child_name, child_photo FROM child WHERE child_place='".$_SESSION['name']."' LIMIT $previousRows$rowsPerPage";
        } 
    PHP Code:
    function view($data)
    {

        echo 
    "<p align='left'><strong><small>Hello ".$_SESSION['name']."!</small><br><br>VIEW CHILD DETAILS</strong></p><br>";
        echo 
    "<p align='left'><strong>NOTE: </strong>Click on the <strong>Child Id</strong> below the photo to view the detailes of this child.</p><br>";

        if(!empty(
    $options))
        {
            echo 
    "<form action='".$data['phpself']."' method='get'>"
            echo 
    "Select Center: ";
            echo 
    "<select name='center' size='1'>".implode('\n'$data['options'])."</select>";
            echo 
    "<input type='submit' name='submit' value='Ok' />";
            echo 
    "</form>";
            echo 
    $_SESSION['selected'];
        }

        if(
    $data['record_count'] > 0) {
            echo 
    "<table width='100%' cellspacing='5' cellpadding='2' border='1'>";
            echo 
    "<tr>";

            
    $i=0;

            foreach(
    $results as $row) {

                if(
    $i <= 2) {
                    echo 
    "<td width='30%' align='center'><img src='http://www.bsskindia.org/es/upload/$row[child_photo]' border='1'><br><form action='childdetails.php' method='post'><input type='hidden' name='id_val' value='$row[child_id]'><input type='submit' name='submit' value='$row[child_id]'></form>$row[child_name]</td>";
                    
    $i++;
                } else {
                    echo 
    "</tr>";
                    echo 
    "<tr>";
                    echo 
    "<td width='30%' align='center'><img src='http://www.bsskindia.org/es/upload/$row[child_photo]' border='1'><br><form action='childdetails.php' method='post'><input type='hidden' name='id_val' value='$row[child_id]'><input type='submit' name='submit' value='$row[child_id]'></form>$row[child_name]</td>";
                    
    $i=1;
                }

            }

            echo 
    "</tr>";
            echo 
    "</table>";
        } else {
            echo 
    "No records found!";
        }

    }

    function 
    notLoggedOn()
    {
        echo 
    "<br><br><br><p align='center'><strong>YOU ARE NOT LOGGED IN AND SO CAN NOT VIEW DATA!</strong></p><br>";
        echo 
    "<p align='center'><strong>LOGIN FIRST BY CLICKING ON Login ON THE LEFT.</strong></p>";
    }


    //Your logic goes here...................

    if($_SESSION['logged_in']) {notLoggedOn();} 

    else {

        include(
    "database_connection.php");

        
    $data=array();
        
    $data['phpself'] = $_SERVER["PHP_SELF"];

        
    $rowsPerPage 12;
        if(isset(
    $_GET['page'])) {
            
    $pageNum$_GET['page'];
        } else {
            
    $pageNum 1
        }

        
    $previousRows =($pageNum 1) * $rowsPerPage;

        
    $data['options']=array();
        if(
    $_SESSION['name'] == "PUNE") {
            
    $centers="SELECT name, usercode FROM users WHERE usercode !='ES'";
            
    $centers_result=mysql_query($centers);
            
    $data['options'][]="<option value=''></option>";
            while (
    $centers_rows=mysql_fetch_array($centers_result)) {
                
    $data['options'][]="<option value='$centers_rows[name]'>$centers_rows[name]</option>";
            }
        }

        if(
    $_SESSION['name'] == "PUNE") {
            
    $_SESSION['selected'] = " LIKE '%'";
            if(isset(
    $_GET['center'])) {
                
    $_SESSION['selected'] = "='".$_GET['center']."'";
            }
            
    $query="SELECT child_id, child_name, child_photo FROM child WHERE child_place".$_SESSION['selected']." LIMIT $previousRows$rowsPerPage";
        } else {
            
    $query="SELECT child_id, child_name, child_photo FROM child WHERE child_place='".$_SESSION['name']."' LIMIT $previousRows$rowsPerPage";
        }

        
    $result=mysql_query($query);
        
    $data['record_count'] = mysql_num_rows($result);

        if (!
    $result)
        {
            die(
    'record does not exist');
        } 

        
    $results=array();
        while (
    $row mysql_fetch_array($result)) {
            
    $results[]=$row;
        }
        
    $data['results']=$results;

        
    //Deal with your pagination

        //Display the page!
        
    view($data);


    A BIG THANK YOU!!!! I will surely try and test your logic! The reason I wanted to store SQL in a SESSION variable was because I wasn't getting the variable value repeating on every page so I thought that the SESSION values since are not destroyed easily, I could make use of SESSION variable to access the value anytime on any page. The main reason was that probably I couldn't completely understand how PHP_SELF works. Anyway, I am very glad to get important tips from you and will certainly modify my program as per your suggestions! Thank you!!

IMN logo majestic logo threadwatch logo seochat tools logo