2013-08-27 36 views
0

是否可以使用一個數組來保存mysql查詢的結果並通過增加數組索引計數器來顯示它?是否可以顯示一個mysql查詢的結果,而無需在php中循環?

我有這樣的代碼,允許從6的數輸入到11,並且生成 2表分割數均勻如果possible.It生成 遞增隊名(來自球隊1,TEAM2,...)。我嘗試使用循環 來顯示我的數據庫中真實的團隊名稱,但它也循環了 表的一代,並創建了很多tables.please幫助。

<?php 

     $count = count($result); 
     $team = $count; 
     $teamctr = 1; 

     if ($team > 5 && $team <= 11){ 
      $table = 2; 
      $tblctr = 1; 
      $r1ctr = 0; 
      $r2ctr = 0; 
      if ($team == 6){ 
       $row1 = 3; 
       $row2 = 3; 
      } 
      else if ($team == 7){ 
       $row1 = 3; 
       $row2 = 4; 
      } 
      else if ($team == 8){ 
       $row1 = 4; 
       $row2 = 4; 
      } 
      else if ($team == 9){ 
       $row1 = 4; 
       $row2 = 5; 
      } 
      else if ($team == 10){ 
       $row1 = 5; 
       $row2 = 5; 
      } 
      else if ($team == 11){ 
       $row1 = 5; 
       $row2 = 6; 
      } 
      while($tblctr <= $table){$i = 0; 
       echo "Number of Teams: ".$team; ?><br><?php 
       echo "Group Into: ".$table; ?><br><?php 

       ?> 
       <table border="1" align="center" width="30%"> 
        <tr> 
         <th width="80%" align="center">Group &nbsp <?php echo $tblctr; ?></th> 
         <th width="10%" align="center">W</th> 
         <th width="10%" align="center">L</th> 
        </tr> 
        <?php 
        if($tblctr = 1){ 
         while($r1ctr < $row1){ 
         ?> 
         <tr> 
          <td>Team &nbsp <?php echo $teamctr ?></td> 
          <td><input type="text" name="score" maxlength="6" size="6" /></td> 
          <td><input type="text" name="score" maxlength="6" size="6" /></td> 
         <?php 
         $r1ctr++; 
         $teamctr++; 
        } 
        echo ""; ?><br><?php 
        } 
        if($tblctr = 2){ 
         ?> 
       <table border="1" align="center" width="30%"> 
        <tr> 
         <th width="80%" align="center">Group &nbsp <?php echo $tblctr; ?></th> 
         <th width="10%" align="center">W</th> 
         <th width="10%" align="center">L</th> 
        </tr> 
        <?php 
         while($r2ctr < $row2){ 
         ?> 
         <tr> 
          <td>Team &nbsp <?php echo $teamctr ?></td> 
          <td><input type="text" name="score" maxlength="6" size="6" /></td> 
          <td><input type="text" name="score" maxlength="6" size="6" /></td> 
         <?php 
         $r2ctr++; 
         $teamctr++; 
        } 
        echo ""; ?><br><?php 
        } 
        else{ 
         echo "Done IF"; 
        } 

        $tblctr++; 
      } 
     } 

如何使用此查詢從我的數據庫中顯示團隊名稱?

$query = "SELECT * From tbl_teams"; 
     $result = mysql_query($query); 
     if($result) { 

    $i = 0; 

    while ($row = mysql_fetch_array($result)) { 
     $id[$i] = $row['team_name']; 
     echo $id[$i]; 
    } 
} 
+3

您的問題讓沒有意義。如果你增加了一個計數器,那麼你基本上使用一個循環ANYWAYS。你只是簡單地構建了你的循環。你不會把整個表放到循環中,只是實際的數據輸出。 「

」和「
」業務都在循環之外。 –

+0

不,不可能。另外,做$ i ++;在你的時候。 –

+0

我需要將

放在循環中,因爲我的代碼會生成兩個表。請嘗試我的代碼。對不起,我是一個新手在這個PHP – James

回答

0

在你的數據庫查詢循環,嘗試更改爲

$team_names = array(); 
while ($row = mysql_fetch_array($result)) { 
    $team_names[] = $row['team_name']; 
} 

然後在你的表的代碼,嘗試將其更改爲

$count = count($team_names); 
$team = $count; 
$teamctr = 0; 

<td>Team &nbsp <?php echo $team_names[$teamctr]; ?></td>