簡體   English   中英

PHP僅顯示最后一行中的最后一個ID號

[英]PHP only display last id number in last row

我試圖在一個頁面中顯示6個項目,每行3個項目。 當用戶點擊圖片鏈接時,它將重定向到另一個頁面並顯示ID。 但是,當用戶點擊圖像時,它會重定向到另一個頁面,但它會顯示頁面最后一行中最后一個產品的ID,這是不正確的。 我不確定我犯了哪個錯誤。 我希望你能看看我的代碼並給我一個暗示錯誤所在的提示。

      <?PHP
session_start();

function connect()
{
    $servername = xxxxxxxx;
    $username   = xxxxxxxx;
    $password   = xxxxxxxx;
    $dbname     = xxxxxxxx;

    $conn = mysqli_connect($servername, $username, $password, $dbname);
    if (!$conn) {
        echo 'connection is invalid';
    } else {
        mysqli_select_db($conn, "mytest");

    }

    return $conn;
}

function getData()
{
    $conn = connect();

    if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
        $startrow = 0;
    } else {
        $startrow = (int) $_GET['startrow'];

    }

    $sql = "SELECT * FROM tbl_products LIMIT $startrow, 6";
    $getdata = mysqli_query($conn, $sql) or die(mysqli_error());


    $cell_img = mysqli_num_rows($getdata);

    $i       = 0;
    $per_row = 3;
    echo "<table id='productTumb'><tr id='proRow'>";
    $data = '';
    while ($row = mysqli_fetch_assoc($getdata)) {
        //echo "<a href='ProDet.html'></a>";
        echo "<td><a href='test.php'><img style='vertical-align: bottom;' width='218px' height='332px' src='" . $row['products_image'] . "'/ ></a></td>";
        $data .= "<td style='background-color:#FF0004'>" . $row['product_name'] . "</td>";
        $product_id = $row['products_id'];

        $_SESSION['id'] = $product_id;
        if (++$i % $per_row == 0 && $i > 0 && $i <= $cell_img) {
            echo "</tr><tr>$data</tr><tr>";
            $data = '';




        }
    }

    for ($x = 0; $x < $per_row - $i % $per_row; $x++) {


        echo "<td></td>";


    }

    echo "</tr>";
    echo "</table>";


    echo '<a href="' . $_SERVER['PHP_SELF'] . '?startrow=' . ($startrow + 5) . '">Next >>></a>';
    $prev = $startrow - 5;
    if ($prev >= 0)
        echo '<a href="' . $_SERVER['PHP_SELF'] . '?startrow=' . $prev . '"> <<<< Previous</a>';



}







?>

這行代碼$ _SESSION ['id'] = $ product_id; 將最后的產品ID設置為SESSION。(覆蓋以前的ID)

嘗試將產品ID添加到錨點href標記中

while ($row = mysqli_fetch_assoc($getdata)) {
  echo "<td><a href='test.php?id=".$row['products_id']."'><img style='vertical-align: bottom;' width='218px' height='332px' src='" . $row['products_image'] . "'/ ></a></td>";
  ......
  }

在test.php文件中通過下面的代碼獲取id

 if(isset($_GET)){
  $pid = $_GET['id']; 
  echo $pid;
 }

希望這會對你有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM