Welcome Guest, Not a member yet? Register   Sign In
Displaying data from a query
#1

[eluser]bcarter[/eluser]
Hi guys

I have probably the biggest noob question going! I am trying to display some data I have pulled out of my database and I am almost bald with the frustration!

I can get it to work with a foreach loop but as I only want to display 1 record (I don't want to use a limit clause in my query). How do I display data without using the foreach loop?

Here's my code...

Model
Code:
class Update_model extends Model {
    
    function Update_model() {
        
        parent::Model();
    }
    
    function get_data($id) {
        
        $q = $this->db->query("SELECT details.empName, details.contact, details.add1, details.add2, details.add3, details.town, details.postTown, details.county, details.postCode, details.tel, details.email, details.interestArea, details.linkAdviser, details.leadSource, details.lead_status, details.tps_date, details.employee_count, notes.notes
        FROM details
        LEFT JOIN notes ON ( details.id = notes.employer )
        WHERE details.id = '$id'");
        
        $info = $q->result();
        
        return     $info;
    }
}


View
Code:
<table class="results_table">
        <tr>
            <td><label>Contact: </label></td>
            <td>&lt;?php echo $info->contact ?&gt;</td>
        </tr>
    <table>

Controller
Code:
class Update extends Controller {
    
    function Update() {
        
        parent::Controller();
    }
    
    function index() {
        $data['page_title'] = "Update Lead";
        
        $this->load->library('form_validation');
            
        $id  = '34';
        
        $this->load->model('Update_model');
        
        $data['info'] = $this->Update_model->get_data($id);
        
        $this->load->view('head', $data);
        $this->load->view('update_view', $data);
        
    }
}


Thanks for helping a noob!
#2

[eluser]mddd[/eluser]
The "result" object is an object containing multiple results.
If you only need the first one, use the "row" object.
This is similar to the 'mysql_fetch' function of php. It will get you just one row.

So, in your model, use $info = $q->row(); in stead of $info = $q->result();
That should do it for you.
#3

[eluser]bcarter[/eluser]
Thanks mate!!!
#4

[eluser]JoostV[/eluser]
Even though you do not like to use a limit clause, doing so would actually improve the performance of your script Smile




Theme © iAndrew 2016 - Forum software by © MyBB