循环遍历所有查询结果行

时间:2016-09-20 10:59:12

标签: php loops

我显示表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']等。

如何将我的代码转换为一个漂亮的小循环?有什么帮助吗?

3 个答案:

答案 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 } ?>

答案 1 :(得分:0)

mysqli_num_rows()仅返回结果集中的行数,但不返回数据。

在while循环中使用mysqli_fetch_row迭代所有行

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

并且您不需要任何$ rows变量。使用

var_dump($row)

查看返回数据的结构

要遍历一行中的所有列,请使用

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

答案 2 :(得分: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问题)。