Welcome Guest, Not a member yet? Register   Sign In
how to fetch data from database automatically on first load
#1

[eluser]ranjitbd[/eluser]
i can retrieve data from database when any event triggers like click on button or a link.

but in my index page a search criteria box is there where 4 combo box is exist for user search option.

i have to fetch data from database when the page is loaded. earlier i have done this with flat php.
the code is showing below:

//city selection box

Code:
<select name="city" id="city">
     <option value="0">select city</option>
          &lt;?php
        $sql= "select * from city_info" ;
        $result = mysql_query($sql, $db);
        while($row = mysql_fetch_array($result))
            {?&gt;
                          <option value="&lt;?php echo $row['CITY_CODE'];?&gt;">&lt;?php echo $row['CITY_NAME'];?&gt;</option>
                   &lt;?php } ?&gt;
</select>

but now what to do with CI
#2

[eluser]alboyd[/eluser]
I could help you but it seems you don't even acknowledge where you have been helped. EG That other thread where you seem more interested in having some stupid argument about using code blocks.
#3

[eluser]ranjitbd[/eluser]
i told you sir that i am new to CI and i dnt knw english well
so plz help me.
thanks in advance.
#4

[eluser]alboyd[/eluser]
In your controller you need to make a call to your model to retrieve the selection values - recommend grab an ID and a display value.

In your controller you should save that query result to a variable to pass into the view.

In your view you should iterate through the query results and do as you have already done and create the html for your select dropdown.

EDIT:

Once your query results are in an array available to your view it could look something like this:
Code:
<select name="city" id="city">
     <option value="0">select city</option>
        &lt;?php
        foreach($city_query->result() as $city) { ?&gt;
            <option value="&lt;?php echo $city->id;?&gt;">&lt;?php echo $city->city_name;?&gt;</option>
        &lt;?php } ?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB