Does not work perfectly for Online User in WordPress - php

I am making a classified website in wordpress with classipress theme,
http://www.mohibsalahuddin.com/client/ads/test-ad/
for showing author is online or offline on ad page, i wrote the following code, but it does not work if two users registered at a time... If registered user wants to see another registered user's status it always shows offline...
I though may be i missed session start thing do i have to write it? Kindly help me in this
I write the code below
First I create the table in MYSQL
CREATE TABLE wordpress_users_online` (
user_id int(11) default NULL,
full_name varchar(64) NOT NULL default '',
session_id varchar(128) NOT NULL default '',
ip_address varchar(15) NOT NULL default '',
time_entry varchar(14) NOT NULL default '',
time_last_click varchar(14) NOT NULL default '',
last_page_url varchar(255) NOT NULL default ''
) ENGINE=MyISAM;`
THEN I WRITE THE FILE wordpress_users_online.php with following code
<?php
define('TABLE_USERS', 'wordpress_users');
define('TABLE_USER_ONLINE', 'wordpress_users_online');
function update_user_online() {
//global $user_id;
global $current_user;
get_currentuserinfo();
if ($current_user->ID && $current_user->user_login) {
$wo_user_id = $current_user->ID;
$wo_full_name = $user['user_nicename'];
} else {
$wo_user_id = '';
$wo_full_name = 'Guest';
}
$wo_session_id = session_id();
$wo_ip_address = getenv('REMOTE_ADDR');
$wo_last_page_url = getenv('REQUEST_URI');
$current_time = time();
$xx_mins_ago = ($current_time - 300);
// remove entries that have expired
mysql_query("delete from " . TABLE_USER_ONLINE . " where time_last_click < '" . $xx_mins_ago . "'");
$stored_user_query = mysql_query("select count(*) as count from " . TABLE_USER_ONLINE . " where session_id = '" . mysql_escape_string($wo_session_id) . "'");
$stored_user = mysql_fetch_array($stored_user_query);
if ($stored_user['count'] > 0) {
mysql_query("update " . TABLE_USER_ONLINE . " set user_id = '" . (int)$wo_user_id . "', full_name = '" . mysql_escape_string($wo_full_name) . "', ip_address = '" . mysql_escape_string($wo_ip_address) . "', time_last_click = '" . mysql_escape_string($current_time) . "', last_page_url = '" . mysql_escape_string($wo_last_page_url) . "' where session_id = '" . mysql_escape_string($wo_session_id) . "'");
} else {
mysql_query("insert into " . TABLE_USER_ONLINE . " (user_id, full_name, session_id, ip_address, time_entry, time_last_click, last_page_url) values ('" . (int)$wo_user_id . "', '" . mysql_escape_string($wo_full_name) . "', '" . mysql_escape_string($wo_session_id) . "', '" . mysql_escape_string($wo_ip_address) . "', '" . mysql_escape_string($current_time) . "', '" . mysql_escape_string($current_time) . "', '" . mysql_escape_string($wo_last_page_url) . "')");
}
}
function is_online($user_id) {
$stored_user_query = mysql_query("select user_id from ".TABLE_USER_ONLINE." where user_id = '" . mysql_escape_string($user_id) . "'");
$stored_user = mysql_fetch_array($stored_user_query);
if(isset($stored_user['user_id'])){
$online = '<img src="http://www.mohibsalahuddin.com/client/wp-content/themes/classipress-313/images/online.png" border="0" /> Online';
echo $online;
}
else {
$online = '<img src="http://www.mohibsalahuddin.com/client/wp-content/themes/classipress-313/images/offline.png" border="0" /> Offline';
echo $online;
}
return $online;
}
?>

There's actually a wordpress plugin for this already...
--Edit--
http://wordpress.org/extend/plugins/wp-useronline/
Sorry the following was the original suggestions but they were the wrong links:
http://wordpress.org/extend/plugins/who-is-online/faq/
http://wordpress.org/extend/plugins/wassup/
Remember, don't re-invent the wheel unless you're going to make it hover too.

Related

Opencart: Error No:1062 Duplicate entry '1' for key 'PRIMARY'

I'm developing a carousel extension for opencart that has Item ID, Item Name, Link, Image, Sort Order. The carousel had an issue while saving it will change the item id so I had to modify the code to get fixed ID queryed to the DB, however now I get the 1062 Error.
Notice: Error: Duplicate entry '1' for key 'PRIMARY' Error No: 1062
INSERT INTO crousal SET crousal_id = '1', name = 'Baby & Toys', link =
'/index.php?route=product/product&product_id=7570', image =
'data/carousel/banner2.jpg', sort_order = '0' in
/home/user/public_html/system/database/mysql.php on line 49
Sharing the model/carousel.php editCarousel Function
public function editCrousal($crousal_image) {
$crousal_id = $this->db->getLastId();
$this->db->query("DELETE FROM " . DB_PREFIX . "mobiapp_crousal WHERE crousal_id = '" . (int)$crousal_id . "'");
if (isset($crousal_image['crousal_image'])) {
foreach ($crousal_image['crousal_image'] as $crousal_image) {
$this->db->query("INSERT INTO " . DB_PREFIX . "mobiapp_crousal SET crousal_id = '" . (int)$crousal_id . "', name = '" . $this->db->escape($crousal_image['name']) . "', link = '" . $this->db->escape($crousal_image['link']) . "', image = '" . $this->db->escape($crousal_image['image']) . "', sort_order = '" . (int)$crousal_image['sort_order'] . "'");
$crousal_id = $this->db->getLastId();
}
}
}
Any advice why I'm getting this error and how to resolve, thank you in advanced.
P.S.: Opencart 1.5.x
Edited: DB Structure & Module info
$this->db->query("
CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "mobiapp_crousal` (
`crousal_id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`link` VARCHAR(255) NOT NULL,
`image` VARCHAR(255) NOT NULL,
`sort_order` INT(3) NOT NULL,
PRIMARY KEY (`crousal_id`)
) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
Model File
public function addCrousal($crousal_image) {
$this->db->query("INSERT INTO " . DB_PREFIX . "mobiapp_crousal SET name = '" . $this->db->escape($crousal_image['name']) . "', sort_order = '" . (int)$crousal_image['sort_order'] . "'");
$crousal_id = $this->db->getLastId();
if (isset($crousal_image['crousal_image'])) {
foreach ($crousal_image['crousal_image'] as $crousal_image) {
$this->db->query("INSERT INTO " . DB_PREFIX . "mobiapp_crousal SET crousal_id = '" . (int)$crousal_id . "', link = '" . $this->db->escape($crousal_image['link']) . "', image = '" . $this->db->escape($crousal_image['image']) . "'");
$crousal_id = $this->db->getLastId();
}
}
}
public function editCrousal($crousal_image) {
$crousal_id = $this->db->getLastId();
$this->db->query("DELETE FROM " . DB_PREFIX . "mobiapp_crousal WHERE crousal_id = '" . (int)$crousal_id . "'");
if (isset($crousal_image['crousal_image'])) {
foreach ($crousal_image['crousal_image'] as $crousal_image) {
$this->db->query("INSERT INTO " . DB_PREFIX . "mobiapp_crousal SET crousal_id = '" . (int)$crousal_id . "', name = '" . $this->db->escape($crousal_image['name']) . "', link = '" . $this->db->escape($crousal_image['link']) . "', image = '" . $this->db->escape($crousal_image['image']) . "', sort_order = '" . (int)$crousal_image['sort_order'] . "'");
$crousal_id = $this->db->getLastId();
}
}
}
public function getCrousalImages() {
$crousal_image_data = array();
$crousal_image_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "mobiapp_crousal ORDER BY sort_order ASC");
foreach ($crousal_image_query->rows as $crousal_image) {
$crousal_image_description_data = array();
$crousal_image_description_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "mobiapp_crousal WHERE crousal_id = '" . (int)$crousal_image['crousal_id'] . "'");
$crousal_image_data[] = array(
'crousal_image' => $crousal_image,
'link' => $crousal_image['link'],
'name' => $crousal_image['name'],
'crousal_id' => $crousal_image['crousal_id'],
'image' => $crousal_image['image'],
'sort_order' => $crousal_image['sort_order']
);
}
return $crousal_image_data;
}
I did review for this module. Seems like this module has totally wrong structure. As you can see in method addCrousal foreach-statement with insertion to database. But in this insert carousal_id not incrementing so in the table will be records with not unique carousal_id. So this column can't be PRIMARY.
I suggest you to rewrite module with two tables "Slides" with relations (has many) "Images". Or you can remove PRIMARY index and operation will pass but it's not true solution.

PHP can't read/receive response

So what I am trying to do is receiving data about a player from the Steam API.
Basically, a player logs in via the Steam website and then gets redirected to a page of my website where data about the player is registered into the database.
It seems that the data about the player (which is being retrieved from the Steam API) is null (it cant be retrieved)
When I try to access the API in the browser, it works just fine.
Here is the code for registering the data:
$url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&steamids=". $matches[1];
$json_object = file_get_contents($url);
$json_decoded = json_decode($json_object);
foreach ($json_decoded->response->players as $player) {
$steamid = $player->steamid;
$name = $player->personaname;
$avatar = $player->avatar;
}
$hash = md5($steamid . time() . rand(1, 50));
$sql = $db->query("SELECT * FROM `users` WHERE `steamid` = '" . $steamid . "'");
$row = $sql->fetchAll(PDO::FETCH_ASSOC);
if (count($row) == 0) {
$db->exec("INSERT INTO `users` (`hash`, `steamid`, `name`, `avatar`) VALUES ('" . $hash . "', '" . $steamid . "', " . $db->quote($name) . ", '" . $avatar . "')");
} else {
$db->exec("UPDATE `users` SET `hash` = '" . $hash . "', `name` = " . $db->quote($name) . ", `avatar` = '" . $avatar . "' WHERE `steamid` = '" . $steamid . "'");
}
If you want to try the API out your self, copy the $url and replace $matches[1] with this steam id: 76561198117114460
Thanks in advance,
Guus Huizen

how to check duplicate records in mysql database

$queryb = "SELECT product, imei, country, warranty, config from PRODUCT WHERE product_slno = '$mnserialno' ";
$resultb = mysql_query($queryb, $gndbconn) ;
if(mysql_num_rows($resultb) > 0)
{
$queryc = "UPDATE PRODUCT SET product='$desc', product_slno='$mnserialno',imei='$imei',country='$country',warranty='$warranty',config='$config' WHERE product_slno = '.$mnserialno.' ";
$resutc = mysql_query($queryc, $gndbconn) ;
}
else{
$querya = "INSERT INTO PRODUCT SET product='$desc', product_slno='$mnserialno',imei='$imei',country='$country',warranty='$warranty',config='$config'";
$resulta = mysql_query($querya, $gndbconn) ;
}
I want to check the serial number if that serial number already exist in database so records get update, otherwise it get insert into the database.
but the code inserting the records only, no updation, what is the fault i am not getting,
how to prevent the duplicate entry?
INSERT INTO PRODUCT SET
(`product`, `product_slno`, `imei`, `country`, `warranty`, `config`)
VALUES
('" . $desc . "', '" . $mnserialno . "', '" . $imei . "', '" . $country . "', '" . $warranty . "', '" . $config . "')
ON DUPLICATE KEY UPDATE
product='" . $desc . "',
product_slno='" . $mnserialno . "',
imei='" . $imei . "',
country='" . $country . "',
warranty='" . $warranty . "',
config='" . $config . "'";

Opencart Undefined in model file?

Hey Guys I am trying to pass data to my model, but for some reason I keep getting an "undefined customitem_id" in my model file. I am testing to see if it will even send to the model so:
the code is as follows. My controller file from customer.php file
$data['customitem_id']= 19;
if(isset($this->request->post['customitem_id'])) {
$this->request->post['customitem_id'];
}
My code from:
public function editCustomer($customer_id, $data) {
if (!isset($data['custom_field'])) {
$data['custom_field'] = array();
}
$this->db->query("UPDATE " . DB_PREFIX . "customer SET customer_group_id = '" . (int)$data['customer_group_id'] . "', sales_representative = '" . $this->db->escape($data['username']) . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', fax = '" . $this->db->escape($data['fax']) . "', custom_field = '" . $this->db->escape(isset($data['custom_field']) ? serialize($data['custom_field']) : '') . "', newsletter = '" . (int)$data['newsletter'] . "', status = '" . (int)$data['status'] . "', approved = '" . (int)$data['approved'] . "', safe = '" . (int)$data['safe'] . "' WHERE customer_id = '" . (int)$customer_id . "'");
$this->db->query("UPDATE " . DB_PREFIX . "custom_item SET customer_id = '" . (int)$customer_id . "' WHERE customitem_id = '" . (int)$customitem_id . "'");
it keeps giving me an undefined variable in the model file. How would I go about making sure it sends the data?
Thanks for your help.
Looks like you're passing an array $data to your editCustomer($customer_id, $data) right?
Try changing this from:
$this->db->query("UPDATE " . DB_PREFIX . "custom_item SET customer_id = '" . (int)$customer_id . "' WHERE customitem_id = '" . (int)$customitem_id . "'")
to
$this->db->query("UPDATE " . DB_PREFIX . "custom_item SET customer_id = '" . (int)$customer_id . "' WHERE customitem_id = '" . (int)$data['customitem_id'] . "'")
note that I changed (int)$customitem_id to (int)$data['customitem_id']
You're using variable $customitem_id in your model but in your controller it is $data['customitem_id']. You likely just need to change $customitem_id to $data['customitem_id'] in your model.

Can PHP foreach statements have if/else inside them? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am doing an OpenCart modification to an order total. This is the snippet of the code:
<?php
foreach ($data['totals'] as $total) {
if (
$this->db->escape($total['code'])=="sub_total" ||
$this->db->escape($total['title'])="Sub-Total"
) {
$this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape($total['code']) . "', title = '" . $this->db->escape($total['title']) . "', text = '" . $this->db->escape($total['text']) . "', `value` = '" . (float)$data['total'] . "', sort_order = '" . (int)$total['sort_order'] . "'");
} else {
$this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape($total['code']) . "', title = '" . $this->db->escape($total['title']) . "', text = '" . $this->db->escape($total['text']) . "', `value` = '" . $_SESSION['GCFinalOrderTotalIncludingDelivery'] . "', sort_order = '" . (int)$total['sort_order'] . "'");
}
}
?>
This is the pseudo code. Can an IF/Else be nested inside the foreach statement?
<?php
foreach ($d['totals'] as $total) {
if ($query=="x" || $query=="y") {
a();
} else {
b();
}
}
?>
Sure they can.
And it is quite hard to use 30 characters to say that.
Sure, it can. Not clear what is your problem, but suppose you have a typo in this line:
$this->db->escape($total['title']) = "Sub-Total"
which should be like this:
$this->db->escape($total['title']) == "Sub-Total"
I think you know by now that: "Yes, they can!". However: if your loop isn't that big, you might want to rethink what the actual difference is between the two branches. Take your pseudo-code example, for instance: if the branch is decided on depending on a value that is not derived from the variables provided by the loop itself, you're better of moving the branch:
foreach($someArr as $k=>$v)
{
if ($someOtherVar === true)
{
echo 'The value is '.$v;
}
else
{
echo 'The key is '.$k;
}
}
Can (and should) be written as:
$str = 'The '.($someOtherVar === true ? 'value' : 'key').' is ';
foreach($someArr as $k=>$v)
{
echo $str.$v;
}
Yes you can do this. It is pretty commonly done too. Make sure you use == when testing though; you have = as your test.
In OpenCart the file you're trying to edit I assume is the Order Model?
/catalog/model/checkout/order.php
You have a problem with missing two == which was mentioned by FAngel but the problem lies with your float. The data attribute gets taken away in this function. Because the Data array is no longer inside that snippet. It relies on TOTAL:
So your line:
$this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape($total['code']) . "', title = '" . $this->db->escape($total['title']) . "', text = '" . $this->db->escape($total['text']) . "', `value` = '" . (float)$data['total'] . "', sort_order = '" . (int)$total['sort_order'] . "'");
should be: (notice the NEW (float)$total['value'])
$this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape($total['code']) . "', title = '" . $this->db->escape($total['title']) . "', text = '" . $this->db->escape($total['text']) . "', `value` = '" . (float)$total['value'] . "', sort_order = '" . (int)$total['sort_order'] . "'");
Which means it would be:
#######
// WAS (float)$data['value']
// NOW (float)$total['value']
#######
foreach ($data['totals'] as $total) {
if (
$this->db->escape($total['code'])=="sub_total" ||
$this->db->escape($total['title'])=="Sub-Total"
) {
$this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape($total['code']) . "', title = '" . $this->db->escape($total['title']) . "', text = '" . $this->db->escape($total['text']) . "', `value` = '" . (float)$total['value'] . "', sort_order = '" . (int)$total['sort_order'] . "'");
} else {
$this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape($total['code']) . "', title = '" . $this->db->escape($total['title']) . "', text = '" . $this->db->escape($total['text']) . "', `value` = '" . $_SESSION['GCFinalOrderTotalIncludingDelivery'] . "', sort_order = '" . (int)$total['sort_order'] . "'");
}
}
Yes absolutely.Else what would have been the point in having a Programing Language.

Categories