donglei1699 2012-01-04 01:58
浏览 80
已采纳

PHP变量for循环

hey guys is this even possible to be done in php

<?php   
$sectorcount = $row_SectorCount['COUNT(*)'];
//number of rows in database
for ($i = 1; $i <= $sectorcount; $i++)
{
${spendingname . $i}= array();
${spendingpercent . $i} = array();
${spendingid . $i} = array();

mysql_select_db($database_conn2, $conn2);
$query_Spending = "SELECT CONCAT(spending.SectorID, spending.ExpenditureID) AS 'SpendingID',
expenditure.ExpenditureName, spending.SpendingPercent, spending.SectorID
FROM spending   
INNER JOIN expenditure ON spending.ExpenditureID = expenditure.ExpenditureID
WHERE spending.SectorID = ".$i;
$Spending = mysql_query($query_Spending, $conn2) or die(mysql_error());
$totalRows_Spending = mysql_num_rows($Spending);
while($row_Spending = mysql_fetch_assoc($Spending))
{
${spendingname.$i}[] = $row_Spending['ExpenditureName'];
${spendingpercent.$i}[] = $row_Spending['SpendingPercent'];
${spendingid.$i}[]= $row_Spending['SpendingID'];
}
mysql_free_result($Spending);
}

i was planning to use this here in this context. having an array to draw out the values using a where clause and display them individually

  • 写回答

5条回答 默认 最新

  • drsqpko5286 2012-01-04 02:03
    关注

    This is what you are looking for:

    for ($i = 0; $i < 14; $i++) {
        ${'name' . $i} = $i;
    }
    
    echo $name3;
    

    But you should just use an array:

    $name = array();
    for ($i = 0; $i < 14; $i++) {
        $name[$i] = $i;
    }
    
    echo $name[3];
    

    If you need an array in an array, go ahead and do that too. If you need more nested arrays than that, you should probably be using a class somehow instead of all that array nesting.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划