简体   繁体   English

如何使用数组访问循环外的whileloop记录

[英]How to access whileloop record outside the loop using array

Hello i selected all records from users and returned them using a php whileloop and array, using implode i could get all the records outside the loop, what i wish to do actually is to access individual record and assign them to a variable individually to be used later in the same page.您好,我从用户那里选择了所有记录并使用 php whileloop 和数组返回它们,使用 implode 我可以获取循环外的所有记录,我实际上想要做的是访问单个记录并将它们分配给单独的变量以供使用稍后在同一页面中。

this is what i came up with already, it works, but i don't know how to assign the recorded to individual variables, since all the records are displayed using implode这是我已经想出的,它有效,但我不知道如何将记录分配给各个变量,因为所有记录都是使用 implode 显示的

       `<?php
$data = array();
            $query = "SELECT * FROM users ";
            $queryResult = mysqli_query($conn,$query);
            while($_row = mysqli_fetch_array($queryResult)){
              $data[] = $_row['first_name'];
            $data[] = $_row['email'];
            $data[]  = $_row['amount'];
               
                ?>
                <?php }?>
                <?php
                echo "<br />";
               $request = implode("<br />", $data);
              
               echo $request;?>`

please i need assistance on how to achieve or a better way to access whileloop records outside the loop this thanks in advance请我需要有关如何实现或更好的方法来访问循环外的whileloop记录的帮助,这在此先感谢

This is what i intended doing with that result of the loop in the next query这就是我打算在下一个查询中对循环结果做的事情

`<?php

$profit = 10;
$query = "UPDATE storage SET ";
$query .="store_profit = '{$profit}' ";
$query .= "WHERE email = '{$email_from_loop_above}' ";?>`

for clarity purposes.. this script will be executed by a cronjob every 1 minute, initially this is what is did..为清楚起见..此脚本将由 cronjob 每 1 分钟执行一次,最初是这样做的..

            `
<?php
$query = "SELECT * FROM users WHERE email = $_SESSION['email']  ";
            $queryResult = mysqli_query($conn,$query);
            while($_row = mysqli_fetch_array($queryResult)){
              $first_name = $_row['first_name'];
           $email_from_loop_above = $_row['email'];
            $amount  = $_row['amount'];
               
                ?>
                <?php }?>`

Then for the update query this is what i did然后对于更新查询,这就是我所做的

    `<?php
//The profit below was gotten from a percent from the amount i return from the loop above.
    $profit = 10;
    $query = "UPDATE storage SET ";
    $query .="store_profit = '{$profit}' ";
    $query .= "WHERE email = '{$_SESSION['email']}' ";
    ?>`

Now this code above works perfectly, but when the user logout out the store_profit would not update anymore simply because this you know would require an active user SESSION to work, so what i am trying to do now is is make it work accordingly even when the user is logged out of the account, if i use the following method现在上面的代码可以完美运行,但是当用户注销时,store_profit 将不再更新,因为您知道这需要一个活动用户 SESSION 才能工作,所以我现在要做的是让它相应地工作,即使当用户退出帐户,如果我使用以下方法

 `<?php
$query = "SELECT * FROM users ";
            $queryResult = mysqli_query($conn,$query);
            while($_row = mysqli_fetch_array($queryResult)){
              $first_name = $_row['first_name'];
            $email_from_loop = $_row['email'];
            $amount  = $_row['amount'];
               
                ?>
                <?php }?>`
    
    
    `
    <?php
    $profit = 10;
    $query = "UPDATE storage SET ";
    $query .="store_profit = '{$profit}' ";
    $query .= "WHERE email = '{$email_from_loop}' ";
    ?>`

it would only return the last user data only, simply because i am outside the loop, now when i try to update inside the loop, it works but it adds 10 to store_profit to the first user and adds 20 to the second user also adds 30 to third user... simply because it inside the loop它只会返回最后一个用户数据,只是因为我在循环之外,现在当我尝试在循环内更新时,它可以工作,但它会将 10 添加到第一个用户的 store_profit 并添加 20 到第二个用户也添加 30给第三个用户......仅仅因为它在循环内

now what is want to do is make this work accordingly so i decided to use array, however i am not to familiar with array that why i became confused.现在要做的是使这项工作相应地进行,所以我决定使用数组,但是我不熟悉数组,这就是我感到困惑的原因。 An explanation of how i can achieve this purpose would be very much appreciated thanks.非常感谢我如何实现这一目的的解释。 however i was thinking of not destroying all the user session maybe i create session for that purpose and not distroy it.. but i don't if that method would be safe or good.但是我想不破坏所有用户 session 也许我为此目的创建 session 而不是破坏它..但我不知道该方法是否安全或良好。

As a result of your loop you have a $data array which you then turn into a string by gluing all its elements using implode function.作为循环的结果,您有一个$data数组,然后您通过使用implode function 粘合其所有元素将其变成一个字符串。

You can of course still access every individual element of $data array outside of the loop by addressing to its direct index.当然,您仍然可以通过寻址到其直接索引来访问循环外$data数组的每个单独元素。

For instance:例如:

echo $data[0]['email'];

will output the email record of first (arrays are 0 based in PHP) element of your array.将 output email 记录您的数组的第一个(数组在 PHP 中为 0)元素。

You can take a look at below code你可以看看下面的代码

    $data = array();
    $query = "SELECT * FROM users ";
    $qeuryResult = mysqli_query($conn,$query);
    while($_row = mysqli_fetch_array($qeuryResult)){
    array_push($data, array(
                            'first_name' => $_row['first_name'],
                            'email' => $_row['email'],
                            'amount' => $_row['amount'],
        ));
       
        }
        //print_r($data[0]['first_name']);
        echo "<pre>";
        print_r($data);
        

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

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