Joomla's Who's Online Counter - How to add thousand separator? - php

One of my sites frequently has more than 1000 concurrent visitors and just for consistency I want to add a thousands separator to the display so it shows like 1,000.
My initial thought was just to add number_format before the variable holding the guest count but this stops the counter working for some reason.
The function in helper.php counting the guests looks like this:
// show online count
static function getOnlineCount() {
$db = JFactory::getDbo();
// calculate number of guests and users
$result = array();
$user_array = 0;
$guest_array = 0;
$query = $db->getQuery(true);
$query->select('guest, usertype, client_id');
$query->from('#__session');
$query->where('client_id = 0');
$db->setQuery($query);
$sessions = (array) $db->loadObjectList();
if (count($sessions)) {
foreach ($sessions as $session) {
// if guest increase guest count by 1
if ($session->guest == 1 && !$session->usertype) {
$guest_array ++;
}
// if member increase member count by 1
if ($session->guest == 0) {
$user_array ++;
}
}
}
$result['user'] = $user_array;
$result['guest'] = $guest_array;
return $result;
}
And in the template the data is displayed using the following:
<?php if ($showmode == 0 || $showmode == 2) : ?>
<?php $guest = JText::plural('MOD_WHOSONLINE_GUESTS', $count['guest']); ?>
<?php $member = JText::plural('MOD_WHOSONLINE_MEMBERS', $count['user']); ?>
<p><?php echo JText::sprintf('MOD_WHOSONLINE_WE_HAVE', $guest, $member); ?></p>
Where do I put the number_format so the separator is added please?

does this not work?
$guest = JText::plural('MOD_WHOSONLINE_GUESTS',number_format($count['guest'],0,'.',','));

Related

Is there a way to update 12000+ rows from txt file in less then 2mins?

I need to update a table with more then 12000 row using php Codeigniter and a txt file.. reading the file and the foreach loop are fine but when updating line by line it takes like 30 mins, I guess the problem is I'm searching by name because I have no id in the txt file...
Here is my code:
controller:
$fn = fopen($this->upload->data('full_path'),"r");
$update = true;
while(! feof($fn) && $update) {
$pieces = explode("|", fgets($fn));
if(sizeof($pieces) == 9 && is_numeric(trim($pieces[1]))) {
$update = $this->model_products->update3s($pieces);
}
}
fclose($fn);
Model:
public function update3s($product) {
if ($product) {
$product[2] = trim(str_replace("'","''",$product[2]));
$product[1] = trim($product[1]);
$product[6] = trim($product[6]);
$product[3] = trim($product[3]);
$sql = "UPDATE products set qty = $product[3], price_vente = $product[6] where (name = '$product[2]')";
echo $sql.'<br>';
$update = $query = $this->db->query($sql);
return $update;
}
return false;
}
You can use transaction and add index for column name in database table.
$fn = fopen($this->upload->data('full_path'),"r");
$update = true;
$updatedCount = 0;
while(! feof($fn) && $update) {
$pieces = explode("|", fgets($fn));
if(sizeof($pieces) == 9 && is_numeric(trim($pieces[1]))) {
if ($updatedCount == 0) {
$databaseInstance->beginTransaction();
}
$update = $this->model_products->update3s($pieces);
++$updatedCount;
if ($updatedCount > 500) { //in one transaction update 500 rows
$databaseInstance->commit();
$updatedCount = 0;
}
}
}
if ($updatedCount > 0) { // if we have not commited transaction
$databaseInstance->commit();
}
fclose($fn);
Some tips
Add index to field name
Use prepared statements
Disable the MySQL forgeign key check Read more
writing sql function can do that even in much lesser time .
using feature like :
REPLACE()
cursors
SPLIT_STRING(custom)
in a mysql user defined function
CREATE FUNCTION update3s(hole_file_content LONGTEXT) RETURNS Boolean
BEGIN
-----Your implementation(same logic in sql ) ------
END
then coll it just by if it is CI 3
$this->db->call_function('update3s', file_get_contents($this->upload->data('full_path')));
else
$this->db->query("select update3s(".file_get_contents($this->upload->data('full_path')).")");

PHP - Error when trying to use ceiling function on data retrieved from database

Below is my PHP code. I'm tying to retrieve values from a database and round them to the nearest 10 (upwards only). All the values in the database in this column are integers.
<?PHP
#$Teach_ID = $_POST['txtteachID'];
#$Class_ID = $_POST['txtclass'];
#$BookingDate = $_POST['txtbookingdate'];
#$BookingPeriod = $_POST['txtperiod'];
require_once('../BookingSystem/DBconnect.php');
$capacity = 'SELECT ClassSize FROM classes WHERE ClassID = 1';
$result = $dbh->query($capacity);
$result = (int)$result;
function ceiling($number, $significance = 1)
{
return ( is_numeric($number) && is_numeric($significance) ) ? (ceil($number/$significance)*$significance) : false;
}
}
if ($result->num_rows > 0) {
echo ceiling($result, 10);
}
?>
Error Description
Am I missing something obvious?
You need to loop through your $result variable, which is a mysqli_result object, to get its different entries.
Use mysqli_fetch_assoc to get the values, which will end in something like this :
$capacity = 'SELECT ClassSize FROM classes WHERE ClassID = 1';
$result = $dbh->query($capacity);
function ceiling($number, $significance = 1)
{
return ( is_numeric($number) && is_numeric($significance) ) ? (ceil($number/$significance)*$significance) : false;
}
}
while ($row = $result->fetch_assoc()) {
// if ($result->num_rows > 0) { /* I think this condition is not needed anymore */
$value = intval($row['ClassSize'], 10); // will convert the string from database to an int in base 10
echo ceiling($value, 10); /* I suppose you wanted to use the ClassSize key since it's the one you query */
// }
}
You can't do this $result = (int)$result;. You have to irritate over each row and then extract the data.

Adding another User ID if that wasn't present in a string

I'm using this function;
function add_profile_visitor($add_uid,$to_uid,$visitors_list)
{
global $db;
$list = trim($visitors_list);
$list_users = explode(",",$list);
if (in_array($add_uid,$list_users))
{
return;
}
else
{
if (strpos($list_users,",") || strlen($list_users) > 0)
{
$newlist = $list_users.",".intval($add_uid);
}
else
{
$newlist = intval($add_uid);
}
$db->query("UPDATE users SET profile_visitor='".$db->escape_string($newlist)."' WHERE uid=".$to_uid);
}
}
I want to add a User ID if its not present already in the field profile_visitor. For your notice, the profile_visitor list is comma separated list. Here are the variable's legend:
$add_uid = The user id that is going to be inserted in profile_visitor if its not present in already.
$to_uid = The User ID where profile_visitor column is present and the $add_uid add in if its not present.
$visitors_list = The comma separated list of profile visitors. Before adding any User ID that list will be empty (obviously)
The issue is: Each time the page loads (where that function is running) the list profile_visitor has the new User ID instead of adding the new User ID to the list.
Please help
Change your function to this:
function add_profile_visitor($add_uid,$to_uid,$visitors_list)
{
global $db;
$list = trim($visitors_list);
$list_users = explode(",",$list);
if (in_array($add_uid,$list_users))
{
return;
}
else
{
if (strpos($list,",") || strlen($list) > 0)
{
$newlist = $list.",".intval($add_uid);
}
else
{
$newlist = intval($add_uid);
}
$db->query("UPDATE users SET profile_visitor='".$db->escape_string($newlist)."' WHERE uid=".$to_uid);
}
}

Total unpublished occurances across multiple tables using a loop and array

I have a function that gets the number of unpublished records from a table (which works fine). However now I want to get the total number of unpublished occurances in multiple tables. I figure I can do a foreach loop using the $tables = array('projects', 'testimonials') .etc. then add together their respective results to get total number and return that? However I am not sure how to do this, this kindof loop is a bit outside my abilities.
function publishCount($table) {
$sql = mysql_query("SELECT COUNT(*) AS nb FROM ".$table." WHERE published='0' OR published=''") or die(mysql_error());
$result = mysql_result($sql, 0);
if (!$result == 0) { echo 'Awaiting to be published <span class="badge badge-important">'.$result.'</span>'; }
else { echo 'Awaiting to be published <span class="badge badge-inverse">'.$result.'</span>'; }
}
You should edit your function this way:
function publishCount($table) {
$sql = mysql_query("SELECT COUNT(*) AS nb FROM ".$table." WHERE published='0' OR published=''") or die(mysql_error());
$result = mysql_result($sql, 0);
return $result;
}
So you can then do a loop through your $tables list:
$tables = array('projects', 'testimonials', [...]);
$nbResults = 0;
foreach($tables as $table) {
$nbResults += publishCount($table);
}
if($nbResults == 0) {
// ...
} else {
// ...
}

Printing the Categories and Sub Categories Alone

function build_list($id=0,$collapsed="") //return an array with the categories ordered by position
{
$RootPos = "";
$this->c_list = array();
if($id != 0){
$this_category = $this->fetch($id);
$positions = explode(">",$this_category['position']);
$RootPos = $positions[0];
}
// lets fetch the root categories
$sql = "SELECT *
FROM ".$this->table_name."
WHERE position RLIKE '^([0-9]+>){1,1}$' AND c_group = '".$this->Group."'
ORDER BY c_name";
$res = mysql_query($sql) or die(trigger_error("<br><storng><u>MySQL Error:</u></strong><br>".mysql_error()."<br><br><storng><u>Query Used:</u></strong><br>".$sql."<br><br><storng><u>Info:</u></strong><br>",E_USER_ERROR));
while($root = mysql_fetch_array($res)){
$root["prefix"] = $this->get_prefix($root['position']);
$this->c_list[$root['id']] = $root;
if($RootPos == $root['id'] AND $id != 0 AND $collapsed != ""){
$this->list_by_id($id);
continue;
}else{
// lets check if there is sub-categories
if($collapsed == "" AND $id==0){
$has_children = $this->has_children($root['position']);
if($has_children == TRUE) $this->get_children($root['position'],0);
}}}
return $this->c_list;
}
// He is the Author of the code...
Categories Class
Author: Shadi Ali
Now I want to just return the Categories and Sub Categories from the above code.
function browse() {
$categories = new categories;
$categories_list = $categories->build_list();
foreach($categories_list as $c)
{
return $c->$id;
}
}
The above code is not working.... can anyone help me out.
Here are two problems with the browse() function:
The return statement is inside the foreach loop. The statement will return one value for one of the items in the $categories-list (at most), and not continue to loop over the rest of the $categories-list.
The $id variable is never declared or initialised in return $c->$id, perhaps you meant to use $c['id'] or $c->id

Categories