2016-09-20 258 views
0

我顯示從上表users結果:遍歷所有查詢結果行

<div class='box-for-info'> 
      <span>Name:</span> 
      <div class='box-data-content'> 
      <span class='text-al-center'> 
       <?php if(!$row['name']==NULL){echo $row['name'];}else{echo '-';}?> 
      </span> 
      <input type='text' class='data-inputs' maxlength="30" name='name' value='<?php echo $row['name'];?>'/> 
      </div> 
     </div> 
     <div class='box-for-info'> 
      <span>Gender:</span> 
      <div class='box-data-content'> 
      <span class='text-al-center'> 
       <?php if(!$row['gender']==NULL){echo $row['gender'];}else{echo '-';}?> 
      </span> 
      <input type='text' class='data-inputs' maxlength="30" name='gender' value='<?php echo $row['gender'];?>'/> 
      </div> 
     </div> 
     <div class='box-for-info'> 
      <span>Age:</span> 
      <div class='box-data-content'> 
      <span class='text-al-center'> 
       <?php if(!$row['age']==NULL){ echo $row['age'];}else{echo '-';}?> 
      </span> 
      <input type='number' class='data-inputs' maxlength="30" name='age' value='<?php if(isset($age)){echo $age;}?>'/> 
      </div> 
     </div> 
     <div class='box-for-info'> 
      <span>Country:</span> 
      <div class='box-data-content'> 
      <span class='text-al-center'> 
       <?php if(!$row['country']==NULL){ echo $row['country'];}else{echo '-';}?> 
      </span> 
      <input type='text' class='data-inputs' maxlength="30" name='country' value='<?php echo $row['country'];?>'/> 
      </div> 
     </div> 
     <div class='box-for-info'> 
      <span>City/Town:</span> 
      <div class='box-data-content'> 
      <span class='text-al-center'> 
       <?php if(!$row['city']==NULL){echo $row['city']; }else{echo '-';}?> 
      </span> 
      <input type='text' class='data-inputs' maxlength="30" name='city' value='<?php echo $row['city'];?>'/> 
      </div> 
     </div> 
     <div class='box-for-info'> 
      <span>Native languages:</span> 
      <div class='box-data-content'> 
      <span class='text-al-center'> 
       <?php if(!$row['native_lang']==NULL){echo $row['native_lang'];}else{echo '-';}?> 
      </span> 
      <input type='text' class='data-inputs' maxlength="30" name='native_lang' value='<?php echo $row['native_lang'];?>'/> 
      </div> 
     </div> 
     <div class='box-for-info'> 
      <span>Learned languages:</span> 
      <div class='box-data-content'> 
      <span class='text-al-center'> 
       <?php if(!$row['learned_lang']==NULL){echo $row['learned_lang'];}else{echo '-';}?> 
      </span> 
      <input type='text' class='data-inputs' maxlength="30" name='learned_lang' value='<?php echo $row['learned_lang'];?>'/> 
      </div> 
     </div> 
     <div class='box-for-info'> 
      <span>Learning leanguages:</span> 
      <div class='box-data-content'> 
      <span class='text-al-center'> 
       <?php if(!$row['learning_lang']==NULL){echo $row['learning_lang'];}else{echo '-';}?> 
      </span> 
      <input type='text' class='data-inputs' maxlength="30" name='learning_lang' value='<?php echo $row['learning_lang'];?>'/> 
      </div> 
     </div> 
      <div class='box-for-info'> 
      <span>Most favourite language:</span> 
      <div class='box-data-content'> 
      <span class='text-al-center'> 
       <?php if(!$row['most_fav_lang']==NULL){echo $row['most_fav_lang'];}else{echo '-';}?> 
      </span> 
      <input type='text' class='data-inputs' maxlength="30" name='most_fav_lang' value='<?php echo $row['most_fav_lang'];?>'/> 
      </div> 
     </div> 
     <div class='box-for-info'> 
      <span>Best method to learn a language:</span> 
      <div class='box-data-content'> 
      <span class='text-al-center'> 
       <?php if(!$row['best_method_to_learn']==NULL){ echo $row['best_method_to_learn'];}else{echo '-';}?> 
      </span> 
      <textarea class='textar' maxlength="2000" name='best_method_to_learn'><?php echo $row['best_method_to_learn'];?></textarea> 
      </div> 
     </div> 
     <div class='box-for-info'> 
      <span>Why do you learn languages:</span> 
      <div class='box-data-content'> 
       <span class='text-al-center'> 
       <?php if(!$row['why_do_you_learn'] == NULL){ echo $row['why_do_you_learn'];}else{echo '-';}?> 
      </span> 
      <textarea class='textar' maxlength="2000" name='why_do_you_learn'><?php echo $row['why_do_you_learn'];?> 
      </textarea> 
      </div> 
     </div> 
      <div class='box-for-info'> 
      <span>Your most favourite way to practice a language:</span> 
      <div class='box-data-content'> 
      <span class='text-al-center'> 
       <?php if(!$row['your_most_fav_way'] == NULL){echo $row['your_most_fav_way'];}else{echo '-';}?> 
      </span> 
      <textarea class='textar' maxlength="2000" name='your_most_fav_way'><?php echo $row['your_most_fav_way']; ?> 
      </textarea> 
      </div> 
     </div> 
      <div class='box-for-info'> 
      <span>If the world would speak one language, it would be:</span> 
      <div class='box-data-content'> 
      <span class='text-al-center'> 
       <?php if(!$row['one_lang_in_the_world']==NULL){ echo $row['one_lang_in_the_world'];}else{echo '-';}?> 
      </span> 
      <textarea class='textar' maxlength="2000" name='one_lang_in_the_world'><?php echo $row['one_lang_in_the_world']; ?> 
      </textarea> 
      </div> 
     </div> 

正如你所看到的 - 代碼正在重演,但該行的變化。我以爲我可以拯救整個大量的代碼和內存,如果我會用一個循環,會是這樣的:

$row = mysqli_num_rows($result); 
while($rows){?> 
<div class='box-for-info'> 
    <span>Name:</span> 
    <div class='box-data-content'> 
     <span class='text-al-center'> 
     <?php if(!$row[$rows]==NULL){echo $row['name'];}else{echo '-';}?> 
     </span> 
     <input type='text' class='data-inputs' maxlength="30" name='name' value='<?php echo $row[$rows];?>'/> 
     </div> 
    </div> 

的問題是,我真的不知道如何識別行,所以我可以遍歷它們在這裏:

$row[$rows] 

,並在這裏:

name='gender' 

我需要每一個循環像更改行的名稱:$row['name']$row['gender']

如何將我的代碼轉移到一個漂亮的小循環中?任何幫助?

+0

也許我解釋了問題不完整 - 但我不需要循環行!行結果可以是一個!只有一個$ row ['name'],$ row ['age']等等。Theres不需要遍歷所有$ row ['age'],因爲只有一個!相反,我需要通過sme列的不同列循環!因此每次寫入都是這樣的:$ row ['age'],$ row ['country']我想做一些類似於:$ row [$ i]的東西,所以我需要排列數組中相同行的列,只是陳列指數;我怎麼做? – Donatta

+0

爲什麼不使用** foreach($ row作爲$ key => $ value){var_dump($ value);} ** –

回答

0

mysqli_num_rows()僅返回結果集中的行數,它不返回數據。

使用mysqli_fetch_row在while循環遍歷所有行

while($row = mysqli_fetch_row($result)){ 

,你不需要任何$行變量。使用

var_dump($row) 

看到返回的數據

要循環的結構在所有列在一排用

foreach($row as $key => $value) { var_dump($value);} 
0

比較典型的做法是這樣的:

while($row = mysqli_fetch_assoc($result)){ ?> 
    ...stuff with $row["columnname"]... 
<?php } 

您基本上會爲每行數據獲得一個新的關聯數組$ row,並且在數據用完時它會自動終止。除非需要創建計數器,否則無需引用索引變量。

或者,也許你可以試試:

while($row = mysqli_fetch_assoc($result)) { 
    foreach($row as $colname=>$coldata) { 
     echo "<p>"; 
     echo htmlentities($colname); 
     echo " is "; 
     echo htmlentities($coldata); 
     echo "</p>"; 
    } 
} 

...這將通過列名通過迭代列的你。顯然在你的真實輸出中使用$ colname和$ coldata - 這只是說明。注意使用htmlentities(),它確保一個任意小於號的符號不會弄亂你的html(並有助於防止XSS問題)。

+0

$ row [「columnname」]?但是如果它隨着每個循環而變化,我應該如何引用一行? – Donatta

+0

在循環的頂部,您將逐字地獲取整行數據到$ row中,因此您可以執行if(!$ row ['one_lang_in_the_world'] == NULL)之類的操作,並且只要您使用查詢結果中指定的列。不要反套 - 給它一個鏡頭。 –

+0

呃。等待。你的模式是什麼樣的?每個盒子是否在一行而不是一列? –

1
This will helpful only replace the array to db array 

    <?php 
$arr=array('test1','test2','test3'); 
$row = count($arr); 
foreach($arr as $key=>$value){?> 
<div class='box-for-info'> 
    <span>Name:</span> 
     <div class='box-data-content'> 
      <span class='text-al-center'> 
      <?php if(!$key==NULL){echo $value;}else{echo '-';}?> 
      </span> 
      <input type='text' class='data-inputs' maxlength="30" name='name' value='<?php echo $value;?>'/> 
      </div> 
     </div> 
    <?php } ?> 
+0

它工作,但我不能{echo $ value;}這部分回顯一行的值! – Donatta

+0

所以在yur代碼的情況下,我需要以某種方式(哦,上帝)連接兩個數組(第一個:查詢 - 數組本身和第二個= $ arr數組)在獲取一行的值... – Donatta

+0

使用msql_fetch_array( )它將返回鍵值對中的關聯數組。 –