2016-04-18 12 views
0

基本的に私はデータの入ったテーブルを持っていますが、箱やものが入ったフォームがあります。私がやっていることは、次のボタンまたは前のボタンをクリックすると、データベースからの次のレコード。私は最初と最後のレコードを行うことができますが、私はその間を把握することができません。これは私が持っているもの。PHP MYSQL次と前の行

$sql="SELECT * FROM Emails where ID > 1 ORDER BY UserEmail LIMIT 1"; 

     if ($result=mysqli_query($connection,$sql)) 
     { 

     // Return the number of rows in result set 
     $rowcount=mysqli_num_rows($result); 

     while($row = mysqli_fetch_array($result)) 
     { 
     $Email_Field   = $row['UserEmail'];   //primary key 
     $Name_Field   = $row['UserName'];   
     $UserTel    = $row['UserTel'];   
     $Drop_Down   = $row['Drop_down'];  
     $MessageType   = $row['MessageType'];  
     $Comments   = $row['Comments'];   
     $SubjectOther  = $row['SubjectOther'];   
     $Check    = $row['Request'];   
     } 

<form method="POST" action="Controller_leads.php"> 
    <p><strong>What kind of comment would you like to send?</strong></p> 
    <input type="radio" <?php if ($MessageType == "Complaint") echo "checked"; ?> name="MessageType" value="Complaint">Complaint 
    <input type="radio" <?php if ($MessageType == "Problem") echo "checked"; ?> name="MessageType" value="Problem">Problem 
    <input type="radio" <?php if ($MessageType == "Suggestion") echo "checked"; ?> name="MessageType" value="Suggestion">Suggestion 
    <br> 
    <p><strong>What about us do you want to comment on?</strong></p> 

    <select name="Drop_Down" size="1"> 
     <option value ="Web Site" <?php if ($Drop_Down == "Web Site") echo selected ?>>Web Site</option> 
     <option value ="Office Hours" <?php if ($Drop_Down == "Office hours") echo selected ?>>Office Hours</option> 
     <option value ="Pamphlet" <?php if ($Drop_Down == "Pamphlet") echo selected ?>>Pamphlet</option> 
    </select> 

    Other: <input type="text" size="26" maxlength="256" name="SubjectOther" value="<?php echo $SubjectOther ?>"> 

    <p><strong>Enter your comments in the space provided below:</strong></p> 

    <textarea name="Comments" rows="5" cols="42"><?php echo $Comments;?></textarea><br><br> 

    <strong>Tell us how to get in touch with you:</strong><br><br> 

    <table> 
     <tr><td width="45">&nbsp;Name  </td> <td><input type="text" size="35" maxlength="256" name="UserName" value="<?php echo $Name_Field ?> "></td></tr> 
     <tr><td width="45">&nbsp;E-mail </td> <td><input type="text" size="35" maxlength="256" name="UserEmail" value="<?php echo $Email_Field ?>"></td></tr> 
     <tr><td width="45">&nbsp;Telephone</td> <td><input type="text" size="35" maxlength="256" name="UserTel" value="<?php echo $UserTel ?>"></td></tr> 
    </table> 

    <br> 
    <input type="checkbox" name="Check" <?php if ($Check == "Contact Requested") echo checked; ?> value="Contact Requested">Please contact me as soon as possible regarding this matter 
    <br><br> 
    <input type="submit" value="First" name="first"> 
    <input type="submit" value="Next" name="next"> 
    <input type="submit" value="Previous" name="previous"> 
    <input type="submit" value="Last" name="last"> code here 
+0

PHP mysqlページネーションを探します – Mihai

+0

UserEmailは数字ですか?素晴らしい –

+0

データベースから次のレコードを取得するためにオフセットを使用します。 –

答えて

0

クエリにオフセットを追加してみてください。それぞれの上で、オフセットを$ offsetに1つ追加すると、それまでのオフセットごとに1つ減算されます。そこで、このようなクエリでオフセットが含ま:フォームで

# get the current offset 

# initial value 
$offset = 1; 

# if we have an offset from a previous or next click, use that 
if (isset($_POST['offset'])) { 

    # validate this input to protect against sql injection 
    if (is_int($_POST['offset'])) { 
     $offset = $_POST['offset']; 
    } 

    # now that we have our current value, see if we need to get the next or previous 
    if ($_POST['submit']=="Next") { 
     # next, add one offset 
     $offset++; 
    } else if ($_POST['submit']=="Previous") { 
     # previous, go back one if we are greater than one 
     if ($offset > 1) { 
      $offset--; 
     } 
    } 
} 

# query time, give me one result (LIMIT 1), staring at record $offset 
$sql = "select SELECT * FROM Emails where UserEmail > 1 
     ORDER BY UserEmail LIMIT 1, $offset"; 

これを追加します。別のノートオン

<input type="hidden" name="offset" value="<?php echo $offset; ?>"> 

を、USEREMAIL> 1は奇妙なようだが、私はあなたのデータを知りません。

+0

この状況でオフセットがどのように役立つか理解していません.iは何をしようとしているのかを見るためにhtmlパーツを追加しました。最初のボタンをクリックすると、最初のデータが表示され、次にクリックすると次の行が表示されますが、もう一度クリックすると、そのデータが表示されます。入力タイプをサブミットとして使用する必要がありますか? –

+0

はい、ユーザー入力を使用してオフセットを変更する必要があります。私は答えに例を追加しました。 – Michael

+0

nmでは、オフセットは完全に無視され、sqlクエリを実行するだけで、オフセットを増加させることなく同じクエリを実行します。 –