สอบถามเกี่ยวกับ pagination การเลือกเเสดงข้อมูลในเเต่ละหน้า (มีภาพประกอบ)
ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา สอบถามเกี่ยวกับ pagination การเลือกเเสดงข้อมูลในเเต่ละหน้า (มีภาพประกอบ)
สอบถามเกี่ยวกับ pagination การเลือกเเสดงข้อมูลในเเต่ละหน้า (มีภาพประกอบ)
จากรูปตัวอย่าง คือผมจะทำปุ่มที่จะกดเลือกได้ว่า ต้องการให้เเสดงข้อมูล ในหน้านั้น จำนวนเท่าไหร่ ซึ่งผมไม่ทราบว่ามันรียกว่าอะไร เเละรบกวนขอตัวอย่างในการเขียนด้วยครับ
ภาพที่1

ภาพที่2

ภาพที่1
ภาพที่2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | $row = 0; // number of rows per page $rowperpage = 5; if (isset( $_POST [ 'num_rows' ])){ $rowperpage = $_POST [ 'num_rows' ]; } // Previous Button if (isset( $_POST [ 'but_prev' ])){ $row = $_POST [ 'row' ]; $row -= $rowperpage ; if ( $row < 0 ){ $row = 0; } } // Next Button if (isset( $_POST [ 'but_next' ])){ $row = $_POST [ 'row' ]; $allcount = $_POST [ 'allcount' ]; $val = $row + $rowperpage ; if ( $val < $allcount ){ $row = $val ; } } ?> </head> <body> <div class = "container" > <table width= "100%" id= "emp_table" border= "0" > <tr class = "tr_header" > <th>S.no</th> <th>Name</th> <th>Salary</th> </tr> <?php // count total number of rows $sql = "SELECT COUNT(*) AS cntrows FROM employee" ; $result = mysqli_query( $con , $sql ); $fetchresult = mysqli_fetch_array( $result ); $allcount = $fetchresult [ 'cntrows' ]; // selecting rows $sql = "SELECT * FROM employee ORDER BY ID ASC limit $row," . $rowperpage ; $result = mysqli_query( $con , $sql ); $sno = $row + 1; while ( $fetch = mysqli_fetch_array( $result )){ $name = $fetch [ 'emp_name' ]; $salary = $fetch [ 'salary' ]; ?> <tr> <td align= 'center' ><?php echo $sno ; ?></td> <td align= 'center' ><?php echo $name ; ?></td> <td align= 'center' ><?php echo $salary ; ?></td> </tr> <?php $sno ++; } ?> </table> <!-- Pagination control --> <form method= "post" action= "" id= "form" > <div id= "div_pagination" > <input type= "hidden" name= "row" value= "<?php echo $row; ?>" > <input type= "hidden" name= "allcount" value= "<?php echo $allcount; ?>" > <input type= "submit" class = "button" name= "but_prev" value= "Previous" > <input type= "submit" class = "button" name= "but_next" value= "Next" > <!-- Number of rows --> <div class = "divnum_rows" > <span class = "paginationtextfield" >Number of rows:</span> <select id= "num_rows" name= "num_rows" > <?php $numrows_arr = array ( "5" , "10" , "25" , "50" , "100" , "250" ); foreach ( $numrows_arr as $nrow ){ if (isset( $_POST [ 'num_rows' ]) && $_POST [ 'num_rows' ] == $nrow ){ echo '<option value="' . $nrow . '" selected="selected">' . $nrow . '</option>' ; } else { echo '<option value="' . $nrow . '">' . $nrow . '</option>' ; } } ?> </select> </div> </div> </form> |

คำแนะนำ และการใช้งาน
สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก
- ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
ความคิดเห็นที่
1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <!-- Pagination control --> <form method= "post" action= "" id= "form" > <div id= "div_pagination" > <input type= "hidden" name= "row" value= "<?php echo $row; ?>" > <input type= "hidden" name= "allcount" value= "<?php echo $allcount; ?>" > <input type= "submit" class = "button" name= "but_prev" value= "Previous" > <input type= "submit" class = "button" name= "but_next" value= "Next" > <style type= "text/css" > .btn-page-navi{ display:inline-block; border:1px solid #CCC; background-color:wihte; color:#000; text-align:center; } .btn-page-navi.active{ background-color:green; color:#FFF; text-align:center; } </style> <!-- Number of rows --> <div class = "divnum_rows" > <span class = "paginationtextfield" >Number of rows:</span> <?php $numrows_arr = array ( "5" , "10" , "25" , "50" , "100" , "250" ); foreach ( $numrows_arr as $nrow ){ $active_class = "" ; if (isset( $_POST [ 'num_rows' ]) && $_POST [ 'num_rows' ] == $nrow ){ $active_class = "active" ; } ?> <button name= "num_rows" class = "btn-page-navi <?=$active_class?>" type= "submit" value= "<?=$nrow?>" ><?= $nrow ?></button> <?php } ?> </div> </div> </form> |

ความคิดเห็นที่
2
ขอบคุณครับ

ความคิดเห็นที่
3
พี่ครับ ตอนนี้ติดปัญหาตรงที่เมื่อ กดเลือกจำนวนที่ต้องการให้เเสดง เช่น เลือก 10 ในหน้านั้นก็จะเเสดง จำนวน 10 รายการเเล้ว เเต่เมื่อผมกด next Previous มันจะกลับไปเเสดง ค่าเดิม คือค่าเริ่มต้น 5 ครับ ตรงนี้ ต้องเเก้ยังไงครับ รบกวนอีกครั้งนะครับ

ความคิดเห็นที่
4
ถ้าง่ายสุด ก็เพิ่ม input hidden ที่ชื่อ เหมือนปุ่มว่า num_rows เข้าไป
1 2 3 4 5 | <input type= "hidden" name= "num_rows" value= "<?php echo $rowperpage; ?>" > <input type= "hidden" name= "row" value= "<?php echo $row; ?>" > <input type= "hidden" name= "allcount" value= "<?php echo $allcount; ?>" > <input type= "submit" class = "button" name= "but_prev" value= "Previous" > <input type= "submit" class = "button" name= "but_next" value= "Next" > |
ให้เข้าใจเสมอว่า ปุ่ม input กับปุ่ม button ค่าจะถูกไป เมื่อคลิก ถ้าไม่คลิก จะไม่มีตัวแปร และไม่มีค่าถูกส่งไป
อ่านเนื้อหานี้เป็นความรู้เพิ่มเติม http://niik.in/que_2933_6542
ก่อนแก้ปัญหา ตัวแปร $rowperpage ถูกกำหนดจากค่าเริ่มต้นเป็น 5 หรือจากค่าของ $_POST['num_rows'] ถ้ามีการกดปุ่ม
นั่นคือทุกครั้งที่เรากดที่ปุ่ม แบ่งจำนวนที่จะแสดง ก็จะได้ค่าที่ถูกต้อง แต่พอเราไม่ได้กดที่ปุ่ม <button name="num_rows">
เช่น กรณีกดปุ่ม but_prev หรือ but_next แทน ค่าของตัวแปร $rowperpage จึงเป็นค่าเริ่มต้นเสมอ คือ 5 เพราะค่าของ
$_POST['num_rows'] ไม่ถูกส่งมาด้วย
พอเราแก้ปัญหาโดยเพิ่ม input hidden ที่ชื่อ num_rows ที่เป็นชื่อเดียวกับปุ่ม และให้มีค่าเท่ากับค่าที่ส่งมาล่าสุดหรือก็คือค่า
ของตัวแปร $rowperpage นั่นคือ ถ้ากดปุ่ม but_prev หรือ but_next ค่า num_rows จะใช้ของ input hidden ซี่งเป็นค่าที่จะถูกส่งไปใน
ฟอร์มเสมอ แต่ถ้ากดที่ปุ่ม num_rows กรณีนี้ จะเป็นว่า เราส่งทั้งค่า hidden และ ค่าจาก button หรือก็คือมีค่าจาก $_POST['num_rows']
สองค่าถูกส่งไป แต่จริงแล้วค่าที่จะถูกใช้งานจะเป็นค่าตัวหลัง ที่เป็นค่าจากปุ่มนั่นเอง ตามเงื่อนไขที่ว่า ถ้าชื่อเหมือนกัน จะใช้ตัวสุดท้าย
อ่านเนื้อหานี้เป็นความรู้เพิ่มเติม http://niik.in/que_2933_6542
จริงๆ เราสามารถใช้วิธีการส่งค่าที่อาจจะใช้งาน ได้หลายแบบ เช่น เป็นตัวแปร SESSION หรือส่งแบบ GET ไปใน URL
ก็คงต้องไปทำความเข้าใจเพิ่มเติม

ขอบคุณทุกการสนับสนุน
![]()