简体   繁体   English

在代码中多个地方使用mysql查询结果

[英]Use mysql query result in more than one place in code

I'm building a system that tracks contact lenses. 我正在建立一个跟踪隐形眼镜的系统。 I'm storing the contact lens info in a database as sometimes prices/availabilities change and i access this info from multiple points in the program. 我将隐形眼镜信息存储在数据库中,因为有时价格/可用性会发生变化,而且我会从程序的多个位置访问此信息。 I'm trying to interface with this list using a dropdown by doing "SELECT * FROM contacts" as a query. 我正在尝试使用“选择*来自联系人”作为查询,使用下拉列表与此列表进行交互。 my code looks like this : 我的代码如下所示:

$contact_list = mysqli_query($link, "SELECT brand FROM contacts ORDER BY brand");

Then I echo that list out in a while loop using PHP to populate the options in the dropdown. 然后,我使用PHP在while循环中回显该列表,以填充下拉列表中的选项。

My question is this: I have these dropdowns for each eye on the same form. 我的问题是:在同一表格上,每只眼睛都有这些下拉菜单。 So it's "Brand Right Eye"....other miscellaneous info about the right eye....then "Brand Left Eye". 因此,它是“品牌右眼”。...有关右眼的其他信息。...然后是“品牌左眼”。 But ONLY the right eye is populating with the brand info because it appears first in the code. 但是只有右眼可以看到品牌信息,因为它首先出现在代码中。 What i'm having to do is copy/paste the exact same query and do 我要做的是复制/粘贴完全相同的查询并执行

$contact_list2 = mysqli_query($link, "SELECT brand FROM contacts ORDER BY brand");

then later if I need the dropdown again, I need to do $contact_list3..and so on. 然后,如果我再次需要该下拉菜单,则需要执行$ contact_list3 ..依此类推。 Why can i not generate a drop down using the same variable? 为什么我不能使用相同的变量生成下拉列表? Why does it stop responding to calling the variable after the first execution of it and is there any work around that I can implement that would allow me to not have to copy/paste the same query with a different variable association each time? 为什么它在第一次执行变量后就停止对调用变量的响应,并且我可以实现任何解决方法,使我不必每次都使用不同的变量关联来复制/粘贴相同的查询?

just for refernce, my php while code is this: 仅供参考,我的php代码如下:

 <select class="form-control" name = "brandOS"> 
    <option value="0">Please Select</option>
        <?php
            while($row = mysqli_fetch_array($contact_list))
            {
            ?>
            <option value = "<?php echo($row['brand'])?>" name = "brandOS">
                <?php echo($row['brand']) ?>
            </option>
            <?php
            }               
        ?>
    </select>

I have this loop copy/pasted for right eye and left eye. 我将此循环复制/粘贴为右眼和左眼。 But it only works on which ever drop down appears first in the code. 但它仅适用于代码中首先出现下拉菜单的情况。

A possible solution will be more efficient in term of performance could be : 一个可能的解决方案在性能方面可能会更加高效:

<?php
    $left_eye = '<option value="0">Please Select</option>';
    $rigth_eye = '<option value="0">Please Select</option>';

    while($row = mysqli_fetch_array($contact_list))
    {
        //logic for left eye
        $left_eye .= <<<HTML
        <option value ="{$row['brand']}" name = "brandOS">
            {$row['brand']}
        </option>
HTML;

        //logic for rigth eye
        $rigth_eye .= <<<HTML
        <option value ="{$row['brand']}" name = "brandOS">
            {$row['brand']}
        </option>
HTML;
    }       
?>



<select class="form-control" name = "brandOS"> 
  <?php echo $left_eye ; ?>
</select>


<select class="form-control" name = "brandOS"> 
  <?php echo $rigth_eye ; ?>
</select>

With this solution you get your result in the same while loop. 使用此解决方案,您可以在相同的while循环中获得结果。 If the left and right select are the same you can use the same variable. 如果左右选择相同,则可以使用相同的变量。

Store the brands in an array, then you can just loop through the array. 将品牌存储在阵列中,然后就可以遍历阵列。

<?php

    $contact_list = mysqli_query($link, "SELECT brand FROM contacts ORDER BY brand");

    $brands = array();

    while($row = mysqli_fetch_array($contact_list))
    {
        array_push($brands, $row['brand']);
    }

?>
<select class="form-control" name = "brandOS"> 
    <option value="0">Please Select</option>
    <?php
        foreach($brands as $brand){
            ?>

                <option value = "<?php echo($brand[0])?>" name = "brandOS">
                    <?php echo($brand[0]) ?>
                </option>

            <?php
        }

    ?>
</select>

You can use a PHP array, like the SESSION one, to store values and use them across your site. 您可以使用PHP数组(如SESSION数组)来存储值,并在整个站点中使用它们。 Be sure you call "session_start()" method on each page you use that array, though. 但是,请确保在使用该数组的每个页面上调用“ session_start()”方法。

//Initialize sessions
session_start();

...

//Right after getting result from query
$_SESSION['contact_list'] = $contact_list;

To use it, just be sure to call the method I told you above, and call the variable with the same syntax: 要使用它,只需确保调用我上面告诉您的方法,然后使用相同的语法调用该变量:

<?php 
    while($row = mysqli_fetch_array($_SESSION['contact_list'])) { ?>

Hope this helps. 希望这可以帮助。

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

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