dongtan3395 2016-09-20 10:59
浏览 41

循环遍历所有查询结果行

I display results from the table 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>

And as you can see - the code is repeating itself but the row changes. I thought I could save whole lot of code and memory if I would use a loop that would be something like:

$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>

The issue is that I don't really know how to identify rows so I can iterate through them here:

$row[$rows]

and also here:

name='gender'

I need with each loop to change the name of a row like: $row['name'], $row['gender'] etc.

How can I transfer my code into a nice little loop? Any help?

  • 写回答

3条回答 默认 最新

  • doudou2121 2016-09-20 11:04
    关注

    mysqli_num_rows() returns only number of rows in result set, it does not return data.

    Use mysqli_fetch_row in while loop to iterate over all rows

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

    and you don't need any $rows variable. Use

    var_dump($row)
    

    to see the structure of returned data

    To loop over all columns in one row use

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

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line