Advertisement

Tuesday, 28 October 2014

How to exclude items by user id in osclass.

In this post i have explained you how to exclude the items in main page listing by user id in osclass.
By using this function you can exclude the items posted by a user in main page listing or in any page listing.

Usually we will use the function osc_query_item in osclass to exclude the items.But in this case its not usefull to use the osc_query_item function to exclude the item using user id.We can only do this by the use of the functions . Follow the procedures to use the function.

Add this at the bottom of the functions.php file inside your theme folder.

<?php function cust_query_item($params = null) {
 $mSearch = new Search(); if($params==null) { $params = array(); }
else if(is_string($params)){
      $keyvalue = explode("=", $params); $params = array($keyvalue[0] => $keyvalue[1]);
}
 foreach($params as $key => $value) {
   switch($key) {
      case 'id': $mSearch->addItemId($value); break;
      case 'author': $tmp = explode(",", $value);
         foreach($tmp as $t) { $mSearch->fromUser($t); }
         break;
     case 'category':
     case 'category_name': $tmp = explode(",", $value);
          foreach($tmp as $t) { $mSearch->addCategory($t); }
         break;
   case 'country':
   case 'country_name': $tmp = explode(",", $value);
         foreach($tmp as $t) { $mSearch->addCountry($t); } break;
   case 'region':
   case 'region_name': $tmp = explode(",", $value);
        foreach($tmp as $t) { $mSearch->addRegion($t); }
        break;
   case 'city':
   case 'city_name': $tmp = explode(",", $value);
        foreach($tmp as $t) { $mSearch->addCity($t); }
        break;
   case 'city_area':
   case 'city_area_name': $tmp = explode(",", $value);
        foreach($tmp as $t) { $mSearch->addCityArea($t); }
        break;
  case 'results_per_page':
       $mSearch->set_rpp($value);
       break;
  case 'premium': $mSearch->onlyPremium(($value==1?true:false));
      break;
 case 'page': $mSearch->page($value);
      break;
 case 'offset': $mSearch->limit($value);
      break;
 case 'excluded_author':
       $mSearch->addConditions(sprintf("%st_item.fk_i_user_id NOT IN (%s)", DB_TABLE_PREFIX, implode(',', $value))); break; case 'order': $mSearch->order($value, '');
        break;
 default: osc_run_hook('custom_query', $mSearch, $key, $value); break;
   }
 }
View::newInstance()->_exportVariableToView("customItems", $mSearch->doSearch());
 }?>

Now, this is the same code as core function osc_query_item, but I've added two more customizable cases, "excluded_author" (for our needs) and "order" so you might get a truly randomized list. 

 Then you call this new function like this:
 
<?php
cust_query_item(array("premium"=>1, "results_per_page" => 495, "order"=> 'RAND()', 'excluded_author' => array(XX,YY,ZZ)));
?>
You can change the result per page as your wish. And you can edit the function as your need.

Monday, 27 October 2014

How to insert a script file in osclass admin.

The enqueue functions are used for loading your javascript and css files in osclass admin.The reason for using enqueue functions is becuse many plugins are using javascript frameworks such as JQuery, JQuery-ui, and fancybox just to name a few. There is a chance that these javascript files could be loaded multiple times and that is why the enqueue functions are used. The enqueue functions help to reduce the chances of the same file getting loaded more than once in osclass. 

Here, I have explained with examples below about how to insert a script file in osclass admin when we creating a plugin.
Registering a javascript file
osc_register_script('jqueryPopup', osc_base_url() . 'oc-content/plugins/popup_for_osclass/js/jCarouselPopup.js', 'jquery');
Enqueueing the script
osc_enqueue_script('jqueryPopup');
Enqueueing a style
osc_enqueue_style('popuplCss', osc_base_url() . 'oc-content/plugins/popup_for_osclass/css/popup.css');
You may have noticed that i did not register the style and that is because it is not required and there is no function to do so.Real life example,for inserting a script file in osclass admin.Ok now let us move to a real life example. To use these functions you have to create a function for this example I am going to use examle_load_scripts() please use a unique function name in your plugin if you are planning on releasing it otherwise we could end up with conflicting function names.

function examle_load_scripts() {
     osc_register_script('jqueryPopup', osc_base_url() . 'oc-content/plugins/popup_for_osclass/js/jCarouselPopup.js', 'jquery');
     osc_enqueue_script('jqueryPopup');
     osc_enqueue_style('carouselCss', osc_base_url() . 'oc-content/plugins/popup_for_osclass/css/popup.css');
 }
Once you add that code you will then need to add the following hook to your index.php file of your plugin. 

osc_add_hook('init', 'examle_load_scripts');
To load the script on the admin side you would use this hook

osc_add_hook('init_admin', 'examle_load_scripts');
This is the way of inserting a script or a css file into osclass admin.

Sunday, 31 August 2014

How to write a custom query in osclass to fetch results from database.

Here in this tutorials, we have explained you about how to write the custom query in osclass.In osclass classified script, it is easy to write a custom query to fetch some result from the database or insert or update some fields into the database table. 

We can use some predefined functions to print some fields from osclass database. But the predefined functions in osclass are limited, so we can't get all field's data from osclass database by the predefined functions.  Another one important thing in osclass , there is no way to update or insert any fields using any predefined functions .

 For that we need to write a custom query and execute it in osclass. This post will be very much usefull to the osclass users those who customising the functionality to their needs in osclass. If we want to use the custom queries in osclass , we need to have it as a seperate function inside theme funtions.php  file. The main reason for keeping it as a seperate function is, to avoid the repetation of the code.

 Example custom query for selecting the post count for a user in osclass is given below. Paste this function at the bottom of the functions.php file inside the theme folder.Make sure , not to leave any space after the function.
For Select
if( !function_exists('logged_user_post_count') ){
    function logged_user_post_count($userId) {
        $conn = getConnection();
        $item_count = $conn->osc_dbFetchResults("SELECT count(*) FROM %st_item WHERE fk_i_user_id='%d' AND b_active='1'", DB_TABLE_PREFIX, $userId);
        return $item_count[0]['count(*)'];
     }
 }


You can call the fucntion by passing user id to the function as parameter. Here $conn = getConnection(); is the connections string to establish the connection to the osclass database to execute the query.
$conn = getConnection();
This function is only used to select the fields from database using custom Query in osclass.
$conn->osc_dbFetchResults();
Example custom query for Inserting the a value to the database in osclass.Paste this function at the bottom of the functions.php file inside the theme folder.Make sure , not to leave any space after the function.
For Insert
 $conn = getConnection();
 $conn->osc_dbExec("INSERT INTO %st_pages (s_internal_name, b_indelible, dt_pub_date) VALUES ('subscription_email', 1,'%s' )", DB_TABLE_PREFIX, date('Y-m-d H:i:s'));

$conn->osc_dbExec();
 
This function is used to execute the custom query in osclass for Insert,update,delete  .The table names which are used in the above examples are osclass table names to understand better for the users. This is the way to write custom query to select , insert, update, delete operations in osclass.

Wednesday, 27 August 2014

osclass tutorials for beginners

osclass is an open source script that allows you to create a classifieds site easily without any technical knowledge.

 There are plenty of osclass themes comes with responsive designs ,so the we don't have to worry about the user interface in mobile,tablet views.

The osclass-tutorials.blogspot.in is specially created for the peoples those who is interested in learning osclass classifieds.

The post "osclass tutorials for beginners." is created to give guidelines to the developers and interested peoples.

First of all we need to know what is osclass classifieds scripts and how the structure of the scripts. In this tutorial we are giving guidelines about the given below topics.


 2. Default admin settings in osclass.

 3. Theme customization in osclass.

 4. Functions we should know in osclass.

 These are all the important things for the beginners to work in osclass. Here we are providing every post with a good examples , so that the beginners will easily understand about how to start work in osclass.

 For the time being we have the linked only the first topic(How to install a osclass site in local server or localhost).

We will post the link very soon for the users about the other topics.

How to print the parent category id or slug using its subcategory id or slug in osclass.

I have googled many times for printing parent category "id" or "slug" using subcategoy "id" or "slug" in osclass for some listing functionality.

 In osclass site there is no predefined function to print the parent category "id" or "slug" using subcategory "id" or "slug".

 So i have created one function to print the parent category "id" or "slug" using its subcategory "id" or "slug".

 The function can be added into the functions.php file inside the theme folder. In osclass site we can use custom functions but it should not bother the default functions.

 I have given some explainations with the coding and how to use the function to print the parent category "id" or "slug" using subcategoy "id" or "slug". 

Paste the function at the bottom of the function.php file inside the theme folder. Be sure not to leave any space after the function.

This is the function which accepts only parent id as parameter.so you have to pass the category id as parameter for the given below function when calling the function.
if( !function_exists('bender_item_parentcategory') ){
        function bender_item_parentcategory($category){
            $catDeats = array();
            $aCategory = osc_get_category('id',$category);
            $parentCategory = osc_get_category('id', $aCategory['fk_i_parent_id']);
            $catDeats['current_name'] = $aCategory ['s_name'];
            $catDeats[
current_id'] = $aCategory ['pk_i_id'];
            $catDeats['
current_slug'] = $aCategory ['s_slug'];
            $catDeats[parent_name'] = $parentCategory ['s_name'];
            $catDeats['
parent_id'] = $parentCategory ['pk_i_id'];
            $catDeats['
parent_slug'] = $parentCategory ['s_slug'];
            return $catDeats;       
        }
    } 




This is the function which accepts only parent slug as parameter.so you have to pass the category slug as parameter for the given below function when calling the function.
if( !function_exists('bender_relation_category') ){
        function bender_relation_category
($category){
            $catDeats = array();
            $aCategory = osc_get_category('slug',$category);
          
  $parentCategory = osc_get_category('id', $aCategory['fk_i_parent_id']);
            $catDeats['current_name'] = $aCategory ['s_name'];
            $catDeats[
current_id'] = $aCategory ['pk_i_id'];
            $catDeats['
current_slug'] = $aCategory ['s_slug'];
            $catDeats[parent_name'] = $parentCategory ['s_name'];
            $catDeats['
parent_id'] = $parentCategory ['pk_i_id'];
            $catDeats['
parent_slug'] = $parentCategory ['s_slug'];
            return $catDeats;        
        }
    }



Call the function anywhere in the theme files and pass the category id as parameter for the function .
 you will get a result array which has the details for slugname, parent category id ect...

Function calling example using category id as parameter,

$values = bender_item_parentcategory(12);


Function calling example using category slug as parameter,

$values = bender_relation_category('userd-cars');


you can print the result array using print_r($values) for your understanding.

If the parameter your passing to the function is the parent category , then the result array only print its category name, slug, category id in the result array.

If the parameter your passing to the function is subcategory , then you will get the category and its parent category details.

How to install osclass in localhost

The Osclass free classifieds script is wonderfull script for the classifieds websites.Osclass is a php script that is used to create and manage your own free classifieds site.Using osclass script, you can provide free advertising for items for sale, real estate, jobs, cars ect...

Nearly 25% of free classified advertising sites are using Osclass scripts. You can Visit demo in the osclass.org and can know more about osclass. Instalation in localhost.

1. First of all we need to download latest version of osclass script from osclass.org and extract the zip file. Rename the extracted file as you want and copy the folder into your root folder. For example "osclass-example".

2. Go to your phpmyadmin page and create a database and copy the name of the created database.

3. Run the created osclass website in your local server. For example "http://localhost/osclass-example/" type the url in your browser to run the site.

 4. After you will see the screen like the image given below. Click the "Install" button move one stept ahead in instalation.

osclass-install-localhost

 5. After clicking "Install" click "Run the install" button.

osclass-install-localhost

 6. Next step in the installation is to fill the database information and click "Next" button.

osclass-install-localhost

 7. After DB details, enter the adminitrator login details for the newly created osclass site and click "Next".
 
osclass-install-localhost

If you have installed the Osclass script successfully, you can visit the site you have created in your localserver by typing the "http://localhost/osclass-example/" in your browser.
 Allright you have installed the osclass site in your local server and you can start doing the site as your wish with my tutorials.