Besoin d'aide en PHP / HTML
Bonjour,
Voici un bout de code où je m'arrache les cheveux !
C'est une pagination avec un nombre de lignes, paramétrable, avec suivant et précédent.
1- Je désire arriver sur ce formulaire avec une variable (qui vient d'ailleurs) qui me détermine le nb de lignes initial de ma liste. (Dans cet exemple : 5 lignes )
2- Je navigue donc dans ma liste du choix du nb de pages. 3- J'en valide un. Le nb de ligne voulu s'affiche.
3- Et je désire que la valeur du 'selected' que je viens de valider, réapparaisse comme choix par défaut, pour qu'il puisse piloter le nombre de lignes des pages suivantes ... si je ne le modifie pas !
Merci de votre aide.
Cordialement.
Maub
Code:
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| <!doctype html>
<html>
<head>
<link href="style.css" type="text/css" rel="stylesheet">
<?php
include("config.php");
echo "<br>"; echo "<br>"; echo "<br>";
$rowperpage = 5;
$row = 0;
if(isset($_POST['rowperpage'])){
echo "nblign : ".$_POST['rowperpage'] ;
$rowperpage = $_POST ['rowperpage'] ;
}
echo "<h3> <center> Index_Demo_Pagination (".$rowperpage." lignes/page, sur 17 en tout) </h3> ";
echo "<center>";
echo '<form action="" method="POST"> ';
echo ' Nb lignes par page : ';
echo '<select name="rowperpage" >';
echo '<option value=" 3"> 3 lignes </option>';
echo '<option value=" 5"> 5 lignes </option>';
echo '<option value="10"> 10 lignes </option>';
echo '<option value="20"> 20 lignes </option>';
echo '</select>';
echo '<input type="submit" value="Valider" />';
echo "<br>"; echo "<br>";
// First Page
if(isset($_POST['but_first'])){ // Première page !!!
$row = $_POST['row'];
// $row -= $rowperpage;
$row = 0;
if( $row < 0 ){
$row = 0;
}
}
// 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;
}
}
// Last Page
if(isset($_POST['but_last'])){ // Denière page !!!
$row = $_POST['row'];
$row = $_POST['allcount'] - $rowperpage;
$allcount = $_POST['allcount'];
$val = $row + $rowperpage;
if( $val < $allcount ){
$row = $val;
}
}
// generating orderby and sort url for table header
function sortorder($fieldname){
$sorturl = "?order_by=".$fieldname."&sort=";
$sorttype = "asc";
if(isset($_GET['order_by']) && $_GET['order_by'] == $fieldname){
if(isset($_GET['sort']) && $_GET['sort'] == "asc"){
$sorttype = "desc";
}
}
$sorturl .= $sorttype;
return $sorturl;
}
?>
</head>
<body>
<div id="content">
<table width="100%" id="emp_table" border="0">
<tr class="tr_header">
<th>S.no</th>
<th ><a href="<?php echo sortorder('emp_name'); ?>" class="sort">Name</a></th>
<th ><a href="<?php echo sortorder('salary'); ?>" class="sort">Salary</a></th>
<th ><a href="<?php echo sortorder('gender'); ?>" class="sort">Gender</a></th>
<th ><a href="<?php echo sortorder('city'); ?>" class="sort">City</a></th>
<th ><a href="<?php echo sortorder('email'); ?>" class="sort">Email</a></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
$orderby = " ORDER BY id desc ";
if(isset($_GET['order_by']) && isset($_GET['sort'])){
$orderby = ' order by '.$_GET['order_by'].' '.$_GET['sort'];
}
// fetch rows
$sql = "SELECT * FROM employee ".$orderby." limit $row,".$rowperpage;
$result = mysqli_query($con,$sql);
$sno = $row + 1;
while($fetch = mysqli_fetch_array($result)){
$name = $fetch['emp_name'];
$salary = $fetch['salary'];
$gender = $fetch['gender'];
$city = $fetch['city'];
$email = $fetch['email'];
?>
<tr>
<td align='center'><?php echo $sno; ?></td>
<td align='center'><?php echo $name; ?></td>
<td align='center'><?php echo $salary; ?></td>
<td align='center'><?php echo $gender; ?></td>
<td align='center'><?php echo $city; ?></td>
<td align='center'><?php echo $email; ?></td>
</tr>
<?php
$sno ++;
}
?>
</table>
<form method="post" action="">
<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_first" value=" Première Page ">
<input type="submit" class="button" name="but_prev" value="<<< Page Précédente ">
<input type="submit" class="button" name="but_next" value="Page Suivante >>>">
<input type="submit" class="button" name="but_last" value=" Dernière Page ">
</div>
</form>
</div>
</body>
</html> |
Déformation professionnelle
Ah ? Pas fait attention, dans ce cas, si je vois bien sur mon écran de smartphone, changer la ligne 16 où l'affectation se fait et rajouter un (intval) ou int(...).
Sous php 7 j'utiliserai la syntaxe courte pour définir le tableau, je n'utiliserai donc pas = array() mais
Code:
1 2
|
$nbLignes = [3,5,10,20]; |