doudao8283 2013-05-10 22:01
浏览 580
已采纳

在MYSQL查询中使用变量作为列名

I made a table for appointments that works as a form, it shows whether a time slot is free or not and you can reserve appointments from it. This is the code that works ..

$query=mysql_query("SELECT * FROM days "); 
echo " <form action=\"change.php\" method=\"post\"><TABLE>";
echo "<TR><Th> Dia <Th> 8 <Th> 9 <Th> 10 <Th> 11<Th>12 <Th> 13 <Th> 14 <Th> 15 <Th> 16       ";

while ($rows=mysql_fetch_array($query)) { 
    if ($rows['08'] == 'taken') {
        $rows['08'] = "<TD id=\"red\" >TAKEN";
    } 
    else {
        $rows['08'] = "<TD id=\"blue\" ><input type=\"radio\" name=\"slot\" value= \"08 $rows[day]\" >$rows[08]";
    }

    if ($rows['09']=='taken') {
        $rows['09']="<TD  id=\"red\" >TAKEN";
    }
    else {
        $rows['09']="<TD  id=\"blue\" ><input type=\"radio\" name=\"slot\" value= \"09 $rows[day]\" >$rows[09]";
    }

    if ($rows['10']=='taken') {
        $rows['10']="<TD  id=\"red\" >TAKEN";
    }

the code goes further to cover all timeslots .. this is an example of how the table looks like .. well ..with the free cells having a radio botton next to them:

           day   08      09     10     11     12     13     14     15      16 
    2013-05-05  free   free   free   taken   free   free   taken   free   free

what i'm trying to do is to automate this . as in creating a loop so i dont have to repeat the same code so many times .. i tried the following but it didnt work:

while ($rows=mysql_fetch_array($query)) {
    $c="08"; 
    if ($rows["$c"]=='taken') {
        $rows["$c"]='taken';
    } 

    if ($rows["$c"]=='free') {
        $rows[$c] = "<input type=\"radio\" name=\"slot\" value= \"$c. $rows[day]\" >$rows[$c]";
    }

    echo "<TR><TD>$rows[day]"; echo "<TD> $rows[$c]"; $c++;
    echo "<TD> $rows[$c]"; $c++;
    echo "<TD> $rows[$c]"; $c++;
    echo "<TD> $rows[$c]"; $c++;

.....and so on ..

I'm thinking the problem is in the way to put the variable inside the row array any suggestions?

  • 写回答

1条回答 默认 最新

  • duanluanlang8501 2013-05-10 22:12
    关注
    $query=mysql_query("SELECT * FROM days "); 
    echo "<form action='change.php' method='post'><table>";
    echo "<tr><td>day</td><td>8</td><td>9</td><td>10</td><td>11</td><td>12</td><td>13</td><td>14</td><td>15</td><td>16</td></tr>";
    
    while ($rows=mysql_fetch_array($query)) { 
    
        while (  $rows=mysql_fetch_assoc($query) ) {
    
            $res[]="<tr><td>".$rows[day];
    
            for ($c="8";$c<="16";$c++)
            {
    
                if ($c<10){$ca="0".$c;} else {$ca=$c;}
    
                if ($rows[$ca]=='taken') {
                    $res[]='taken';
                } 
                if ($rows[$ca]=='free') {
                    $res[] = "<input type='radio' name='slot' value='".$c." ".$rows[day]." >".$rows[$ca];
                }
            }
            $res[]="</td></tr>";
            echo implode("</td><td>",$res);
        }
        echo "</table></form>"; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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