Monday, 24 June 2013

Some Basic But Useful WordPress Functions | PHP Tutorial for Beginners

In this tutorial for Php Tutorial for Beginners we will show you some basic but important functions of WordPress

1 - $current_category = single_cat_title("", false);
Display current category Title.

2 - $permalink = get_permalink( $id ); 
Returns the permalink to a post or page for use in PHP. 

3 - $cat_id = get_cat_ID( $cat_name ) ;
Retrieve the ID of a category from its name.

4 - $current_user = wp_get_current_user(); 
Retrieve the current user object.

5 - $current_user_id = get_current_user_id( ); 
Returns the ID of the current viewer if they are logged in. Returns 0 if the viewer is not logged in.

6 - $title = get_the_title();
Return the cureent post/page title.

7 - $posts = get_posts( array('numberposts' => 6,'orderby' => 'post_date','order' => 'DESC','post_type' => 'post','post_status' => 'publish' ) );
Get the posts from database accroding to the argument passed in it.

8 - get_header()
Add Header page to the file.

9 - bloginfo('home')
Fetch the URL of your home page.

10 - the_content()
Displays the contents of the current post. This template tag must be within the loop.

Hope this php tutorial is useful for you. Keep following Php Tutorial for Beginners for more Codes.

Checking USER AGENT of Client in Php | PHP Tutorial for Beginners

In this tutorial for Php Tutorial for Beginners we will show you the example of checking user agent in PHP.
The first question will be WHAT IS USER AGENT?
It is a software that is acting on behalf of user. Let's take browser Example, when you are using your browser for surfing at that time it's your USER AGENT. If you are working on Google Chrome then your USER AGENT will be "CHROME", if you are using Mozilla Firefox then your user agent will be "Mozilla". The USER AGENT values of particular request not only contain browser details but also it include your platform details.

Suppose you are making some project and at particular situation you want some code to be run only on Mozilla Firebox. In that situation you need to add following code in your page.
if(strpos($_SERVER['HTTP_USER_AGENT'],'Firefox'))
{
//Your Firefox code
}

This code will fetch the USER AGENT details of user and our "strpos" function will check whether "Firefox" is present or not in that code. If it return true then your code for Mozilla Firefox browser will be executed.

Similarly you can also check the platform of user.
Example:-
if(strpos($_SERVER['HTTP_USER_AGENT'],'iPhone'))
{
//Your iPhone code
}


Hope this php tutorial is useful for you. Keep following Php Tutorial for Beginners for more Codes.

Thursday, 20 June 2013

Creating a Search Engine in PHP and MySQL Or Creating Search Form in PHP MySQL | PHP Tutorials for Beginners


In this tutorial of PHP Tutorial for Beginners we will show how to create "Search Engine in PHP with MySQL or how to create your Search Form in PHP by using MySQL".

What is Search Engine:-
Search Engine is a functionality where you enter your keywords or text in to search box, and it will give you all the record from database that matches your keyword.

In making Search Engine or Search Form you need to follow few steps:-
->Create a Database in phpmyadmin.
->Create a Table called "main_article" in the Database.
->In your table create fields called "article_entry_title" and enter some data in it which is used  during search.
->Now create config.php which have database connection code.
->Last you have to create your script file for Search Engine.


Now create the config.php file and put the following code in it. This file we will include in our search engine script for database connectivity.
You can check the tutorial here for Database connection in Php MySQL.
<?php
$database = "Php_Tutorial";  // the name of the database.
$server = "localhost";  // server to connect to.
$db_user = "root";  // mysql username to access the database with.
$db_pass = "";  // mysql password to access the database with.
$table = "users";    // the table that this script will set up and use.
$link = mysql_connect($server, $db_user, $db_pass);
mysql_select_db($database,$link);
?>

Here is final script that will create your Search Engine page. Create search.php file and past the below code in it.
<html>
<head>
  <title>Creating Search Engine page in php using mysql</title>
</head>
<body>
<form name="php search" action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
<label>Enter Search Keyword:</label><input type="text" name="search_keyword">
<input type="submit" value="Search">
</form>
</body>
</html>

<?php
require 'config.php';
//included database connection file

if (isset($_POST['search_keyword']) && !empty($_POST['search_keyword']))
//Here we are checking Keyword is entered or not.
{
$keyword = $_POST['search_keyword'];
if(!empty($keyword))
{
$query ="SELECT * FROM main_article WHERE article_entry_title LIKE '%".mysql_real_escape_string($keyword)."%'";

$query_result = mysql_query($query);

$total_rows = mysql_num_rows($query_result);
// We got the number of rows for matched keyword search

if($total_rows>0)
{
echo '<b>Total '.$total_rows.' records found for your searched keyword:<br></b>';
$i=1;
While($query_row = mysql_fetch_array($query_result))
{
echo $i.'-'.$query_row['article_entry_title'].'<br>';
//Displaying each result line by line.
$i++;
}
}
else
{
echo'No result found for your search query.';
}
}
}
else
{
echo '<b>Please enter Keyword to search</b>';
}
?>


Your PHP Search Engine is now ready to use. Almost all user use the search engine in their website to easily search for the product which user require. You can modify it and use this search engine code in admin panel of your project for finding perticular entries.

Hope this php tutorial is useful for you. Keep following Php Tutorial for Beginners for more Codes.