Notice: Undefined property: PDOStatement::$mysqli_num_rows in - php

Here is my PHP code who is returning the following error.
public static function categoryParentChildTree($parent = 14, $spacing = '', $category_tree_array = '') {
$db=Db::getConnection();
if (!is_array($category_tree_array))
$category_tree_array = array();
$sql = 'SELECT id,name,parentid FROM category WHERE parentid = 14 ORDER BY id ASC';
$resCategory=$db->query($sql);
*if ($resCategory->num_rows > 0) {*
while($rowCategories = $resCategory->fetch_assoc()) {
$category_tree_array[] = array("id" => $rowCategories['id'], "name" => $spacing . $rowCategories['name']);
$category_tree_array = Self::categoryParentChildTree($rowCategories['id'], ' '.$spacing . '- ', $category_tree_array);
}
}
return $category_tree_array;
}
please help me this function output this error
Notice: Undefined property: PDOStatement::$mysqli_num_rows in

Your notice appears because the object is not created.
Replace line $resCategory=$db->query($sql); by this :
if($resCategory = $db->query($sql)) {
//continue with your code...
if ($resCategory->num_rows > 0) {
...
}
else {
// SQL Query didn't return anything
}

Related

Invalid parameter number: number of bound variables does not match number of tokens (search filter php mvc)

Im trying to make a search filter for table products and im trying to have pagination and filter in the same sql query. Im using a MVC pattern.
This is the model:
<?php
namespace Dao\test;
use Dao\Table;
class Productos extends Table
{
public static function obtenerProductos($list, $search, $numPerPage)
{
$startFrom = (intval($list)-1)*$numPerPage;
$sqlStr = "SELECT * FROM productos WHERE name LIKE '%:search%' LIMIT :startFrom,:numPerPage;";
$parametros = array(
"search" => $search,
"startFrom" => $startFrom,
"numPerPage" => $numPerPage
);
return self::obtenerRegistros($sqlStr, $parametros);
}
public static function obtenerNumeroProductos()
{
$sqlStr = "SELECT * FROM productos;";
return self::obtenerRegistros($sqlStr, array());
}
}
?>
This is the controller:
<?php
namespace Controllers\test;
use Controllers\PublicController;
use Views\Renderer;
class Productoss extends PublicController
{
public function run() :void
{
\Utilities\Site::addLink("/public/css/test1.css");
if(isset($_GET["search"])){
$search = $_GET["search"];
}else{
$search="";
}
$numPerPage = 5;
if(isset($_GET["list"])){
$list=$_GET["list"];
}
else{
$list=1;
}
$viewData = array(
"totalProductos" => 0,
"totalList" => "",
);
$viewData["producto"] = \Dao\test\Productos::obtenerProductos($list, $search, $numPerPage);
$viewData["lista"] = \Dao\test\Productos::obtenerNumeroProductos();
foreach ($viewData["lista"] as $producto) {
$viewData["totalProductos"] = $viewData["totalProductos"] + 1;
}
$viewData["totalList"] = ceil($viewData["totalProductos"]/$numPerPage);
for ($i=1; $i <= $viewData["totalList"]; $i++) {
$viewData["nList"]["number"] = $i;
$viewData["nPages"][] = $viewData["nList"];
}
if($list<$viewData["totalList"]){
$viewData["next"] = true;
$viewData["nextBtn"] = $list+1;
}
if ($list>1) {
$viewData["previous"] = true;
$viewData["prevBtn"] = $list - 1;
}
Renderer::render("test/Productoss", $viewData);
}
}
?>
I get the error:
Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter
number: number of bound variables does not match number of tokens in
C:\xampp\htdocs\PHPmvc\SimplePHPMvcOPP\src\Dao\Table.php:70 Stack
trace: #0
C:\xampp\htdocs\PHPmvc\SimplePHPMvcOPP\src\Dao\Table.php(70):
PDOStatement->execute() #1
C:\xampp\htdocs\PHPmvc\SimplePHPMvcOPP\src\Dao\test\Productos.php(18):
Dao\Table::obtenerRegistros('SELECT * FROM p...', Array) #2
C:\xampp\htdocs\PHPmvc\SimplePHPMvcOPP\src\Controllers\test\Productoss.php(34):
Dao\test\Productos::obtenerProductos(1, 'a', 5) #3
C:\xampp\htdocs\PHPmvc\SimplePHPMvcOPP\index.php(27):
Controllers\test\Productoss->run() #4 {main} thrown in
C:\xampp\htdocs\PHPmvc\SimplePHPMvcOPP\src\Dao\Table.php on line 70
I finally found an answer, what i did was this:
public static function obtenerLaboratorios($list, $search, $numPerPage)
{
$startFrom = (intval($list)-1)*$numPerPage;
if(!empty($search)){
$sqlStr = "SELECT * FROM laboratorio WHERE `laboratorioNombre` LIKE :search LIMIT :startFrom,:numPerPage;";
$parametros = array(
"search" => "%$search%",
"startFrom" => $startFrom,
"numPerPage" => $numPerPage
);
}else{
$sqlStr = "SELECT * FROM laboratorio LIMIT :startFrom,:numPerPage;";
$parametros = array(
"startFrom" => $startFrom,
"numPerPage" => $numPerPage
);
}
return self::obtenerRegistros($sqlStr, $parametros);
}
This way the mvc doesn't take the error for the '' symbols, that was what cause me problems. Takes the value of the variable $search and just puts the "%" in the start/end of the variable.

Warning: Undefined property: Joomla\CMS\Object\CMSObject::$playid in D:\xampp\htdocs\joomla4\components\com_football\tmpl\team\default.php on line 120

So I am building a Joomla component which will be like a football league manager. In the DB I have a table with #__football_team and #__football_player in which I am trying to display the players on the team. When trying to pull and assign in the model view using:
if (isset($this->_item->playid) && $this->_item->playid != '') {
if (is_object($this->_item->playid)) {
$this->_item->playid = ArrayHelper::fromObject($this->_item->playid);
}
$values = (is_array($this->_item->playid)) ? $this->_item->playid : explode(',',$this->_item->playid);
$textValue = array();
foreach ($values as $value) {
$db = Factory::getDbo();
$query = "SELECT *
FROM #__football_players
WHERE `active` = '1'
AND #__football_team.teamid LIKE '" . $value . "'";
$db->setQuery($query);
$results = $db->loadObject();
if ($results) {
$textValue[] = $results->lname;
}
}
$this->_item->playid = !empty($textValue) ? implode(', ', $textValue) : $this->_item->playid;
}
return $this->_item;
}
But I keep getting an error that the property is undefined:
Warning: Undefined property: Joomla\CMS\Object\CMSObject::$playid in D:\xampp\htdocs\joomla4\components\com_football\tmpl\team\default.php on line 120

How to return filtered information in a foreach loop

I want to filter information inside the foreach and to return as result only some of them.
This is the code :
$keywords = "";
$avgmonthly = "";
$compet ="";
foreach ($response->getResults() as $result) {
$avgmonthly = $result->getKeywordIdeaMetrics()->getAvgMonthlySearches()->getValue();
$compet = $result->getKeywordIdeaMetrics()->getCompetition();
$log = $this->getLogger();
$log->err($response->serializeToJsonString());
if ($avgmonthlysearch > 10 && $competition== 4) {
$keywords .= $result->getText()->getValue() . ",";
}
}
return $keywords;
in my log i see this results:
{"results":[{"text":"world","keywordIdeaMetrics":
{"avgMonthlySearches":"140","competition":"HIGH"}},
{"text":"today ","keywordIdeaMetrics":{"avgMonthlySearches":"10","competition":"HIGH"{"text":"office","keywordIdeaMetrics":{"avgMonthlySearches":"5","competition":"LOW"}}
when i try to call this function i get this error : Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function getAvgMonthlySearches() on null
If your goal is just to espace this case, you can add an if condition ( As $result->getKeywordIdeaMetrics() can be null, it will not go inside this if condition )
foreach ($response->getResults() as $result) {
if ($result->getKeywordIdeaMetrics()) {
$avgmonthly = $result->getKeywordIdeaMetrics()->getAvgMonthlySearches()->getValue();
$compet = $result->getKeywordIdeaMetrics()->getCompetition();
}
$log = $this->getLogger();
$log->err($response->serializeToJsonString());
if ($avgmonthlysearch > 10 && $competition== 4) {
$keywords .= $result->getText()->getValue() . ",";
}
}

PHP Warning: number_format() expects parameter 1 to be float [duplicate]

This question already has answers here:
Warning: number_format() expects parameter 1 to be double, string given [closed]
(3 answers)
Closed 5 months ago.
Can you please someone help me with this code I got
PHP Warning: number_format() expects parameter 1 to be float, string given in line 39
I am copied the whole class I hope that will help you better to find the problem.
class stats_site extends stats {
function __construct() {
global $CONF, $DB, $FORM, $LNG, $TMPL;
$TMPL['header'] = $LNG['stats_header'];
$TMPL['username'] = $DB->escape($FORM['u'], 1);
$stats = $DB->fetch("SELECT * FROM {$CONF['sql_prefix']}_stats WHERE username = '{$TMPL['username']}'", __FILE__, __LINE__);
unset($stats['username']);
$sites = array($DB->fetch("SELECT * FROM {$CONF['sql_prefix']}_sites WHERE username = '{$TMPL['username']}'", __FILE__, __LINE__));
if ($stats) {
$TMPL = array_merge($TMPL, $stats, $sites);
$TMPL['unq_pv_max_daily'] = $TMPL['unq_pv_0_daily'] > $TMPL['unq_pv_max_daily'] ? $TMPL['unq_pv_0_daily'] : $TMPL['unq_pv_max_daily'];
$TMPL['tot_pv_max_daily'] = $TMPL['tot_pv_0_daily'] > $TMPL['tot_pv_max_daily'] ? $TMPL['tot_pv_0_daily'] : $TMPL['tot_pv_max_daily'];
$TMPL['unq_in_max_daily'] = $TMPL['unq_in_0_daily'] > $TMPL['unq_in_max_daily'] ? $TMPL['unq_in_0_daily'] : $TMPL['unq_in_max_daily'];
$TMPL['tot_in_max_daily'] = $TMPL['tot_in_0_daily'] > $TMPL['tot_in_max_daily'] ? $TMPL['tot_in_0_daily'] : $TMPL['tot_in_max_daily'];
$TMPL['unq_out_max_daily'] = $TMPL['unq_out_0_daily'] > $TMPL['unq_out_max_daily'] ? $TMPL['unq_out_0_daily'] : $TMPL['unq_out_max_daily'];
$TMPL['tot_out_max_daily'] = $TMPL['tot_out_0_daily'] > $TMPL['tot_out_max_daily'] ? $TMPL['tot_out_0_daily'] : $TMPL['tot_out_max_daily'];
$TMPL['unq_pv_max_weekly'] = $TMPL['unq_pv_0_weekly'] > $TMPL['unq_pv_max_weekly'] ? $TMPL['unq_pv_0_weekly'] : $TMPL['unq_pv_max_weekly'];
$TMPL['tot_pv_max_weekly'] = $TMPL['tot_pv_0_weekly'] > $TMPL['tot_pv_max_weekly'] ? $TMPL['tot_pv_0_weekly'] : $TMPL['tot_pv_max_weekly'];
$TMPL['unq_in_max_weekly'] = $TMPL['unq_in_0_weekly'] > $TMPL['unq_in_max_weekly'] ? $TMPL['unq_in_0_weekly'] : $TMPL['unq_in_max_weekly'];
$TMPL['tot_in_max_weekly'] = $TMPL['tot_in_0_weekly'] > $TMPL['tot_in_max_weekly'] ? $TMPL['tot_in_0_weekly'] : $TMPL['tot_in_max_weekly'];
$TMPL['unq_out_max_weekly'] = $TMPL['unq_out_0_weekly'] > $TMPL['unq_out_max_weekly'] ? $TMPL['unq_out_0_weekly'] : $TMPL['unq_out_max_weekly'];
$TMPL['tot_out_max_weekly'] = $TMPL['tot_out_0_weekly'] > $TMPL['tot_out_max_weekly'] ? $TMPL['tot_out_0_weekly'] : $TMPL['tot_out_max_weekly'];
$TMPL['unq_pv_max_monthly'] = $TMPL['unq_pv_0_monthly'] > $TMPL['unq_pv_max_monthly'] ? $TMPL['unq_pv_0_monthly'] : $TMPL['unq_pv_max_monthly'];
$TMPL['tot_pv_max_monthly'] = $TMPL['tot_pv_0_monthly'] > $TMPL['tot_pv_max_monthly'] ? $TMPL['tot_pv_0_monthly'] : $TMPL['tot_pv_max_monthly'];
$TMPL['unq_in_max_monthly'] = $TMPL['unq_in_0_monthly'] > $TMPL['unq_in_max_monthly'] ? $TMPL['unq_in_0_monthly'] : $TMPL['unq_in_max_monthly'];
$TMPL['tot_in_max_monthly'] = $TMPL['tot_in_0_monthly'] > $TMPL['tot_in_max_monthly'] ? $TMPL['tot_in_0_monthly'] : $TMPL['tot_in_max_monthly'];
$TMPL['unq_out_max_monthly'] = $TMPL['unq_out_0_monthly'] > $TMPL['unq_out_max_monthly'] ? $TMPL['unq_out_0_monthly'] : $TMPL['unq_out_max_monthly'];
$TMPL['tot_out_max_monthly'] = $TMPL['tot_out_0_monthly'] > $TMPL['tot_out_max_monthly'] ? $TMPL['tot_out_0_monthly'] : $TMPL['tot_out_max_monthly'];
$this->averages();
$TMPL['average_rating'] = $TMPL['num_ratings'] > 0 ? round($TMPL['total_rating'] / $TMPL['num_ratings'], 0) : 0;
$stats = array_map('number_format', $stats);
$TMPL = array_merge($TMPL, $stats);
$this->locale();
$TMPL['header'] .= " - {$TMPL['title']}";
$TMPL['category_url'] = urlencode($TMPL['category']);
$query = "SELECT id, date, review FROM {$CONF['sql_prefix']}_reviews WHERE username = '{$TMPL['username']}' AND active = 1";
if (isset($FORM['all_reviews']) && $FORM['all_reviews']) {
$result = $DB->query("{$query} ORDER BY date DESC", __FILE__, __LINE__);
}
else {
$result = $DB->select_limit("{$query} ORDER BY RAND()", 2, 0, __FILE__, __LINE__);
}
$TMPL['reviews'] = '';
while (list($TMPL['id'], $TMPL['date'], $TMPL['review']) = $DB->fetch_array($result)) {
$TMPL['reviews'] .= $this->do_skin('stats_review');
}
$TMPL['content'] = $this->do_skin('stats');
}
else {
$this->error($LNG['g_invalid_u']);
}
}
}
And line 39 is this line here:
$stats = array_map('number_format', $stats);
Many thanks in advance!
EDIT
I've put here THE FULL CODE so you can better fix the problem with it.
I tried everything you advice here but it doesn't help :(
Here is the full code:
<?php
if (!defined('ATSPHP')) {
die("This file cannot be accessed directly.");
}
class stats extends base {
function __construct() {
global $FORM;
if (isset($FORM['u'])) { $stats = new stats_site; }
else { $stats = new stats_overall; }
}
function averages() {
global $TMPL;
$ranking_periods = array('daily', 'weekly', 'monthly');
$ranking_methods = array('unq_pv', 'tot_pv', 'unq_in', 'tot_in', 'unq_out', 'tot_out');
foreach ($ranking_periods as $ranking_period) {
foreach ($ranking_methods as $ranking_method) {
$TMPL["{$ranking_method}_avg_{$ranking_period}"] = 0;
for ($i = 0; $i < 10; $i++) {
$TMPL["{$ranking_method}_avg_{$ranking_period}"] = $TMPL["{$ranking_method}_avg_{$ranking_period}"] + $TMPL["{$ranking_method}_{$i}_{$ranking_period}"];
}
$TMPL["{$ranking_method}_avg_{$ranking_period}"] = number_format($TMPL["{$ranking_method}_avg_{$ranking_period}"] / 10, 1);
}
}
}
function locale() {
global $CONF, $LNG, $TMPL;
setlocale(LC_ALL, $CONF['default_language']);
for ($i = 2; $i < 10; $i++) {
$TMPL["{$i}_daily"] = strftime('%B %d', time()-3600*24*$i + (3600*$CONF['time_offset']));
}
for ($i = 2; $i < 10; $i++) {
$TMPL["{$i}_weekly"] = "{$LNG['stats_week']} ".date('W', time()-3600*24*7*$i + (3600*$CONF['time_offset']));
}
for ($i = 2; $i < 10; $i++) {
$TMPL["{$i}_monthly"] = strftime('%B %y', mktime(0, 0, 0, date('m')-$i, 1));
}
}
}
class stats_site extends stats {
function __construct() {
global $CONF, $DB, $FORM, $LNG, $TMPL;
$TMPL['header'] = $LNG['stats_header'];
$TMPL['username'] = $DB->escape($FORM['u'], 1);
$stats = $DB->fetch("SELECT * FROM {$CONF['sql_prefix']}_stats WHERE username = '{$TMPL['username']}'", __FILE__, __LINE__);
unset($stats['username']);
$sites = array($DB->fetch("SELECT * FROM {$CONF['sql_prefix']}_sites WHERE username = '{$TMPL['username']}'", __FILE__, __LINE__));
if ($stats) {
$TMPL = array_merge($TMPL, $stats, $sites);
$TMPL['unq_pv_max_daily'] = $TMPL['unq_pv_0_daily'] > $TMPL['unq_pv_max_daily'] ? $TMPL['unq_pv_0_daily'] : $TMPL['unq_pv_max_daily'];
$TMPL['tot_pv_max_daily'] = $TMPL['tot_pv_0_daily'] > $TMPL['tot_pv_max_daily'] ? $TMPL['tot_pv_0_daily'] : $TMPL['tot_pv_max_daily'];
$TMPL['unq_in_max_daily'] = $TMPL['unq_in_0_daily'] > $TMPL['unq_in_max_daily'] ? $TMPL['unq_in_0_daily'] : $TMPL['unq_in_max_daily'];
$TMPL['tot_in_max_daily'] = $TMPL['tot_in_0_daily'] > $TMPL['tot_in_max_daily'] ? $TMPL['tot_in_0_daily'] : $TMPL['tot_in_max_daily'];
$TMPL['unq_out_max_daily'] = $TMPL['unq_out_0_daily'] > $TMPL['unq_out_max_daily'] ? $TMPL['unq_out_0_daily'] : $TMPL['unq_out_max_daily'];
$TMPL['tot_out_max_daily'] = $TMPL['tot_out_0_daily'] > $TMPL['tot_out_max_daily'] ? $TMPL['tot_out_0_daily'] : $TMPL['tot_out_max_daily'];
$TMPL['unq_pv_max_weekly'] = $TMPL['unq_pv_0_weekly'] > $TMPL['unq_pv_max_weekly'] ? $TMPL['unq_pv_0_weekly'] : $TMPL['unq_pv_max_weekly'];
$TMPL['tot_pv_max_weekly'] = $TMPL['tot_pv_0_weekly'] > $TMPL['tot_pv_max_weekly'] ? $TMPL['tot_pv_0_weekly'] : $TMPL['tot_pv_max_weekly'];
$TMPL['unq_in_max_weekly'] = $TMPL['unq_in_0_weekly'] > $TMPL['unq_in_max_weekly'] ? $TMPL['unq_in_0_weekly'] : $TMPL['unq_in_max_weekly'];
$TMPL['tot_in_max_weekly'] = $TMPL['tot_in_0_weekly'] > $TMPL['tot_in_max_weekly'] ? $TMPL['tot_in_0_weekly'] : $TMPL['tot_in_max_weekly'];
$TMPL['unq_out_max_weekly'] = $TMPL['unq_out_0_weekly'] > $TMPL['unq_out_max_weekly'] ? $TMPL['unq_out_0_weekly'] : $TMPL['unq_out_max_weekly'];
$TMPL['tot_out_max_weekly'] = $TMPL['tot_out_0_weekly'] > $TMPL['tot_out_max_weekly'] ? $TMPL['tot_out_0_weekly'] : $TMPL['tot_out_max_weekly'];
$TMPL['unq_pv_max_monthly'] = $TMPL['unq_pv_0_monthly'] > $TMPL['unq_pv_max_monthly'] ? $TMPL['unq_pv_0_monthly'] : $TMPL['unq_pv_max_monthly'];
$TMPL['tot_pv_max_monthly'] = $TMPL['tot_pv_0_monthly'] > $TMPL['tot_pv_max_monthly'] ? $TMPL['tot_pv_0_monthly'] : $TMPL['tot_pv_max_monthly'];
$TMPL['unq_in_max_monthly'] = $TMPL['unq_in_0_monthly'] > $TMPL['unq_in_max_monthly'] ? $TMPL['unq_in_0_monthly'] : $TMPL['unq_in_max_monthly'];
$TMPL['tot_in_max_monthly'] = $TMPL['tot_in_0_monthly'] > $TMPL['tot_in_max_monthly'] ? $TMPL['tot_in_0_monthly'] : $TMPL['tot_in_max_monthly'];
$TMPL['unq_out_max_monthly'] = $TMPL['unq_out_0_monthly'] > $TMPL['unq_out_max_monthly'] ? $TMPL['unq_out_0_monthly'] : $TMPL['unq_out_max_monthly'];
$TMPL['tot_out_max_monthly'] = $TMPL['tot_out_0_monthly'] > $TMPL['tot_out_max_monthly'] ? $TMPL['tot_out_0_monthly'] : $TMPL['tot_out_max_monthly'];
$this->averages();
$TMPL['average_rating'] = $TMPL['num_ratings'] > 0 ? round($TMPL['total_rating'] / $TMPL['num_ratings'], 0) : 0;
function formatter($stats) { return number_format(floatval($stats)); }
$stats = array_map('number_format', $stats);
//$stats = array_map(function($v){return is_numeric($v) ? number_format($v) : $v;}, $stats);
$TMPL = array_merge($TMPL, $stats);
$this->locale();
$TMPL['header'] .= " - {$TMPL['title']}";
$TMPL['category_url'] = urlencode($TMPL['category']);
$query = "SELECT id, date, review FROM {$CONF['sql_prefix']}_reviews WHERE username = '{$TMPL['username']}' AND active = 1";
if (isset($FORM['all_reviews']) && $FORM['all_reviews']) {
$result = $DB->query("{$query} ORDER BY date DESC", __FILE__, __LINE__);
}
else {
$result = $DB->select_limit("{$query} ORDER BY RAND()", 2, 0, __FILE__, __LINE__);
}
$TMPL['reviews'] = '';
while (list($TMPL['id'], $TMPL['date'], $TMPL['review']) = $DB->fetch_array($result)) {
$TMPL['reviews'] .= $this->do_skin('stats_review');
}
$TMPL['content'] = $this->do_skin('stats');
}
else {
$this->error($LNG['g_invalid_u']);
}
}
}
class stats_overall extends stats {
function __construct() {
global $CONF, $DB, $FORM, $LNG, $TMPL;
$TMPL['header'] = $LNG['stats_overall'];
$stats = $DB->fetch("SELECT SUM(unq_pv_overall), SUM(tot_pv_overall), SUM(unq_in_overall), SUM(tot_in_overall), SUM(unq_out_overall), SUM(tot_out_overall),
SUM(unq_pv_0_daily), SUM(unq_pv_1_daily), SUM(unq_pv_2_daily), SUM(unq_pv_3_daily), SUM(unq_pv_4_daily), SUM(unq_pv_5_daily), SUM(unq_pv_6_daily), SUM(unq_pv_7_daily), SUM(unq_pv_8_daily), SUM(unq_pv_9_daily), SUM(tot_pv_0_daily), SUM(tot_pv_1_daily), SUM(tot_pv_2_daily), SUM(tot_pv_3_daily), SUM(tot_pv_4_daily), SUM(tot_pv_5_daily), SUM(tot_pv_6_daily), SUM(tot_pv_7_daily), SUM(tot_pv_8_daily), SUM(tot_pv_9_daily),
SUM(unq_in_0_daily), SUM(unq_in_1_daily), SUM(unq_in_2_daily), SUM(unq_in_3_daily), SUM(unq_in_4_daily), SUM(unq_in_5_daily), SUM(unq_in_6_daily), SUM(unq_in_7_daily), SUM(unq_in_8_daily), SUM(unq_in_9_daily), SUM(tot_in_0_daily), SUM(tot_in_1_daily), SUM(tot_in_2_daily), SUM(tot_in_3_daily), SUM(tot_in_4_daily), SUM(tot_in_5_daily), SUM(tot_in_6_daily), SUM(tot_in_7_daily), SUM(tot_in_8_daily), SUM(tot_in_9_daily),
SUM(unq_out_0_daily), SUM(unq_out_1_daily), SUM(unq_out_2_daily), SUM(unq_out_3_daily), SUM(unq_out_4_daily), SUM(unq_out_5_daily), SUM(unq_out_6_daily), SUM(unq_out_7_daily), SUM(unq_out_8_daily), SUM(unq_out_9_daily), SUM(tot_out_0_daily), SUM(tot_out_1_daily), SUM(tot_out_2_daily), SUM(tot_out_3_daily), SUM(tot_out_4_daily), SUM(tot_out_5_daily), SUM(tot_out_6_daily), SUM(tot_out_7_daily), SUM(tot_out_8_daily), SUM(tot_out_9_daily),
SUM(unq_pv_0_weekly), SUM(unq_pv_1_weekly), SUM(unq_pv_2_weekly), SUM(unq_pv_3_weekly), SUM(unq_pv_4_weekly), SUM(unq_pv_5_weekly), SUM(unq_pv_6_weekly), SUM(unq_pv_7_weekly), SUM(unq_pv_8_weekly), SUM(unq_pv_9_weekly), SUM(tot_pv_0_weekly), SUM(tot_pv_1_weekly), SUM(tot_pv_2_weekly), SUM(tot_pv_3_weekly), SUM(tot_pv_4_weekly), SUM(tot_pv_5_weekly), SUM(tot_pv_6_weekly), SUM(tot_pv_7_weekly), SUM(tot_pv_8_weekly), SUM(tot_pv_9_weekly),
SUM(unq_in_0_weekly), SUM(unq_in_1_weekly), SUM(unq_in_2_weekly), SUM(unq_in_3_weekly), SUM(unq_in_4_weekly), SUM(unq_in_5_weekly), SUM(unq_in_6_weekly), SUM(unq_in_7_weekly), SUM(unq_in_8_weekly), SUM(unq_in_9_weekly), SUM(tot_in_0_weekly), SUM(tot_in_1_weekly), SUM(tot_in_2_weekly), SUM(tot_in_3_weekly), SUM(tot_in_4_weekly), SUM(tot_in_5_weekly), SUM(tot_in_6_weekly), SUM(tot_in_7_weekly), SUM(tot_in_8_weekly), SUM(tot_in_9_weekly),
SUM(unq_out_0_weekly), SUM(unq_out_1_weekly), SUM(unq_out_2_weekly), SUM(unq_out_3_weekly), SUM(unq_out_4_weekly), SUM(unq_out_5_weekly), SUM(unq_out_6_weekly), SUM(unq_out_7_weekly), SUM(unq_out_8_weekly), SUM(unq_out_9_weekly), SUM(tot_out_0_weekly), SUM(tot_out_1_weekly), SUM(tot_out_2_weekly), SUM(tot_out_3_weekly), SUM(tot_out_4_weekly), SUM(tot_out_5_weekly), SUM(tot_out_6_weekly), SUM(tot_out_7_weekly), SUM(tot_out_8_weekly), SUM(tot_out_9_weekly),
SUM(unq_pv_0_monthly), SUM(unq_pv_1_monthly), SUM(unq_pv_2_monthly), SUM(unq_pv_3_monthly), SUM(unq_pv_4_monthly), SUM(unq_pv_5_monthly), SUM(unq_pv_6_monthly), SUM(unq_pv_7_monthly), SUM(unq_pv_8_monthly), SUM(unq_pv_9_monthly), SUM(tot_pv_0_monthly), SUM(tot_pv_1_monthly), SUM(tot_pv_2_monthly), SUM(tot_pv_3_monthly), SUM(tot_pv_4_monthly), SUM(tot_pv_5_monthly), SUM(tot_pv_6_monthly), SUM(tot_pv_7_monthly), SUM(tot_pv_8_monthly), SUM(tot_pv_9_monthly),
SUM(unq_in_0_monthly), SUM(unq_in_1_monthly), SUM(unq_in_2_monthly), SUM(unq_in_3_monthly), SUM(unq_in_4_monthly), SUM(unq_in_5_monthly), SUM(unq_in_6_monthly), SUM(unq_in_7_monthly), SUM(unq_in_8_monthly), SUM(unq_in_9_monthly), SUM(tot_in_0_monthly), SUM(tot_in_1_monthly), SUM(tot_in_2_monthly), SUM(tot_in_3_monthly), SUM(tot_in_4_monthly), SUM(tot_in_5_monthly), SUM(tot_in_6_monthly), SUM(tot_in_7_monthly), SUM(tot_in_8_monthly), SUM(tot_in_9_monthly),
SUM(unq_out_0_monthly), SUM(unq_out_1_monthly), SUM(unq_out_2_monthly), SUM(unq_out_3_monthly), SUM(unq_out_4_monthly), SUM(unq_out_5_monthly), SUM(unq_out_6_monthly), SUM(unq_out_7_monthly), SUM(unq_out_8_monthly), SUM(unq_out_9_monthly), SUM(tot_out_0_monthly), SUM(tot_out_1_monthly), SUM(tot_out_2_monthly), SUM(tot_out_3_monthly), SUM(tot_out_4_monthly), SUM(tot_out_5_monthly), SUM(tot_out_6_monthly), SUM(tot_out_7_monthly), SUM(tot_out_8_monthly), SUM(tot_out_9_monthly)
FROM {$CONF['sql_prefix']}_stats", __FILE__, __LINE__);
// Get rid of SUM() in array keys
foreach ($stats as $key => $value) {
$new_key = str_replace(array('SUM(', ')'), '', $key);
$stats[$new_key] = $value;
unset($stats[$key]);
}
$TMPL = array_merge($TMPL, $stats);
$this->averages();
$stats = array_map('number_format', $stats);
$TMPL = array_merge($TMPL, $stats);
$this->locale();
$TMPL['content'] = $this->do_skin('stats_overall');
}
}
?>
Error happen at line 85:
$stats = array_map('number_format', $stats);
Latest Error after using Miken32's Answer:
PHP Notice: Undefined index: title in sources/stats.php on line 91
PHP Notice: Undefined index: category in sources/stats.php on line 92
PHP Notice: Undefined index: title in sources/misc/skin.php(84) : runtime-created function on line 1
PHP Notice: Undefined index: url in sources/misc/ etc.
array_map() applies a function to each element of the array. In this case you're passing it the name of a function ("number_format") that expects a number but it's getting a string. Instead, you can pass it your own function that can check it:
$stats = array_map(function($v){return is_numeric($v) ? number_format($v) : $v;}, $stats);
By default numeric types from MySQL are returned as string values in php. The easiest thing to do would be create a custom function and pass that in:
function formatter($value) { return number_format(floatval($value)); }

Fatal error: Call to a member function query() on a non-object in /var/www/likeslomakkeet/controllers/petitions_controller.php on line 33

I'm getting:
Fatal error: Call to a member function query() on a non-object in /var/www/likeslomakkeet/controllers/petitions_controller.php on line 33
with the code below. I'm not sure why as I have also defined $name correctly in the model with var $name = 'Petition';
The error comes out when I put custom query.
Anyone has any suggestions?
function index() {
if ($this->Session->read('Auth.User.group_id') != 1) {
$commune_id = $this->Session->read('Auth.User.commune_id');
$commune_id = $this->Petition->Commune->findbyId($commune_id);
$commune_id = $this->Petition->Commune->find('all',array('conditions' => array('group' => $commune_id['Commune']['group'])));
debug($commune_id);
$count = count($commune_id);
$i=1;
$sql = "SELECT * FROM `petitions` WHERE `commune_id` = ";
foreach($commune_id as $commune_ids){
if($i==1){
$sql .= $commune_ids['Commune']['id'];
}else{
$sql .= " OR `commune_id` = ".$commune_ids['Commune']['id'];
}
/*if($i != $count){
$this->paginate = array(
'or' => array(
array('Petition.commune_id LIKE' => $commune_ids['Commune']['id'] . ","),
//array('Petition.commune_id LIKE' => "," . $commune_ids['Commune']['id'] . ",")
),
'limit' => 10
);
}*/
$i++;
}
$query = $this->Petiton->query($sql);
debug($query);
}
$this->Petition->recursive = 0;
$petitions = $this->paginate();
$this->set('petitions', $petitions);
}
I solved my problem like this line:
$this->loadModel('Petiton');
I have one:
$this->Petiton
^^^
Add an i.

Categories