首页 > 解决方案 > 试图创建一个动态的 swiper

问题描述

我正在尝试创建一个动态刷卡器,它能够在我对数据库进行更改时自动更新。我现在遇到的问题是我在 swiper 中有 6 张图片和描述,但它们都只从我的数据库的第一个字段中检索。 页面的旧屏幕截图。正如您从第一个屏幕截图中看到的那样,我能够从数据库中检索图片和描述的数据,但只能从数据库的第一个字段中重复检索所有 6 个结果。能够在php区域显示它。 当我在 html 中调用该函数时,只有第一个滑块在工作。 任何帮助将不胜感激,谢谢。

<?php
$connect = mysqli_connect("localhost", "root", "", "db");
$result = make_query($connect);

function fetch_array(&$array) {
    // Grab the first value from the array
    $return = current($array);
    // remove the value we just grabbed 
    array_shift($array);
    // if what we have is an array spit it out, else return false
    return is_array($return) ? $return : false;
}

function make_query($connect) { 
    $query = "SELECT * FROM db.slider ORDER BY p_id ASC";
    $result = mysqli_query($connect, $query);
    return $result;
}

function make_slide_indicators($result) {
    $output = ''; 
    $count = 0;

    for($i = 0;$i<mysqli_num_rows($result);$i++) {
    //for($i = 0;$i<count($result);$i++) {
        if ($i == 0) {
            $output .= '<li data-target="#" data-slide-to="'.$i.'" class="active"></li>';
        } else {
            $output .= '<li data-target="#" data-slide-to="'.$i.'"></li>';
        }
         $count++ === $count = $count + 1;
    }
    return $output;
}

function make_slides($result) {
    $output = '';
    $count = 0;
    
    while($row = mysqli_fetch_assoc($result)) {
    //while($row = fetch_array($result)) {
        // Not needed as the output is the same
        if($count == 0) {
        $output .= '
    <div class="swiper-slide platform">
        <img src="'.$row["p_img"].'" alt="'.$row["p_name"].'" />
        <div class="swiper-slide platform">
            <h3>'.$row["p_desc"].'</h3>
        </div>
    </div>';
        // Not used at the moment
         $count++;
        }
    }
    return $output;
}

$connect = mysqli_connect("localhost", "root", "", "db");
$result = make_query($connect);
$result = [['p_img' => 'img','p_name' => 'name','p_desc' => 'desc'],['p_img' => 'img','p_name' => 'name','p_desc' => 'desc'],['p_img' => 'img','p_name' => 'name','p_desc' => 'desc'],['p_img' => 'img','p_name' => 'name','p_desc' => 'desc']];

echo make_slide_indicators($result);
echo PHP_EOL;
echo make_slides($result);
?>

标签: phpdatabase

解决方案


使用数据库时使用 while 而不是 foreach。您遇到的问题是 mysqli_result 并不完全是您认为的那样,您可以在这里查看: https ://www.php.net/manual/en/class.mysqli-result.php

这是提供的代码的干净版本。我没有为此设置数据库,因此为了测试我实现了一个虚拟函数来处理集合数组的逻辑。我希望评论是有帮助的。

function fetch_array(&$array) {
    // Grab the first value from the array
    $return = current($array);
    // remove the value we just grabbed 
    array_shift($array);
    // if what we have is an array spit it out, else return false
    return is_array($return) ? $return : false;
}

function make_query($connect) { 
    $query = "SELECT * FROM db.slider ORDER BY p_id ASC";
    $result = mysqli_query($connect, $query);
    return $result;
}

function make_slide_indicators($result) {
    $output = ''; 
    $count = 0;

    //for($i = 0;$i<mysqli_num_rows($result);$i++) {
    for($i = 0;$i<count($result);$i++) {
        if ($i == 0) {
            $output .= '<li data-target="#" data-slide-to="'.$i.'" class="active"></li>';
        } else {
            $output .= '<li data-target="#" data-slide-to="'.$i.'"></li>';
        }
        // $count++ === $count = $count + 1;
    }
    return $output;
}

function make_slides($result) {
    $output = '';
    // $count = 0;
    
    //while($row = mysqli_fetch_assoc($result)) {
    while($row = fetch_array($result)) {
        // Not needed as the output is the same
        //if($count == 0) { ... } else { ... }
        $output .= '
    <div class="swiper-slide platform">
        <img src="'.$row["p_img"].'" alt="'.$row["p_name"].'" />
        <div class="swiper-slide platform">
            <h3>'.$row["p_desc"].'</h3>
        </div>
    </div>';
        // Not used at the moment
        // $count++;
    }
    return $output;
}

//$connect = mysqli_connect("localhost", "root", "", "db");
//$result = make_query($connect);
$result = [['p_img' => 'img','p_name' => 'name','p_desc' => 'desc'],['p_img' => 'img','p_name' => 'name','p_desc' => 'desc'],['p_img' => 'img','p_name' => 'name','p_desc' => 'desc'],['p_img' => 'img','p_name' => 'name','p_desc' => 'desc']];

echo make_slide_indicators($result);
echo PHP_EOL;
echo make_slides($result);

代码中最大的不同是我们在 make_slide_indicators() 中使用了 mysqli_num_rows()。我们不需要从 mysql 结果中获取所有数据,我们只需要我们收到的行数。

这解决了另一个问题。如果您下次尝试使用 mysqli_fetch_assoc() 处理相同的结果时,您将只获得最后一个值。结果包含在读取一行时向前移动位置的内部指针。

如果您想要更多地查看结果,那么您需要使用 mysqli_data_seek()。看看这里: https ://www.php.net/manual/en/mysqli-result.data-seek.php

如果您按原样运行代码,您将看到逻辑正常工作。如果您在切换到数据库连接时仍然遇到问题,那么问题就在那里。这是为您提供的快速调试检查:

$connect = mysqli_connect("localhost", "root", "", "db");
$result = make_query($connect);

echo "Number of rows: ".mysqli_num_rows($result).PHP_EOL;

while($r = mysqli_fetch_assoc($result)) {
    var_dump($r);
}

推荐阅读