I have php function that sends emails to users using templates from db. The problem is that the last 3 queries are not working (I tried from phpmyadmin and it works fine). The code executes, no errors, but instead of getting amount of views and replies there is just empty space (db->loadobjectlist returns null?) and the last update query not doing anything either.
This is what it is sending at the end
Please help me to understand what's wrong with this code
$config = JFactory::getConfig();
$mailfrom = $config->get('mailfrom');
$fromname = $config->get('sitename');
$dbprefix = $config->getValue('config.dbprefix');
$database = JFactory::getDBO();
$query = "select u.id, u.email
from ".$dbprefix."users as u
left join ".$dbprefix."djcf_items as i on u.id=i.user_id and i.published=1
where u.sendEmail=1 and TIMESTAMPDIFF(hour,NOW(), u.WeeklyAD)=0
having count(i.id)>0";
$database->setQuery($query);
$users = $database->loadObjectList();
$template_name = 'Weekly ad';
$template_table = 'Weekly table';
$subject = 'Ads Peformance Update';
$template_main = getTemplate($template_name);
$template_table = getTemplate($template_table);
foreach($users as $user){
$query = "select id, name, image_url, alias, timestampdiff(day, date_start, now()) as days_live
from ".$dbprefix."djcf_items
where user_id = ".$user->id." and published=1";
$database->setQuery($query);
$items = $database->loadObjectList();
$template = $template_main;
foreach($items as $item){
$query = "select count(distinct v.user) as overall, count(distinct vw.user) as week
from ".$dbprefix."visitors as v
left join ".$dbprefix."visitors as vw on vw.name=v.name and timestampdiff(hour, vw.date, now())<=7*24
where v.name='".$item->alias.$item->id."'";
$database->setQuery($query);
$views = $database->loadObjectList();
$query = "select count(distinct ia.id) as overall, count(distinct iaw.id) as week
from ".$dbprefix."djcf_itemsask as ia
left join ".$dbprefix."djcf_itemsask as iaw on iaw.id=ia.item_id and timestampdiff(hour, iaw.date, now())<=7*24
where ia.item_id=".$item->id;
$database->setQuery($query);
$replies = $database->loadObjectList();
$table .= $template_table;
$imagelink = explode(';',$item->image_url);
$imagelink = 'http://'.$_SERVER['HTTP_HOST'].'/components/com_djclassifieds/images/'.$imagelink[0];
$table = str_replace("##ITEM_NAME##", $item->name, $table);
$table = str_replace("##DAYS##", $item->days_live, $table);
$table = str_replace("##VIEWS##", $views->overall, $table);
$table = str_replace("##VIEWS_WEEK##", $views->week, $table);
$table = str_replace("##REPLIES##", $replies->overall, $table);
$table = str_replace("##REPLIES_WEEK##", $replies->week, $table);
$table = str_replace("##ITEM_IMG##", $imagelink, $table);
}
$template = str_replace("##WEEKLY_TABLE##", $table, $template);
SendEmail($mailfrom, $fromname, $user->email, $subject, $template);
$query = "update ".$dbprefix."users set WeeklyAD=ADDDATE(WeeklyAD, interval 7 day) where id='".$user->id."'";
$database->setQuery($query);
}
It appears you may be using the wrong function in the select queries since loadObjectList()
returns an indexed array of PHP objects from the table records returned by the query. To use this function you would access the individual values by using:
$row['index']->name // e.g. $row['2']->email
For single row results use loadObject()
which returns a PHP object from a single record in the table. This matches your code since you would access the individual values by using:
$result->index // e.g. $result->email
The final update query is not executing because you are missing the execute()
.
$query = "update ".$dbprefix."users set WeeklyAD=ADDDATE(WeeklyAD, interval 7 day) where id='".$user->id."'";
$database->setQuery($query);
$result = $database->execute();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.