1

I'm querying an external timetable database, and I need to get the start and end points of the journey to use in a hidden field.

My template code looks like this:

{exp:get_piers:departure_pier cruise_id="{cruise_id}"}

and

{exp:get_piers:arrival_pier cruise_id="{cruise_id}"}

{cruise_id} is simply a numeric value that is set in the EE channel, but relates to the external database.

So I've created a plugin that looks like this:

class Get_piers {

    public $return_data;
    public $cruise_id;

    /**
     * Constructor
     */
    public function __construct()
    {
    $this->EE =& get_instance();
    //get cruise id from template tag
    $this->cruise_id = $this->EE->TMPL->fetch_param('cruise_id');
    //connect to external db
    $this->bookings_db = $this->EE->load->database('timetable', TRUE);
    }

// ----------------------------------------------------------------

/**
 * Departure Point
 */
function departure_pier() {
    $piers = $this->bookings_db->select('depPier')
            ->from('tbl_cruises')
            ->where('cruiseID', $this->cruise_id)
            ->get();
    return $piers->row('depPier');
    }

function arrival_pier() {   
    $piers = $this->bookings_db->select('arrPier')
            ->from('tbl_cruises')
            ->where('cruiseID', $this->cruise_id)
            ->get();
    return $piers->row('arrPier');
}

The problem is that the value for $this->cruise_id is showing in the query as '0', but if I try to return just the value for $this->cruise_id (outside the sql), I get the correct number passed through from the template.

What am I missing? I'm guessing its something to do with visibility inside/outside the function, but having tried lots of alternatives, it just ain't working for me.

Can anyone offer any pointers?

7
  • 2
    Have you tried setting $this->cruise_id inside of departure_pier() instead of inside the constructor? Jun 5, 2014 at 17:36
  • Derek Hogue, please submit this as an answer. Jun 5, 2014 at 20:16
  • Hi Derek, yes, I tried this and got the following fatal error. Cannot re-assign $this Jun 5, 2014 at 20:44
  • Tried a couple more alternatives, but no progress yet. If I output $sql = "SELECT depPier FROM tbl_cruises WHERE cruiseID = $this->cruise_id" to screen, I get the query I want, but when I try and execute it, it seem its then that the $this->cruise_id variable is being nulled or zero'd. Jun 5, 2014 at 21:42
  • What do you get if you manually construct your query rather than use EE/CI helpers to build it? ie $query="SELECT depPier FROM tbl_cruises WHERE cruiseID=".$this->cruise_id;
    – foamcow
    Jun 6, 2014 at 9:57

1 Answer 1

0

Numpty alert...fixed. The original code was in fact working fine, I had a stray tag in my template that had an empty parameter value - hence the null value in the SQL.

:-/

2
  • Should we close the question?
    – Sobral
    Jun 5, 2014 at 22:54
  • Yes - by all means - sorry :-) Jun 5, 2014 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.