How to connect joomla3 DB from external php file? - php

I have created external php file inside my module and there i have use some sql queries , first i have tried using php/mysql and it works , then i tried to make it convert to joomla style . but when i use joomla framework to db connection gives errors
Old code: FROM PHP
mysql_connect("localhost","root","");
mysql_select_db("1234");
$searchp=$_GET["term"];
$query=mysql_query("SELECT * FROM sltdb_cddir_content where title like '%".$searchp."%'AND categories_id=82 order by title ASC ");
$json=array();
while($display=mysql_fetch_array($query)){
$json[]=array(
'value'=> $display["title"],
'label'=>$display["title"]
);
}
echo json_encode($json);
New Code : JOOMLA3
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
define('JPATH_BASE', $_SERVER['DOCUMENT_ROOT'] . DS . '');
require_once (JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once (JPATH_BASE . DS . 'includes' . DS . 'framework.php');
require_once (JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php');
//create application
$mainframe = &JFactory::getApplication('site');
$db = JFactory::getDBO();
// Create a new query object.
$query = $db -> getQuery(true);
$searchp = $_GET["term"];
$query -> select($db -> quoteName(array('title')));
$query -> from($db -> quoteName('sltdb_cddir_content'));
$query -> where($db -> quoteName('title') . ' LIKE ' . $db -> quote('\'$searchp.%\''));
$query -> order('ordering ASC');
$db -> setQuery($query);
$json = array();
while ($display = mysql_fetch_array($query)) {
$json[] = array('value' => $display["title"], 'label' => $display["title"]);
}
echo json_encode($json);
once after converting to the code in joomla its given a error
*"mysql_fetch_array() expects parameter 1 to be resource, object given in "*
Please advice me where i have done incorrect .
EDIT 01
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
define('JPATH_BASE', $_SERVER['DOCUMENT_ROOT'] . DS . '');
require_once (JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once (JPATH_BASE . DS . 'includes' . DS . 'framework.php');
require_once (JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php');
//create application
$mainframe = &JFactory::getApplication('site');
$searchp = $_GET["term"];
$db = JFactory::getDBO();
// Create a new query object.
$query = $db -> getQuery(true);
$query -> select($db -> quoteName(array('title')));
$query -> from($db -> quoteName('sltdb_cddir_content'));
$query -> where($db -> quoteName('title') . ' LIKE ' . $db -> quote('\'$searchp.%\''));
$query->where($db->quoteName('categories_id')." = ".$db->quote(82));
$query -> order('ordering ASC');
$db->setQuery($query);
$results = $db-> loadAssocList();
$json = array();
foreach($results as $json_result) {
$json[] = array('value' => $json_result["title"], 'label' => $json_result["title"]) ;
}
echo json_encode($json);

You are mixing using Joomla's "SDK" with pure PHP methods for data access, so I would recommend stay away from that mix:
1) mysql_fetch_array its going to be depreacated, and indeed it expects the 1st parameter to be a mysql connection created with a mysql_connect call, that's why the error says that the 1st parameter expects to be a resource, you can check the documentation in here http://www.php.net/mysql_fetch_array.
2) Since you are using Data Access classes from joomla to obtain the connection and build the query, which is fine, I would recommend keep using it for obtaining the results and looping through them, like this.
// Reset the query using our newly populated query object.
$db->setQuery($query);
// Load the results as a list of associated arrays.
$results = $db-> loadAssocList();
$json = array();
foreach($results as $json_result) {
$json[] = array('value' => $json_result["title"], 'label' => $json_result["title"])
}
More about Joomla's DB access here: http://docs.joomla.org/Selecting_data_using_JDatabase

Made a few tweaks to your database query and replaced $_GET with the correct Joomla method
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
define('JPATH_BASE', $_SERVER['DOCUMENT_ROOT'] . DS . '');
require_once (JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once (JPATH_BASE . DS . 'includes' . DS . 'framework.php');
require_once (JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php');
$app = JFactory::getApplication();
$jinput = $app->input;
$db = JFactory::getDbo();
$searchp = $jinput->get('term', null, null);
$id = 82;
$query = $db->getQuery(true);
$query->select($db->quoteName('title'))
->from($db->quoteName('sltdb_cddir_content'))
->where($db->quoteName('title') . ' LIKE ' . $db->quote($searchp))
->where($db->quoteName('categories_id') . ' = ' . $db->quote((int) $id))
->order('ordering ASC');
$db->setQuery($query);
Let me know if it works

Use
echo $query->dump();
To see what the generated query looks like and you can then test the query directly.
You need to connect to the database ... is it the same or different? If it is the same you can do
// Creating the database connection.
$this->db = JDatabase::getInstance(
array(
'driver' => $config->dbtype,
'host' => $config->host,
'user' => $config->user,
'password' => $config->password,
'database' => $config->db,
'prefix' => $config->dbprefix,
)
);
But if it is a different databas you'll need to supply the connection data in some other way.

Related

Getting error using $this when not in object context to all methods in a class except __construct() method

I have two classes in separate files. The names of classes and files are: Substitute.php and Models.php. I have some properties in class Substitute.php which I can access using $this->property_name in the __construct() method without any error. However when I try to access the same property in the another method(method test() in my case(see the code below)) in the same class (i.e., class Substitute) I get the error Fatal error: Uncaught Error: Using $this when not in object context in G:\ROHAN\eTh0\VectorVolunteers\vector\backend\models\Substitute.php:22. Line 22 being echo $this->c_day; in the method sss() (I have cut some of the code in the class, adding only the which I thought to be relevant, I'll add rest of the code in case someone requires). I access methods sss() and test() from the url by autoloader method and class Router which I have included below too:
Below this line lies the code inside Substitute.php:
EDIT1: I have added another method from Substitute class where also I get the same error.
class Substitute extends Models{
public $user_id, $c_day, $c_date;
public function __construct(){
$_SESSION['username']='user2';
echo 'constructor in class Substitute ran';
$this->user_id = $this->get_user_id();
$this->c_day = $this->upcoming_class();
}
public function sss(){
echo $this->c_day;
}
public function test(){
if(isset($_POST['select_class_subs'])){
switch ($_POST['select_class_subs']) {
case 'Next Class':
$this->c_date = date('d-M-yy', strtotime(($this->c_day)));
break;
case 'Next-to-next Class':
$this->c_date = date('d-M-yy', strtotime(($this->c_day . '+1 week')));
break;
default:
echo 'Custom class selected <br>';
break;
}
if($this->c_date==date('d-M-yy')){
echo "Sorry, but you cannot post a substitute request for a class that is scheduled to happen on the same day. <br>";
}
else{
echo "Done! <br>";
}
}
}
EDIT2: The code in the file config.php which is required in the autoloader() method(See below):
if(isset($_GET['url'])){$url = explode('/', $_GET['url']);}
require_once (ROOT . DS . 'backend' . DS . 'bootstrap.php');
EDIT2: The code of file bootstrap.php which has autloader() method:
require_once 'config.php';
// Autoloader for classes
spl_autoload_register('autoloader');
function autoloader($class_Name){
if (file_exists (ROOT . DS . 'backend' . DS .'core' . DS . $class_Name . '.php')){
require_once (ROOT . DS . 'backend' . DS . 'core' . DS . $class_Name . '.php');
}
elseif (file_exists (ROOT . DS . 'backend' . DS .'models' . DS . $class_Name . '.php')){
require_once (ROOT . DS . 'backend' . DS . 'models' . DS . $class_Name . '.php');
}
elseif (file_exists(ROOT . DS . 'backend' . DS .'views' . DS . $class_Name . '.php')){
require_once (ROOT . DS . 'backend' . DS . 'views' . DS . $class_Name . '.php');
}
elseif (file_exists(ROOT . DS . 'backend' . DS .'controllers' . DS . $class_Name . '.php')){
require_once (ROOT . DS . 'backend' . DS . 'controllers' . DS . $class_Name . '.php');
}
elseif (file_exists(ROOT . DS . 'backend' . DS .'database' . DS . $class_Name . '.php')) {
require_once (ROOT . DS . 'backend' . DS .'database' . DS . $class_Name . '.php');
}
elseif (file_exists(ROOT . DS . 'backend' . DS .'reminders' . DS . $class_Name . '.php')) {
require_once (ROOT . DS . 'backend' . DS .'reminders' . DS . $class_Name . '.php');
}
else {echo 'Class does not exist or Class file not found' . '<br>';}
}
// Route the request to router.php
if (isset($_GET['url'])) {
Router::route($url);
}
EDIT2: Code in the file that contains class Router{}:
class Router{
private static $controller_name, $method_name;
public static function route($url){
if (isset($url[0]) && $url[0] != "") {
$controller = ucwords($url[0]);
self::$controller_name = $controller;
array_shift($url);
}
if (isset($url[0]) && $url[0] != "") {
$method = ucwords($url[0]);
self::$method_name = $method;
array_shift($url);
}
$params = $url;
$init = new self::$controller_name;
if (method_exists(self::$controller_name, self::$method_name)){
call_user_func_array([self::$controller_name, self::$method_name], $params);
}
else{
if(self::$method_name!=NULL){
echo 'The requested method "' . self::$method_name . '" does not exist in ' . '"' . self::$controller_name . '" controller.';
}
}
}
Below this lies the code inside Models.php (I have included it in case someone requires. But know that I have extended this class to many other classes and all of the methods in this class work fine without any errors or warning).
class Models extends Database {
private $db, $user_id, $table, $rv_id_table_name, $av_id_table_name, $class_day;
protected function get_user_id(){
if (isset($_SESSION['username'])){
$username=$_SESSION['username'];
$sql = 'SELECT * FROM volunteers where username=:username';
$params = [
'username' => $username
];
$array = $this->query($sql, $params);
$this->user_id = $array['id'];
return $this->user_id;
}
else{
echo 'You are not signed in.' . '<br>' . 'Please sign in first.';
}
}
protected function upcoming_class(){
if (isset($_SESSION['username'])) {
$id = $this->get_user_id();
$params = [
'id' => $id
];
$sql = 'SELECT class_day FROM volunteers where id=:id';
$this->class_day = $this->query($sql, $params);
return $this->class_day['class_day'];
}
}
I want to know why am I getting the above mentioned error (in Italics) and how to remove it. I asked it because I have been trying since yesterday, with no success.
How do you use that method (sss)? Are you by chance calling it statically:
Substitute::sss();
If so, that is why you are having the issue. The method is not static. It must be called like this (as one example):
$object = new Substitute();
$object->sss();
I tried converting the properties $c_day and $c_date from class Substitute{} from public to public static and instead of using $this-> to above properties I used self:: in the methods public function sss() and public function test() and to my surprise it works. No more errors.

How to modify template params via database in joomla

I want to modify the current template params via database outside joomla framework. I managed to set the 'preset' param, but I can't write it back to the database. Here is my code.
Thanks a lot
// Initialize The Joomla Framework
// -----------------------------------------------------------------------------------
define('_JEXEC', 1);
// this is relative to the current script, so change this according to your environment
define('JPATH_BASE', '/home/kristof/public_html/joomla1');
define('DS', DIRECTORY_SEPARATOR);
// Require Joomla libraries
require_once(JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once(JPATH_BASE . DS . 'includes' . DS . 'framework.php');
require_once(JPATH_CONFIGURATION . DS . 'configuration.php');
require_once(JPATH_LIBRARIES . DS . 'joomla' . DS . 'database' . DS . 'database.php');
require_once(JPATH_LIBRARIES . DS . 'import.php');
// -----------------------------------------------------------------------------------
$app = JFactory::getApplication('site');
$template = $app->getTemplate(true);
$param=$template->params->set('preset','preset3');
$template = JFactory::getApplication('site')->getTemplate();
$db = JFactory::getDBO();
$sql = "select params from #__template_styles where template = ".$db->quote($template);
$db->setQuery($sql);
$params = json_decode($db->loadResult());
$params->PARAM_NAME= PARAM_VALUE;
$sql = "update #__template_styles set params = ".$db->quote(json_encode($params))." where template = ".$db->quote($template);
$db->setQuery($sql);
return $db->query();

Access virtuemart product data externally

Is there a quick fire method of accessing all the data for a product from an external file by using its sku?
I have looked at How can I get prodcut data of a VirtueMart 2 product in an external file?
and tried to replace the Product ID with my desired id but to no avail:
if (!class_exists( 'VmConfig' )) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart'.DS.'helpers'.DS.'config.php');
VmConfig::loadConfig();
if (!class_exists( 'VmModel' )) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart'.DS.'helpers'.DS.'vmmodel.php');
$productModel = VmModel::getModel('Product');
$product = $productModel->getProduct(Product_ID);
In short terms, I'm looking for a way to access product data.
Product sku may not be unique. so I have found a solution that works for me. Hope this helps you. This is the full code that you need to call product by sku externally.
<?php
define( '_JEXEC', 1 );
define('JPATH_BASE', 'C:\Server\www\joomla' );//you will have diff location for your site
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
jimport('joomla.application.module.helper');
jimport('joomla.application.component.helper');
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();
function getproductBySKU($sku){
$db = JFactory::getDbo();
$db->setQuery(true);
$db->setQuery("SELECT virtuemart_product_id FROM #__virtuemart_products WHERE product_sku= $sku");
$productids = $db->loadAssocList();
return $productids;
}
function getProduct($id)
{
if (!class_exists('VmConfig')) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart' . DS . 'helpers' . DS . 'config.php');
VmConfig::loadConfig();
if (!class_exists('VmModel')) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart' . DS . 'helpers' . DS . 'vmmodel.php');
$productModel = VmModel::getModel('Product');
$product = $productModel->getProduct($id);
return $product;
}
$products = getproductBySKU(1);//In this example SKU is 1 having 2 products
var_dump($products);//Gives the product id's in the SKU
foreach($products as $product){
var_dump(getProduct($product));
}

Points for members in a network who invites non users

I have created a rule in Jomsocial to award points for member who invites non users.. But now the problem is that...The points are awarded even
when the member enters "invite friends" page
points awarded when member sent email to a user(never checks whether user is a member in network or not)
How can I restrict this?
I need to award points only when the email is sent to a "non-user" or when the non-user clicks the link in the email body.
Currently this is used in components/com_community/libraries/mailq.php inside the function:
public function send( $total = 100 )
{
$mailqModel = CFactory::getModel( 'mailq' );
$userModel = CFactory::getModel( 'user' );
$mails = $mailqModel->get( $total, true );
$jconfig = JFactory::getConfig();
$mailer = JFactory::getMailer();
$config = CFactory::getConfig();
$senderEmail = $jconfig->getValue('mailfrom');
$senderName = $jconfig->getValue('fromname');
The code below is used to award points. I think some more conditions need to be added to make it validated:
if($senderName)
{
$JomSocialCheck = JPATH_BASE . DS . 'components' . DS . 'com_community' . DS . 'libraries' . DS . 'userpoints.php';
if ( file_exists($JomSocialCheck)) {
include_once( JPATH_BASE . DS . 'components' . DS . 'com_community' . DS . 'libraries' . DS . 'userpoints.php');
CuserPoints::assignPoint('com_user.add.friend');
}
}
My grandpa used to say: "when you're shouting - I hear you, when you're talking - I'm listening..." ;)
One of the two problems, I believe you can solve by modifying your code as follows:
if($senderName)
{
$JomSocialCheck = JPATH_BASE . DS . 'components' . DS . 'com_community' . DS . 'libraries' . DS . 'userpoints.php';
if ( file_exists($JomSocialCheck)) {
include_once( JPATH_BASE . DS . 'components' . DS . 'com_community' . DS . 'libraries' . DS . 'userpoints.php');
$user =& JFactory::getUser();
if($user->id) {
// he's already a user - do nothing
}
else {
CuserPoints::assignPoint('com_user.add.friend');
}
}
}

Strange behaviour for spl_autoload on phpfog

I'm just trying to build my first app on PHP Fog but there's a piece of code that doesn't run properly - works fine on localhost and other regular hosts though.
I use a modified version of TinyMVC, this is the code responsible for setting up autoloading:
/* Set include_path for spl_autoload */
set_include_path(get_include_path()
. PATH_SEPARATOR . FRAMEWORK_BASEDIR . 'core' . DS
. PATH_SEPARATOR . FRAMEWORK_BASEDIR . 'libraries' . DS
. PATH_SEPARATOR . FRAMEWORK_APPLICATION . DS . 'controllers' . DS
. PATH_SEPARATOR . FRAMEWORK_APPLICATION . DS . 'models' . DS
);
/* File extensions to include */
spl_autoload_extensions('.php,.inc');
/* Setup __autoload */
$spl_funcs = spl_autoload_functions();
if($spl_funcs === false)
spl_autoload_register();
elseif(!in_array('spl_autoload',$spl_funcs))
spl_autoload_register('spl_autoload');
Basically, it fails at the first class it should load, which is located in "FRAMEWORK_BASEDIR . 'core' . DS". The class filename is "framework_controller.php" and class name is "Framework_Controller" (tried lowercase as well). If I include the class manually it works but fails with autoload.
Here's the error message that I get:
Fatal error: spl_autoload(): Class Framework_Controller could not be loaded in /var/fog/apps/app7396/claudiu.phpfogapp.com/application/controllers/home.php on line 12
Any ideas as to what could the problem be?
I managed to sort it out:
function framework_autoload($className, $extList='.inc,.php') {
$autoload_paths = array (
FRAMEWORK_BASEDIR . 'core' . DS,
FRAMEWORK_BASEDIR . 'libraries' . DS,
FRAMEWORK_APPLICATION . DS . 'controllers' . DS,
FRAMEWORK_APPLICATION . DS . 'models' . DS
);
$ext = explode(',',$extList);
foreach($ext as $x) {
foreach ($autoload_paths as $v) {
$fname = $v . strtolower($className).$x;
if(#file_exists($fname)) {
require_once($fname);
return true;
}
}
}
return false;
}
spl_autoload_register('framework_autoload');
Thanks to another question here on StackOverflow: spl_autoload problem

Categories