dpnvrt3119 2015-08-22 14:57
浏览 67
已采纳

在一个变量中使用Foreach循环生成多个查询结果

I'm working on a php project using codeigniter but I'm stuck on mysql query since the last 2 days without finding any solution. The problem is that I have 02 Mysql tables that I want to get results from using a variable from the url but I need to generate multiple results from the first query and get multiple results on the second query using the first query results, it's a little bit complicated because I can't find out a better way to describe the problem but here's the first table :

Table  

+---------------+-------------+------+-----+---------+
| Field         | Type        | Null | Key | Default |
+---------------+-------------+------+-----+---------+
| nApplication  | int(11)     | NO   | MUL | NULL    |
| nProduct      | int(11)     | NO   |     | 1       |
+---------------+-------------+------+-----+---------+

I've multiple nApplication values that have the same nProduct (the variable from the url) and I want to get all the row from this table :

Table : tapplication

+---------------+---------------------+------+-----+---------+----------------+
| Field         | Type                | Null | Key | Default | Extra          |
+---------------+---------------------+------+-----+---------+----------------+
| nApplication  | int(11)             | NO   | PRI | NULL    | auto_increment |
| nStatus       | tinyint(3) unsigned | NO   | MUL | 1       |                |
| nManufacturer | int(11)             | NO   | MUL | 1       |                |
| nSerie        | int(11)             | NO   | MUL | 1       |                |
| sType         | varchar(48)         | NO   |     | #       |                |
| sDate1        | varchar(6)          | NO   |     | #       |                |
| sDate2        | varchar(6)          | NO   |     | #       |                |
| sEngine       | varchar(48)         | NO   |     | #       |                |
| sKiloWatt     | varchar(6)          | NO   |     | #       |                |
| sHorsePower   | varchar(6)          | NO   |     | #       |                |
+---------------+---------------------+------+-----+---------+----------------+

That have the same nApplication from the previous query.

The Query I have so fare (Model)

function getApplication($product_id){
      $this->db->from('tapplicationproduct')
               ->where('nProduct',$product_id);
               $query = $this->db->get();
               foreach ($query->result() as $row) {
                $ret['napp'] = $row['nApplication'];
               }
      return $ret;
    }

I can't figure out to get how to get the Application results from the tapplication tables using the above query results

  • 写回答

2条回答 默认 最新

  • dongtan9518 2015-08-22 15:38
    关注

    Your problem seems to results in what you're storing and returning:

               foreach ($query->result() as $row) {
                $ret['napp'] = $row['nApplication'];
               }
    

    That will results in array with a single key napp and a single value of the last row nApplication in your return value.

    What you want is probably build an array of the data with the row?

               foreach ($query->result() as $row) {
                $ret[] = $row;
               }
    

    Or using nApplication value as key?

               foreach ($query->result() as $row) {
                $ret[$row['nApplication']] = $row;
               }
    

    Now looks like you want to have the data from the nApplication table as well. If you change the query to this you will have all the information in 1 query:

    function getApplication($product_id){
      $this->db->from('tapplicationproduct')
               ->join('nApplication','nApplication.nApplication = tapplicationproduct.nApplication')
               ->where('nProduct',$product_id);
               $query = $this->db->get();
               foreach ($query->result() as $row) {
                $ret[] = $row;
               }
      return $ret;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!