dongsi1944 2014-01-26 16:55
浏览 44
已采纳

将mysql_query结果传递给foreach

I used the following code to to get list of users facebook friends and ccheck it against users in an app database. This code would return the users of the app, who are Facebook friends of the user.

     $friends_set = '(';
             foreach($friends["data"] as $value) {
                 $friends_set .= $value['id'].','; 
             }
             $new_set = preg_replace('/,$/',')',$friends_set);
               $res = mysql_query("SELECT * from user AS u, upload AS up WHERE u.fb_id IN $new_set AND u.fb_id=up.user_id") or die(mysql_error());

    while($row = mysql_fetch_array($res)) {

        echo $row['fb_id']. "". $row['first_name'];
        echo "<br>"; 

      }

$data['top_friends']=$res;
$this->load->view('myview');

This code works. It is in a controller of my codeigniter application and it successfully echos the correct data onto the page.

However now I need to print the result of the query in a for each statement in my view like this:

<?php foreach ($top_friends as $me) : ?>

                    <div >

                    <p><?php echo $me['first_name']; ?></p>

                     <?php endforeach; ?>

However when I try getting the query results in the view using the for each it doesn't work.

How do i fix this?

  • 写回答

1条回答 默认 最新

  • duanshang3230 2014-01-26 17:14
    关注

    You could try it the codeignitor way, Create a model function say get_top_friends and i assume that you are passing a comma separated string as argument like $fb_id = '45,65,78,89'. Say facebook_model is the name of the model then :

    class Facebook_model extends CI_Model{
         //other functions and constrcutor here
    
        //function to get the top friends
         function get_top_friends($fb_id){
           $fbId = explode(",",$fb_id)
           $this->db->select('*');
           $this->db->where_in('fb_id',$fbId);
           $this->db->order_by('points','desc');
           $this->db->limit(10);
           $query = $this->db->get('user');
           if ($query->num_rows() < 1) return FALSE;
           return $query->result_array();
         }
    }
    

    And make change in your code as below:

     $friends_set = '';
         foreach($friends["data"] as $value) {
             $friends_set .= $value['id'].','; 
         }
         $new_set = preg_replace('/,$/',')',$friends_set);
         $res = $this->facebook_model->get_top_friends($new_set);
         $data['top_friends']=$res;
         $this->load->view('myview',$data);
    

    And now in view you can try

    foreach ($top_friends as $me){
      echo $me['first_name'];
    }
    

    [Updated for user ]

    If you want to do it as in your question : then try,

    $result = array();
    while($row = mysql_fetch_array($res)) {
            $result[] = $row;  
    }
    $data['top_friends']=$result;
    $this->load->view('myview',$data);//pass data to view
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法