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 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值