Articles


Our first module, part 7: finishing off our summary tag

Difficulty: Medium

Now we are adding the users’ bids to the database, let’s go back and finish our {exp:auction:summary} tag.

The purpose of this tag was to display the auction’s current bid price and the total number of bids.

So far we had got:

public function summary() {
    
    
// Find the entry_id of the auction to display
    
$entry_id $this->EE->TMPL->fetch_param('entry_id');
    if( 
$entry_id === FALSE {
        
return "";
    
}
    
    $tagdata 
$this->EE->TMPL->tagdata;
    
    
// Build array of our variables
    
$data = array(
        
"current_bid" => "0.00",
        
"total_bids" => 0
    
);
    
    
// Construct $variables array for use in parse_variables method
    
$variables = array();
    
$variables[] $data;

    return 
$this->EE->TMPL->parse_variables$tagdata$variables );

We were hard-coding the current_bid and total_bids values in the $data array as (at that stage) the auction database was empty.

All we need to do is to replace the ‘dummy’ values in the $data array with the correct values from the auction database table:

public function summary() {
    
    $tagdata 
$this->EE->TMPL->tagdata;
    
    
// Fetch entry id from tag parameters
    
$entry_id $this->EE->TMPL->fetch_param('entry_id');
    if( 
$entry_id === FALSE {
        
return "";
    
}
    
    
// Fetch data from database
    
$this->EE->db->select"MAX(bid_amount) as current_bid, 
        COUNT(*) as total_bids" 
);
    
$this->EE->db->where"entry_id"$entry_id );
    
$this->EE->db->group_by"entry_id" );
    
$query $this->EE->db->get"auction" );

    if( 
$query->num_rows() == {
        
// No bids exist in the table, so we'll return zeros
        
$data = array(
            
"current_bid" => "0.00",
            
"total_bids" => 0
        
);
    
else {
        
// Fetch the first (and only) result from the SQL query
        
$data $query->row_array();            
    
}

    
// Construct $variables array for use in parse_variables method
    
$variables = array();
    
$variables[] $data;

    return 
$this->EE->TMPL->parse_variables$tagdata$variables );

Here we’ve used the Active Record class to build the SQL to fetch the data:

SELECT 
    MAX
(bid_amount) as current_bid
    
COUNT(*) as total_bids
FROM exp_auction
WHERE entry_id 
= ?
GROUP BY entry_id 

One thing to note is that the database functions are intelligent enough to add (or replace) the database prefix that you configured when you installed EE. For example, my auction database table is actually called exp_auction. I could use $this->EE->db->get( "auction" ) or $this->EE->db->get( "exp_auction" ) to access it. Equally, if I used a different prefix (you can choose to do this when you install EE), the database functions will use that value. Even if you use $this->EE->db->get( "exp_auction" ) and you have chosen to use a different prefix, EE will use the appropriate table name.

We then test to see if any rows have been returned. If not, either the auction’s entry id does not exist or no bids have been placed yet. In either case, we’ll display the price and number of bids as zero.

If the SQL query does return a row, we’ll use that. The $query->row_array() will return the next (in this case, first) row’s result as an array, with columns mapped to array elements, eg:

Array
(
    
[current_bid] => 18.00
    [total_bids] 
=> 4

Summary

So we now have the {exp:auction:summary} tag working. We are now able to display an auction item with details of the current bidding situation, with a working form that allows visitors to place their own bid.

 

Comments

Thanks for working on this. When can we expect part 8 to be finished?

Posted by Matt on October 15, 2012

Just gone live. Any requests where you’d like the module to go next?

I can continue to add features, or I can start to look at the Control Panel-side.

Posted by Andrew on October 18, 2012

I was able to stitch together a little chunk of code that querys again on submission and only submits the bid if it’s higher than the previous highest bid. It uses the same basic success template. Could we maybe see how to go beyond the standard EE message box? Or, not sure how involved it would be, but it would be cool if we could send a confirmation email to the bidder and a notification to the previous high bidder.

Posted by Matt on October 20, 2012

Thanks Matt.

Those were both possible directions that I already had to take this, so I’ll try and add them next.

Andrew

Posted by Andrew on October 22, 2012

Are you guys going to continue with the series?  The control panel stuff?

This is a great tutorial.

Posted by Brian on December 29, 2012

Hi Brian,

I’m working on the next parts at the moment. These are more concerned with adding front-end features.

Are you particularly interested in the Control Panel side? If so, I can look into adding a few articles after that.

Andrew

Posted by Andrew on January 7, 2013

Yes, how these get added into the control panel would be interesting.

Thanks!

Posted by Brian on February 8, 2013

Add a comment

(Your email address will not be displayed on the site, but will be used for your gravatar)

Notify me of follow-up comments?