Advertisement

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.