1.11M Members

php mysql search with pagination

 
0
 

Hi. can someone help me with my problem regarding search function, i have hard time knowing where to insert my
if(isset($_POST['search'])).................and so on codes within my phpfile.
here is my code, (pagination working)

    <?php
    $sql = "SELECT COUNT(user_id) FROM view_onse_officers";
    $query = mysqli_query($db_conx, $sql);
    $row = mysqli_fetch_row($query);
    $rows = $row[0];
    $page_rows =10;
    $last = ceil($rows/$page_rows);
    if($last < 1){
        $last = 1;
    }
    $pagenum = 1;
    if(isset($_GET['pn'])){
        $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
    }

    if ($pagenum < 1) { 
        $pagenum = 1; 
    } else if ($pagenum > $last) { 
        $pagenum = $last; 
    }

    $limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;

    $sql = "SELECT user_id, studentno, username, CONCAT(last_name, ', ', first_name) AS name, posname, course, gender FROM view_onse_officers $limit";
    $query = mysqli_query($db_conx, $sql);

    $textline2 = "Page <b>$pagenum</b> of <b>$last</b>";

    $paginationCtrls = '';

    if($last != 1){

        if ($pagenum > 1) {
            $previous = $pagenum - 1;
            $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> &nbsp; &nbsp; ';

            for($i = $pagenum-4; $i < $pagenum; $i++){
                if($i > 0){
                    $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
                }
            }
        }

        $paginationCtrls .= ''.$pagenum.' &nbsp; ';

        for($i = $pagenum+1; $i <= $last; $i++){
            $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
            if($i >= $pagenum+4){
                break;
            }
        }

        if ($pagenum != $last) {
            $next = $pagenum + 1;
            $paginationCtrls .= ' &nbsp; &nbsp; <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a> ';
        }
    }
    $list = '';

    ?>

                <br><br>

    <table width="600" border="0" cellspacing="1" cellpadding="0">
        <tr class="onse">
            <td>
                <form name="form1" method="post" action="">
                    <table width="600" border="0" cellpadding="3" cellspacing="0" bgcolor="#CCCCCC">
                        <tr class="onse">
                            <td class="onseTwo" colspan="6" bgcolor="#FFFFFF" align="center"><strong>ONSE OFFICERS</strong> </td>
                        </tr>

                        <tr class="onse">

                            <td class ="onseOne" width ="150px"align="center" bgcolor="#FFFFFF"><strong>Student number</strong></td>
                            <td class ="onseOne" align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
                            <td class ="onseOne" align="center" bgcolor="#FFFFFF"><strong>Gender</strong></td>
                            <td class ="onseOne" align="center" bgcolor="#FFFFFF"><strong>Course</strong></td>
                            <td class ="onseOne" align="center" bgcolor="#FFFFFF"><strong>Position</strong></td>

                        </tr>

    <?php
    while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){?>

    <?
        $id = $row["studentno"];
        $firstname = $row["name"];
        $gender = $row["gender"];
        $lastname = $row["course"];
        $posname = $row["posname"];
        $profile =$row["username"];

    ?>

                        <tr class="onse">

                            <td><? echo $id ; ?></td>
                            <td><a class="prof" href = "<?echo $profile ; ?>"><? echo  $firstname;?></a></td>
                            <td><?echo $gender;?></td>
                            <td><?echo  $lastname ; ?></td>
                            <td><? echo  $posname; ?></td>

                        </tr>

    <?php
    }
    ?>
    <?mysqli_close($db_conx);
    ?>


                    </table>
                </form>
            </td>
        </tr>
    </table>

    <h2>Search</h2> 
            <form action="search.php" method="post" >
                    Search: <input type="text" name="search" placeholder="Search for student name"/>
                        <input type="submit" value="Search" />
     </form>



      <p><?php echo $list; ?></p>
    <div id="pagination_controls"><?php echo $paginationCtrls; ?></div><br>
    <p class="onsepage"><?php echo $textline2; ?></p>



    <center><a class="link" href="onse.php">See all the Members of onse</a></center>
 
0
 

For pagination, this will help you Pagination

 
0
 

my pagination is working without search.
but when i input on my search box, it only shows data from $limit.
example i have 100 data on my table
then i search for name 'ben'. if there is 50 data that has name like ben
it only shows 10 base on the $limit that i've declared. thanks for the source link but i already have my pagination

 
0
 

Hi

can you test line 24.

just for test replace 24 as below

 $sql = "SELECT user_id, studentno, username, CONCAT(last_name, ', ', first_name) AS name, posname, course, gender FROM view_onse_officers $limit, 10";
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: