foreach中的多个foreach只给出一个结果

时间:2014-10-25 23:16:25

标签: php loops mysqli foreach while-loop

我正在尝试从数据库中的结果构建表格布局中的html div。

我有sql表,其中包含行和sql表的位置,内容和&细胞的位置。 我的代码只给出了数据库的第一个结果...

rowId
rowPos
latestRow


cellsRowId
cellsRowOrder
cellsContent

$sql="SELECT `rowId` FROM `positions` ORDER BY `rowPos` ASC";

$result=mysqli_query($con,$sql);

 if (!mysqli_query($con,$sql)) {
     printf("Errormessage: %s\n", mysqli_error($con));}
  else{
   while ($pos=mysqli_fetch_assoc($result)){ 

    foreach($pos as $rowId){
        $sql="SELECT `cellsRowOrder` FROM `content` WHERE `cellsRowId` = '$rowId' ORDER BY `cellsRowOrder` ASC";
        $result=mysqli_query($con,$sql);

        if (!mysqli_query($con,$sql)) {
        printf("Errormessage: %s\n", mysqli_error($con));}
        else{

         while ($order=mysqli_fetch_assoc($result)){
          foreach($order as $cellRowOrder){
             $sql="SELECT `cellsContent` FROM `content` WHERE `cellsRowId` = '$rowId' AND `cellsRowOrder` = '$cellRowOrder'";
             $result=mysqli_query($con,$sql);

             if (!mysqli_query($con,$sql)) {
             printf("Errormessage: %s\n", mysqli_error($con));}
             else{

             while ($cellsContent=mysqli_fetch_assoc($result)){
             foreach($cellsContent as $cellContent){
                    echo '  
       <div class="content-layout-row" onmouseover="editButton'.$rowId.'()" onmouseout="editButtonClose'.$rowId.'()" id="r'.$rowId.'" >
        <div  id="editButton'.$rowId.'" style="visibility:hidden;width:100%;left:0;top:0px;z-index:2">
         <form method="POST" target="_top">
          <input type="hidden" name="rowId" value="'.$rowId.'"> 
          <input type="hidden" name="editRow" value="1">
          <button style="font-size:12px;border-radius:2px 5px 0px 0px" type="submit">edit</button>
         </form>
        </div>
        <div class="layout-cell" id="r'.$rowId.'s'.$cellRowOrder.'" >
         '.$cellContent.'
        </div>
       </div>
        <script type="text/javascript">
        function editButton'.$rowId.'(){
         $("#editButton'.$rowId.'").css("visibility","visible"); 
        }
        function editButtonClose'.$rowId.'(){
         $("#editButton'.$rowId.'").css("visibility","hidden"); 
        }
        </script> ';
                 }
               }
             }
           }
         }
       }
     }
   }
 };

1 个答案:

答案 0 :(得分:2)

$sql="SELECT `cellsRowOrder` FROM `content` WHERE `cellsRowId` = '$rowId' ORDER BY `cellsRowOrder` ASC";
$result=mysqli_query($con,$sql);

您正在重新定义已经使用您之前定义的$result作为条件的循环中途的$result

您需要做的就是将第二个$result重命名为其他内容,例如: $result2

你在这里再做同样的事情

$sql="SELECT `cellsContent` FROM `content` WHERE `cellsRowId` = '$rowId' AND `cellsRowOrder` = '$cellRowOrder'";
$result=mysqli_query($con,$sql);

不要忘记在所有适当的地方重命名它们,而不仅仅是这些行。