0

I am creating a simple form with fields like First_name, Last_name, city etc.So for city field, I want to display dynamic data.Below is the code I am using it's in PHP CodeIgniter.

Controller Page:

public function city()
    {
        $this->load->model('dropdownM');
        $getcity=$this->dropdownM->get_city();
        $this->load->view('form1city',$getcity);
    }

Model Page:

<?php

class DropdownM extends CI_Model
{
    public function get_city()
    {


        $this->db->select('fname');
        $this->db->from('city');
        $query = $this->db->get();
        if($query->num_rows()>0)
            {
            return $query->result();

        }
    }
}

View page:

    <form action="<?php echo base_url(); ?>index.php/Rec/user" method="post">

    <select class="form-control" id="city" name="city">

    <option value="">Select </option>
        <?php if(count($getcity)):?>

            <?php foreach($getcity as $city):?>

                <option value=<?php echo $city->c_id;?>><?php echo $village1->C_name;?></option>

            <?php endforeach;?>

        <?php else:?>

       <?php endif;?>

    </select>
       <center>                                            
       <input type="submit" value="Submit" class="btn btn-bricky" id="subbtn" 
        name="submit1">
       </center>
<form>

It's not displaying anything in the drop-down.I am not able to find out what the issue.

1 Answer 1

3

pass data like this

$data['getcity']=$this->dropdownM->get_city();
$this->load->view('form1city',$data);

and in view

<?php if(count($getcity) > 0):?>
    <select class="form-control" id="city" name="city">
        <option value="">Select </option>
            <?php foreach($getcity as $city):?>

                <option value=<?php echo $city['c_id'];?>><?php echo $village1['C_name'];?></option>

            <?php endforeach;?>
    </select>
<?php else:?>
    <p>No Category Found</p>
<?php endif;?>

In model

$this->db->select('fname');
$this->db->from('city');
$query = $this->db->get();
return $query->result_array();
Sign up to request clarification or add additional context in comments.

8 Comments

I am getting below error:A PHP Error was encountered Severity: Notice Message: Undefined variable: getcity
at which line ?
At this line:<?php if(count($getcity) > 0):?>
<?php if(!empty($getcity)):?> replace to this
This is completely correct and addresses all the issues I see. Use this code verbatim.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.