Welcome Guest, Not a member yet? Register   Sign In
populating form from database
#1

[eluser]sore eyes[/eluser]
hi, I'm new to codeigniter and not much older with php, so please excuse.

I want to populate a form from a database. First by listing shops, then selecting one from the list to populate the form.

The controller is 'welcome':
Code:
<?php

class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();
        $this->load->helper('form');
        $this->load->helper('url');
    }
    
    function index()
    {
        $data['query'] = $this->MShops->getShops();
        $this->load->view('template',$data);
    }
        
    function enterShop()
    {
        $data['title'] = "Shop";
        $shop_id = $this->uri->segment(3);
        $data['query'] = $this->MShops->getOneShop($shop_id);
        $this->load->view('shop_view',$data);
    }
    
    function shop_insert()
    {
        $this->db->insert('shop', $_POST);
        redirect('welcome/enterShop/'.$_POST[shop_id]);
    }
    
    
    
}
?>

Code:
The model is 'MShops':
<?php

class MShops extends Model{
    
    function MShops(){
        parent::Model();
    }
    
function getShops()
     {
    $this->db->select('shopname,shop_id');
    $data = $this->db->get('shop',10);
    return $data;
    }
    
    function getOneShop($shop_id)
    {
        $this->db->where('shop_id', $shop_id);
        $this->db->select('shopname,shop_id');
        $data = $this->db->get('shop');
        return $data;
    }
    
}
?>

The views are 'template':

Code:
<?php  foreach ($query->result() as $row):?>
<?=$row->shop_id; ?>
<?=$row->shopname; ?>
&lt;?=anchor('welcome/enterShop/'.$row->shop_id, 'goto') ?&gt; <br/>
&lt;?php endforeach ?&gt;

And view 'shop_view':
Code:
<h1>&lt;?=$title?&gt;</h1>

&lt;?=form_open('welcome/shop_insert');?&gt;

&lt;?=form_hidden('shop_id', $this->uri->segment(3));?&gt;
&lt;?php $shop_id = $this->uri->segment(3); ?&gt;
<p>Shop ID &lt;input type="text" name="shop_id" value="&lt;?=$shop_id?&gt;" /&gt;&lt;/p>
<p>Shop Name &lt;input type="text" name="shopname" value="&lt;?=$shopname?&gt;" /&gt;&lt;/p>
<p>&lt;input type="submit" value="submit" /&gt;&lt;/p>

I don't know what to use for "value" for the last piece of code.
#2

[eluser]jalalski[/eluser]
'$query->shop_id' and '$query->shopname' ?

BTW, I don't see where you are loading the Model in the Controller.
#3

[eluser]sore eyes[/eluser]
thanks for responding jalalski,


’$query->shop_id’ results in an error

Quote:Message: Undefined property: CI_DB_mysql_result::$shop_id

Filename: views/shop_view.php


The model is autoloaded.
#4

[eluser]jalalski[/eluser]
OK.

So, in the model, you are using:
Code:
$data = $this->db->get('shop');
        return $data;

which is returning a query result, better to do this:
Code:
$data = $this->db->get('shop');
        return $data->row();

although you may want to add some error handling in their as well.
#5

[eluser]sore eyes[/eluser]
hi jalalski, that's done the trick. Thank you so much.




Theme © iAndrew 2016 - Forum software by © MyBB