douyinjiao9351 2016-07-14 09:22
浏览 45
已采纳

通过连接2个变量创建索引变量 - php

I have a mysql query:

$analyse_ot1="SELECT COUNT(*) AS ot_count FROM first, base, users
WHERE base.base_id=$base 
AND
users.base_id=$base
AND
users.id=first.user_id
AND
first.$sub='OT'";

$result_ot1=mysqli_query($con,$analyse_ot1);
$row_ot1= mysqli_fetch_assoc($result_ot1);
$total_ot1= $row_ot1['ot_count'];
$otper1=($total_ot1/$total)*100;

I will be re-using this code many times on a web-page and want to be able to run it as part of a loop.

I don't want to have to rename my variables each time (where $result_ot1 become $result_ot2 etc...)

I've tried introducing an indexing variable $x and then appending it to another variable name:

$x=2;
$result_ot.$x=....

But , it doesn't want to work.

Any suggestions? I have an idea that arrays might be needed but I'm concerned that the queries will be throwing up arrays and I'm not sure an array in an array isn't going to set my computer on fire...

  • 写回答

1条回答 默认 最新

  • duanjing7459 2016-07-14 09:53
    关注

    A hint to use your query everywhere.

    //declare this in a folder, 'functions.php', for example
    function analyse($base, $sub, $con)
    {
        $analyse_ot1 = 'SELECT COUNT(*) AS ot_count FROM first, base, users'
            ." WHERE base.base_id=$base AND users.base_id=$base"
            ." AND users.id=first.user_id AND first.$sub='OT'";
        $result_ot1 = mysqli_query($con, $analyse_ot1);
        $row_ot1 = mysqli_fetch_assoc($result_ot1);
    
        return $row_ot1['ot_count'];
    }
    

    And in any other place of your project:

    include 'functions.php';
    
    $total_ot1 = analyse($base, $sub, $con);
    $otper1 = ($total_ot1 / $total) * 100;
    

    I did not understand well the loop thing. But in order to dave your results in an array, you could just do this:

    $totalCounts = [];
    
    foreach(loop){
        $total_current = analyse($base, $sub, $con);
        $totalCounts[] = $total_current;
    }
    

    And you will have $totalCounts that will be an array of the query result that we find in our function. That could be a way of saving your results in an array.

    You could do it associative also:

    $x = 0;
    foreach(loop){
        $total_current = analyse($base, $sub, $con);
        $totalCounts['count_'.$x] = $total_current;
        $x++;
    }
    

    So, if you now access: $totalCounts['count_3'] you would be accessing the result of the query on the third loop, for example.

    I don't know if this is your answer. But maybe it helped a bit.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败