Tuesday, June 9

Addendum about Sum()

It seems like it's totally okay if you're selecting just the sum to leave out the group by part. I needed to pull data and put it in rows, then show the total on the bottom. I don't know if I necessarily did this the _right_ way, but it worked.

My two queries:

$query1 = "SELECT * FROM table";
$query2 = "SELECT sum(amt) FROM table";
It was in a table so I used a while loop to add rows, then after the loop, added
<tr>
<td>TOTAL</td>
<td>$<?=number_format($row1['sum(pay_amt)'],2)?></td>
</tr>

of course, don't forget to define them. i defined row1 before the while loop.

Thursday, June 4

Notes to Self

Populating a table from csv:

  LOAD DATA LOCAL INFILE '/data.csv'
->INTO TABLE test_table
->FIELDS TERMINATED BY ','
->LINES TERMINATED BY '\r\n'
->(field1, filed2, field3);
Fields for money, use DECIMAL(N,2). N is however long the number will be and 2 is for the cents.

Then use PHP's number_format($number,2) to make it look pretty/readable.

Quick and dirty sum (via tizag):
<?php
$query = "SELECT type, SUM(price) FROM products GROUP BY type";

$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
echo "Total ". $row['type']. " = $". $row['SUM(price)'];
echo "<br />";
}
?>
Don't use & in your $_GET var. Do a str_replace to something else (no, not & amp; either), then change it back before the query

Gigundo tables will time out before loading the page

We are 2.6 parsecs from Sirius

Tuesday, June 2

Hacking QContacts

For the "Contact us" portion of our sites, we use QContacts instead of the plain Joomla contact module. The main reason is because it has the ability to add more fields to the email form. However, I decided a couple things needed to be changed.

  1. It uses a captcha type field using the php secureimage stuff. We need to use something that's ADA Compliant, so I wanted to use ReCaptcha.
  2. We have a big, complex site and every page has a "Contact Webmaster" link. Although it specifically says in bold print right at the top of the form to include the link of the page you're talking about, guess how many people do. Yes, about 1%. So, I wanted to add a referring link to the email.
Luckily, the code for QContacts is pretty straightforward. There were only two files I needed to change, and one I needed to add (for the ReCaptcha).

ReCaptcha Replacement

Using the recaptcha site as a reference, first we edit the file: /components/com_qcontacts/views/contact/tmpl/default_form.php. This is the page containing the actual form (and the hardest to find the first time). I put the recaptchalib.php file into the same place the original securimage.php file was (com_qcontacts/includes/secureimage). Down near the bottom, there will be the captcha code. it starts
if ($this->params->get('captcha')) {
which the following would replace the original code:
require_once JPATH_COMPONENT . DS . 'includes' . DS . 'securimage' . DS . 'recaptchalib.php';
$publickey = "..."; // you got this from the signup page
echo recaptcha_get_html($publickey);
}
Simple enough. Now, we need to validate the user input. This is in the file com_qcontacts/models/contact.php.

Near the top (around line 40 or so) will start the function __construct(). We need to add the following line:
$this->_data->captcha = JRequest::getString('recaptcha_response_field', '', 'post');
This gets the recaptcha_response_field as a post variable from the previous page. This should replace the existing captcha line. But wait, there's more!

Farther down in the code in this same file is the function __validateInputs and in here is where the captcha is validated. We need to replace the code after
if($sc) {
With
require_once JPATH_COMPONENT . DS . 'includes' . DS . 'securimage' . DS . 'recaptchalib.php';
$privatekey = "...";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$captcha);

if (!$resp->is_valid) {
$this->setError( JText::_('Security code was incorrect, please try again', true));
return false;
}
So, fairly simple. In the Joomla backend, when you enable captcha, it will be replaced with ReCaptcha. None of the other parameters have any effect anymore. Now for #2.

Adding Referrer

This one is much simpler. We'll start with the same file as the last one (com_qcontacts/views/contact/tmpl/default_form.php).

At the end of the form, add:
<input type="hidden" name="refer" value="<?=$_SERVER['HTTP_REFERER']?>" />
Now on the other page (com_qcontacts/models/contact.php), add a line to function __construct again:
$this->_data->refer = JRequest::getString('refer', '', 'post');
Then find a line in the email creation section. I stuck the code after the show_ip param so it'd be at the end of the email.
$body .= "\r\n\r\n" . JText::_('Referring Page').': '.$this->_data->refer;
And that's it!