Post by itrends » Tue Sep 11, 2012 9:41 pm

I have added some code to model/account/customer.php

What I would now like to do is have parts of the code replaced with variables from a database table.

E.g.

<?
$from = "text from database goes here";
?>

which would come from the database from a table called 'tablename' from a column called 'textformessage'

How might I go about collecting that database table and then spitting out the value into the code? :)

Active Member

Posts

Joined
Tue Jul 14, 2009 7:54 pm

Post by Avvici » Wed Sep 12, 2012 3:45 am

What code exactly did you add to the model? The model file is for running query's on the database. The control is used for setting up variables to be echoed on to the HTML (or it can query the database , skipping the model entirely)

Your model function could look something like this:

Code: Select all

public function yourFunction() {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "yourtable" ORDER BY field_id);
        
        return $query->rows;
    }
Or if you want to carry an argument over to the function you can do it like this (including a WHERE /AND )

Code: Select all

public function yourFunction($argument_id) {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "yourtable WHERE field_id = '" . (int)$argument_id . "' ORDER BY id");
        
        return $query->rows;
    }
Your control would look something like this (with the argument + foreach loop to deal with the object array):

Code: Select all

$query = $this->model_catalog_product->yourFunction($argument_value);
            
            foreach ($query  as $value) {
                
                $this->data['value'] = $value['field_id'];
            }
Now you simply echo the value in the HTML

Code: Select all

<?php echo $value;?>
If you have more than one value to echo then you set up an array in the control and iterate through it in the HTML with php's FOR EACH LOOP.

Important: Notice the query in the model file uses a wildcard (*). You may not need this if you only have one or two values you want to grab. In this case you don't need to have this in your control:

Code: Select all

  foreach ($query  as $value) {
                
                $this->data['value'] = $value['field_id'];
            }
You can simply echo the value straight from the $query variable like this:

Code: Select all

$query = $this->model_catalog_product->yourFunction($argument_value);
$this->data['value'] = $query['field_id'];
And the model function will look like this:

Code: Select all

public function yourFunction($argument_id) {
        $query = $this->db->query("SELECT field_id FROM " . DB_PREFIX . "yourtable WHERE field_id = '" . (int)$argument_id . "'");
return $query->row;
} 

User avatar
Expert Member

Posts

Joined
Tue Apr 05, 2011 12:09 pm
Location - Asheville, NC

Post by itrends » Wed Sep 12, 2012 10:18 pm

Thank you very much for the explination. I will try and get my head around it.

Right now I don't need to echo anything out into the html / view. I just need to run a script / process behind the scenes that triggers something but I want the option and value of it to come from database settings that the user can control in the admin. :)

Active Member

Posts

Joined
Tue Jul 14, 2009 7:54 pm
Who is online

Users browsing this forum: No registered users and 124 guests