如何使用codeigniter循环从mysql检索数据

时间:2022-12-04 16:33:42

I have a problem wherein i will retrieve several datas in my sql, for example i have 9 datas but my problem is that inside my foreach the data that will be process and the foreach is placed in my controller.

我有一个问题,我将在我的SQL中检索几个数据,例如我有9个数据,但我的问题是在我的foreach内部将处理的数据和foreach放在我的控制器中。

here's my controller:

这是我的控制器:

function getPhaseData() {
    try 
    {
        $this->SessionCheck();
        $this->user->initialize($this->session->userdata('userid'));

        $this->load->model('project_model', 'Project');
        $ProjectID = $this->input->post('ProjectID');

        /***************** Intialize Project model ******************/
        $this->Project->Initialize($ProjectID);
        $Options = Work_breakdown_structure::$WithBaseTaskID;
        //$PhaseTaskID = (int)$this->input->get_post('TaskID',TRUE);


        //$PhaseTaskID = $this->Project->getPhaseBaseTaskID($ProjectID);

        $postlist->phaseList = $this->Project->LatestApplicablePlan->WBS->GetPhaseList($Options);
        $PhaseList = $this->Project->LatestApplicablePlan->WBS->GetPhaseList($Options);

    foreach($PhaseList as $row) 
    {
            $postlist->taskList = $this->Project->LatestApplicablePlan->WBS->GetWBS($Iterate['TaskID'], $Options);
    }


        /*if($PhaseTaskID == null)
        { }
        else
        {
            foreach($PhaseTaskID as $index=>$value)
            {
                $finalArr[$value['baseTaskID']] = $value['baseTaskID'];

                //echo $finalArr[$value['baseTaskID']].' ';
                    $postlist->taskList = $this->Project->LatestApplicablePlan->WBS->GetWBS($finalArr[$value['baseTaskID']], $Options);
            }
        }*/


        $postlist->project = $ProjectID;
        return $this->load->view('MyToDoPhaseDropdown', $postlist);
    }

here's my view where i will display the data get from the controller in my select dropdown.

这是我的视图,我将在我的选择下拉列表中显示来自控制器的数据。

if($project == 0) {
    echo '<td style="padding-top:5x;font-size:14px;" colspan="2"> <br> Phases : ';

        echo '<select disabled id="phases_select" style="width:400px;" onchange="search_filter()" >';

        echo '<option value="0" selected="selected"> Select Project Phase </option>';

 echo '</select>';
 echo '</td>';
    }
else
{
    foreach($taskList as $iterate) 
    {
        echo ' TaskID: '. ' '.$iterate['TaskID'] .' -- TaskName: '. $iterate['TaskName'].'<br>' ;
    }

    /*foreach($phaseList as $row) {
        echo 'TaskID: '. $row['TaskID'].' '.$row['TaskName'].'<br>';
        if(preg_match("/^CYCLE/", strtoupper($row['TaskName'])))
        {
            foreach($row['Child'] as $child) {
                echo 'TaskID: '. $child['TaskID'].' '.$child['TaskName'].'<br>';
            }
        }
    }*/

    echo '<td style="padding-top:5x;font-size:14px;" colspan="2"> <br> Phases : ';
        echo '<select id="phases_select" style="width:400px;" onchange="search_filter()" >';
            echo '<option value="0" selected="selected"> Select Project Phase </option>';
                foreach($phaseList as $row) 
                {
                    if(preg_match("/^CYCLE/", strtoupper($row['TaskName'])))
                    {
                        foreach($row['Child'] as $child)
                        {
                            if($Iterate['BaseTaskID'] != $child['TaskID']) 
                            {
                                echo '<option value="'. $child['TaskID']. '">';
                                echo $row['TaskName'].' > '.$child['TaskName'].  '</option>';
                            }

                            foreach($taskList as $Iterate) 
                            {
                                if($child['TaskID'] == $Iterate['BaseTaskID']) 
                                {
                                    echo '<option value="'. $Iterate['TaskID']. '">';
                                    echo $row['TaskName'].' > '.$child['TaskName'].' '.$Iterate['IterationNumber']. '</option>';
                                }
                            }
                        }
                    }

                    else
                    {
                        if($Iterate['BaseTaskID'] != $row['TaskID'])
                        {
                            echo '<option value="'. $row['TaskID']. '">';
                            echo $row['TaskName'].'</option>';
                        }

                        foreach($taskList as $Iterate) 
                        {
                            if($row['TaskID'] == $Iterate['BaseTaskID'] ) 
                            {
                                echo '<option value="'. $Iterate['TaskID']. '">';
                                echo $row['TaskName'].' '.$Iterate['IterationNumber']. '</option>';
                            }
                        }
                    }
                }
        echo '</select>';
    echo '</td>';
}

1 个解决方案

#1


This is not on exact answer but this will help to understand MVC structure in codeigniter.

这不是确切的答案,但这将有助于理解codeigniter中的MVC结构。

model

  function get_city_list(){
  $this->db->select('city_id, name');
  $this->db->where('status', 1);
  $this->db->order_by("name", "asc"); 
  $this->db->from('city'); 
  $query = $this->db->get();
  $result = $query->result();
  return $result;
  }

Controller

    function listing(){
    $data['city'] =  $this->home->get_city_list();
    $this->load->view('property_listing', $data);
    }

view

    foreach($city as $cty){
        echo $cty->name;
        echo $cty->city_id;
    }

Hope this helps you..

希望这可以帮助你..

#1


This is not on exact answer but this will help to understand MVC structure in codeigniter.

这不是确切的答案,但这将有助于理解codeigniter中的MVC结构。

model

  function get_city_list(){
  $this->db->select('city_id, name');
  $this->db->where('status', 1);
  $this->db->order_by("name", "asc"); 
  $this->db->from('city'); 
  $query = $this->db->get();
  $result = $query->result();
  return $result;
  }

Controller

    function listing(){
    $data['city'] =  $this->home->get_city_list();
    $this->load->view('property_listing', $data);
    }

view

    foreach($city as $cty){
        echo $cty->name;
        echo $cty->city_id;
    }

Hope this helps you..

希望这可以帮助你..