phprockers-logo

mysql_escape_string in php

0 comments
This function escacpe the single quotes, double quotes and unwanted symbol from our passing strings.

Format :
========
mysql_escape_string(string_name);

Example:
======
$first_name = mysql_escape_string($first_name);


Keep in mind:
=============
mysql_escape_string() cann't escape the % and _.

List out all the mysql database name in php

0 comments
List out all the mysql database name using follwing php code.

<?php

    error_reporting(E_ALL);

    $connection = mysql_connect('localhost', 'root', '');

    $database_list = mysql_list_dbs($connection);

    $count = mysql_num_rows($database_list);

    for($i=0;$i<$count;$i++) {

        echo mysql_db_name($db_list, $i) . "<br/>";
    }

?>

How to get the mysql last inserted id using PHP?

1 comments
We have to easily get the last inserted id in mysql table using below code.

<?php

$connection = mysql_connect('localhost', 'MYSQL_USERNAME', 'MYSQL_PASSWORD') or mysql_error();

mysql_select_db('DATABASE_NAME', $connection)  or mysql_error();

$query = "INSERT INTO emp_table (emp_id, emp_name, emp_qualification, emp_salary) VALUES ('SC70699', 'Ramakrishnan', 'MCA', '13186')";

mysql_query($query );

$last_insert_id = mysql_insert_id();

echo "Last insert id in employee table: ".$last_insert_id;

mysql_close($connection) ;

?>

How to get the post tags for current post/page?

0 comments
We can easily get the post tags for current post using following code and give the link for each post tags.

<?php
$current_post_tags = wp_get_post_tags($post->ID);
$post_tags_link .= '<ul class="post-tags-link">';
for($i=0; $i < count($current_post_tags ); $i++) {
$post_tags_link .= '<li><a href ="'.get_bloginfo('url').'/'.$current_post_tags[$i]->slug.'">'.$current_post_tags[$i]->name.'</a></li>';           

$post_tags_link .= '</ul>';
//echo "<pre>";print_r($current_post_tags);
?>
Find more about:<?php echo $post_tags_link; ?>

Breadcrumbs in wordpress

0 comments
global $wp_query;
$Post_ID = $wp_query->post->ID;   
$category = get_the_category();
if(!empty($category)) {
    $current_category = $category[0];
    $parent_category = $current_category->category_parent;
    $bread_cat_link .= '<ul class="post-categories">';
    if ( $parent_category != 0 ) {
    $bread_cat_link .= '<li><a href="' . get_category_link($parent_category) . '">' . get_cat_name($parent_category) . '</a></li><span>&gt;</span>';

How to get the next and Previous title link for current post in wordpress?

0 comments
<div id="posts_navigation">
    //get the previous link for current page
    <?php if(get_adjacent_post(true, '', false)) { ?>
    <div class="next"><?php next_post_link('%link', '%title', TRUE, '');?></div>
    <span> | </span>
    <?php } ?>
    //get the next link for current page
    <?php if(get_adjacent_post(true, '', true)) { ?>
    <div class="prev"><?php previous_post_link('%link', '%title', TRUE, '');?></div>
    <?php } ?>
</div>

Next and Previous link for current post in wordpress

0 comments
<div id="posts_navigation">
    //get the previous link for current page
    <?php if(get_adjacent_post(true, '', false)) { ?>
    <div class="next"><?php next_post_link('%link', '&lt; Previous', TRUE, '');?></div>
    <span> | </span>
    <?php } ?>
    //get the next link for current page
    <?php if(get_adjacent_post(true, '', true)) { ?>
    <div class="prev"><?php previous_post_link('%link', 'Next &gt;', TRUE, '');?></div>
    <?php } ?>
</div>

How to get the custom field values in wordpress?

0 comments
First we should know, what are the custom fileds values are available now. get_post_custom_keys() can be used to find all the custom fields in current post.

<?php
$get_list_custom_fileds = get_post_custom_keys();
print_r($get_list_custom_fileds);
?>

Then we have to esily get the particular custom field value for current post. Here the example of get the book author name for current post.

How do submit a form without submit button?

0 comments
Easily submit the values without submit button using javascript in the form.  You can use following code in your program.

<script type="javascript/text">
document.formname.submit();
</script>

Get nav menu locations in wordpress

0 comments
Definition of get_nav_menu_locations():

It is returns an array with the registered navigation menu locations and which menu is assigned to it.
$locations = get_nav_menu_locations();
echo "<pre>";
print_r($locations);
echo "</pre>";

How to get the register menu items from "Theme Locations"?

0 comments
Very simple to get the register menu items,

$locations = get_nav_menu_locations();

Returns an array with the registered navigation menu locations and which menus are assigned to it.

$args = array(
        'order'                     => 'ASC',
        'orderby'                 => 'menu_order',
        'post_type'              => 'nav_menu_item',
        'post_status'            => 'publish',
        'output_key'            => 'menu_order',
        'nopaging'               => true,
        'depth'                    => '0',
        'update_post_term_cache' => false
    );

How to register the menu in "Theme Location"?

0 comments
In wordpress, very easy to register the menu in "Theme Location". For that so you have to follow some steps.

Step 1: Goto localhost/WORDPRESS FOLDER/wp_content/themes/twentyeleven/

Step 2: Open functions.php file.

Step 3: "register_nav_menu( 'primary', __( 'Primary Menu', 'twentyeleven' ) );" find this inside the file. After find the code, below you have to write a  following code.

Where is the "Theme Location" presents in wordpress?

0 comments
Go to admin panel in wordpress.

See left sidebar,Go to "Appearance" then click the "Menus".

At first time no menus are available in wordpress. After creating the menu, then only visible "Theme Location" to us.

How to get the menu items in wordpress?

0 comments
Using wp_get_nav_menu_items() function, we can get all the items(post,page,category) in the menu.

<?php
    $menu_items = wp_get_nav_menu_items('64');
    $menu_list = '<div class="menu_list">';
    foreach ( (array) $menu_items as $key => $menu_item ) {
        $title = $menu_item->title;
        $url = $menu_item->url;
        $menu_list .= '<div><a href="' . $url . '">' . $title . '</a></div>';
    }
    $menu_list .= '</div>';
    echo $menu_list;
?>

Cron Job in ubuntu

0 comments
Cron Job in PHP
============
If we put the file in cron, It will run automatically by setting the time limit. Using time limit we have run the file every min/hour/day/month etc.

dckap1@dckap-desktop:~$ sudo su

[sudo] password for dckap1: **************
root@dckap-desktop:/home/dckap1# crontab -e

If you are using the cron in first time, First you should select the editor. It(crontab -e) will show five editor. Then you have to select the editor. Here nano editor is very useful in this, so you should select the nano editor.


Pagination in php

0 comments
When we have a large list of items, we can display them grouped in pages and presents navigational elements to move from one page to another. This code creates a pagination for listing the page per 10 records.

<?php

$con = mysql_connect('localhost','ramakrishnan','raman') or die(mysql_error()); ;
mysql_select_db('rama_db',$con) or die(mysql_error()); ;

$page = $_REQUEST['page'];
if($page == "" || $page == null ) {
    $page = 1;
} else {
    $page = $page;
}

//setting the limit for fetching the records.
$per_page_limit = 10;
if($page >1) {
    $start_limit = ($page-1)*$per_page_limit;
    } else {
    $start_limit = 0;
}

How to write a query in Joomla

0 comments
If we want to execute the query in joomla, We have to follow some basic steps in Joomla. Then only query will execute,

<?php

       $db =& JFactory::getDBO();
       $query = "SELECT * FROM Joomla_tables";
       $db->setQuery($query);
       $result = $db->query();
       echo "<pre>";
       print_r($result);
       echo "</pre>";

?>

Static method in php

0 comments
1. Using "static" keyword to declare the static methods.

2. We can access the static methods without creating an instantiation(object) of the class.

Difference between abstract class and interface in php

1 comments

S.No

Abstract Class

Interface Class

1
Abstract class - method must be declared as abstract. Abstract methods doesn’t have any definition. The sub class (extend class) can have the definition for those abstract method. Interface - all the methods by default are abstract methods only. So one cannot declare variables in interfaces. The sub class (implement class) can have all method definition for those methods.

2
The Abstract methods can declare with Access modifiers like public, internal, protected. All methods declared in an interface must be public.

3
Abstract class can contain variables and concrete methods. Interfaces cannot contain variables and concrete methods, but we can possible to define the constants.

4
An abstract class can Inherit only one Abstract class and Multiple inheritance is not possible for Abstract class. A class can implement many interfaces and Multiple interface inheritance is possible.

Get current custom post type in wordpress

0 comments
We can get easily custom post type or normal post type in wordpress by using the post id.
global $posts;
$post_id = $posts->ID;
$post_type = get_post_type($post_id);
echo $post_type;

Get current post id in wordpress

0 comments
2 ways, we have to get current post id in wordpress
First way: 
global $posts;
$current_post_id = $posts->ID;
Second way:  

global $wp_query;
$current_post_id = $wp_query->post->ID;

Get Category Name from Post ID in WordPress

0 comments
Get the category name by using the post id in wordpress,
Step 1: First we have to get the post id
global $posts;
$post_id = $posts->ID;

Step 2: After the get the post id, then we get the category is very simple.
$category = get_the_category($postID);

$category_name = $category->cat_name