简体   繁体   English

如何在mysqli_fetch_array php中获取第一个值

[英]How to get the first value in mysqli_fetch_array php

I have this code where am able to perfectly get all values from the database using php mysqli_fetch_array with while loop我有这段代码,可以使用 php mysqli_fetch_array 和 while 循环完美地从数据库中获取所有值


    $pid = 1;                                             
    $sqlSI = "SELECT * FROM productcolor where proId='$pid'";
    $SIresult = mysqli_query($conn, $sqlSI);
        
    if (mysqli_num_rows($SIresult) == 1) {
        echo ' <h4>Color</h4>
        <div class="colors">';

        while ($row = mysqli_fetch_array($SIresult)) {
            ?>

        <a href="javascript:void(0)" class="s-colors" data-color="<?= $row['colors']; ?>" style="background-color: <?= $row['colors']; ?>;"></a>

        <script>
            $(document).ready(function () {
                $(".pdt-color-cart").val("<?= $row['colors']; ?>");
            })
        </script>

<?php
        }
        echo ' </div>';
    }
    else if (mysqli_num_rows($SIresult) > 1) {
        
        $rowed = mysqli_fetch_array($SIresult);
        echo $rowed['colors'];

        echo ' <h4>Color</h4>
        <div class="colors">';
        
        while ($row = mysqli_fetch_array($SIresult)) {
            
            ?>

        <a href="javascript:void(0)" class="s-colors" data-color="<?= $row['colors']; ?>" style="background-color: <?= $row['colors']; ?>;"></a>

<?php
        }
        echo ' </div>';
    } else {
        echo "";
    }
    

?>

In the code above I did one where if the value is 1 then do this else if it is more than 1 then still do this.在上面的代码中,我做了一个,如果值为1则执行此操作,否则如果它more than 1则仍然执行此操作。

But what I want is that while it is doing the > 1 loop, I want to be able to still grab one value out of it - especially the first value that will come out.但我想要的是,当它在执行> 1循环时,我希望仍然能够从中获取一个值 - 特别是第一个将出现的值。

How do you do it?你怎么做呢?

This task can be achieved easier if you tidy up your code a little bit.如果您稍微整理一下代码,则可以更轻松地完成此任务。 Instead of mixing the database logic with presentation logic, simply fetch the data before you start outputting your HTML/JavaScript.不要将数据库逻辑与表示逻辑混合在一起,只需在开始输出 HTML/JavaScript 之前获取数据。 You can store the number of elements in a variable.您可以将元素的数量存储在变量中。

// Fetch the data and store it in an array
$stmt = $conn->prepare('SELECT * FROM productcolor where proId=?');
$stmt->bind_param('s', $pid);
$stmt->execute();
$data = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$no_of_elements = count($data);

// If more than one element present in the results
if ($no_of_elements > 1) {
    echo $data[0]['colors'];
}

echo ' <h4>Color</h4>
<div class="colors">';
foreach ($data as $row) {
    echo <<<S_COLORS_BLOCK
    <a href="javascript:void(0)" class="s-colors" data-color="{$row['colors']}" style="background-color: {$row['colors']};"></a>
    S_COLORS_BLOCK;
}

// If only one element is present then display JavaScript
if ($no_of_elements === 1) {
    echo '
    <script>
        $(document).ready(function () {
            $(".pdt-color-cart").val('.json_encode($row['colors']).');
        })
    </script>';
}

If I understood you correctly, you want to "remember" what the first color that pops out is?如果我理解正确,您想“记住”弹出的第一个颜色是什么? In that case you could have a variable set before the first if, something like $saveColor = TRUE.在这种情况下,您可以在第一个 if 之前设置一个变量,例如 $saveColor = TRUE。 Then inside the elseif, check $saveColor, if TRUE, change it to FALSE and also set a 2nd variable to save the color, like然后在 elseif 中,检查 $saveColor,如果为 TRUE,则将其更改为 FALSE 并设置第二个变量以保存颜色,例如

$firstColorFound = $rowed['colors'];

or something like that.或类似的东西。

Next time it loops, $saveColor won't be TRUE anymore, and thus $firstColorFound won't be overwritten.下次循环时, $saveColor 将不再为 TRUE,因此 $firstColorFound 不会被覆盖。 Hope that helped!希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM