how to work with multidimensional array in php

If you are programmer then you dont need introduction of array. In every language array is the most important part.

how to work with multidimensional array in php

Using array we can achieve so many things and array is useful for so many times when we do programming.
In this article I am going to share some method about PHP array.

What is PHP array?
An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

Following is syntax
array(key => value)

Creating PHP array:

<?php
$arr = array("str_arr" => "this is string", 25 => true);

echo $arr["str_arr"]; // print this is string
echo $arr[25];&nbsp;&nbsp;&nbsp; // print ture
?>

How to print direct PHP array.

<?php
$arr=array("a"=>"jimi","b"=>"timi","c"=>"limi");
print_r($arr);
?>

How to print PHP array preformated

<?php
$arr=array("a"=>"jimi","b"=>"timi","c"=>"limi");
print_r("<pre>".$arr);
?>

How to create a two-dimensional PHP array

<?php
$arr = array (
 "fruits"&nbsp; => array("a" => "orange", "b" => "banana", "c" => "apple"),
 "numbers" => array(1, 2, 3, 4, 5, 6),
 "holes"&nbsp;&nbsp; => array("first", 5 => "second", "third")
);
print_r("<pre>".$arr);
?>

How to create an array from MySql table

<?
function cre_array($row) {
 foreach($row as $key => $value) {
 global $$key;
 $$key = $value;
 }
}

//User table has first_name field present.

$sql = mysql_query("SELECT * FROM 'users'");
while ($row = mysql_fetch_assoc($sql)) {
 cre_array($row);
 echo $first_name.'<br>'; //instead of $row['first_name']
}

?>

How to count PHP array count

$fruits = array("a" => "orange", "b" => "banana", "c" => "apple");
echo sizeof($fruits);
echo $arrayLength = count($fruits);

PHP and MySql and Array

<?php
$sql = "SELECT key,value FROM table";
$result = mysql_query($sql);
while($row = mysql_fetch_row($result)) {
$myArray[$row[0]] = $row[1];
}

while(list($key,$value) = each($myArray)) {
echo "$key : $value";
}
?>

Following image will help you to understand two dimensional array.

How to work with PHP array
How to work with PHP array

how to send email using php – Using PHPmailer

Many PHP programmers facing this issue and hitting same wall. Here in article we shown, how to send email using php. we given code sample with phpmailer.

how to send email using php

how to send email using php - PHP tutorial
how to send email using php – PHP tutorial

PHP itself provides the mail() function to send emails. In this article I will show you the options of sending email using PHP language.

If you are using linux or windows hosting service for running the PHP application. PHP mail() function uses the sendmail mail server to send emails.
If sendmail mail server is configured and running on linux server then only mail() function is able to send the emails.

First we will talk about mail() function. You can pass the following parameters to mail function.

mail(to,subject,message,headers,parameters)

Sample code as follows:

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
$to = "support@purabtech.in/files/, wordpressapi@gmail.com";
$subject = "Test mail";
$message = "This is a simple email message.
 this is seond line.";
$from = "some@example.com, wordpressapi@gmail.com";

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $from";

$headers .= 'Cc: test@test1.com' . "\r\n";
$headers .= 'Bcc: test2@test1.com' . "\r\n";

mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>

If sendmail mail server is not configured on your server, mostly with windows server then there is issue.
But you can send email using anothere PHP programm called PHPmailer.
you can download the PHPmailer from following URL:

http://sourceforge.net/projects/phpmailer/

Download first PHPmailer and extract the phpmailer folder and in that folder you will find the following three files.
1. class.phpmailer.php
2. class.pop3.php
3. class.smtp.php

copy and paste the following files to your PHP application. and use the following code in case of use POP email

<?php
function sendmail_wordpressapi($to,$from,$subject,$body,$id){
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
include_once('class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail             = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "purabtech.in/files/"; // SMTP server
$mail->From       = $from;
$mail->FromName   = $from;
$mail->Subject    = $subject;
$mail->Body       = $body;
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap   = 50; // set word wrap
$mail->MsgHTML($body);
$mail->AddAddress($to, $to);

if(!$mail->Send()) {
$mail             = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "WORDPRESSAPI@gmail.com";  // GMAIL username
$mail->Password   = "YOURPASSWORD";            // GMAIL password
$mail->AddReplyTo("smtp.production@gmail.com","wordpressapi");
$mail->From       = "support@purabtech.in/files/";
$mail->FromName   = "SMTP EMAIL-wordpressapi";
$mail->Subject    = "email server is having some issue";
$mail->Body       = "Hi,<br>email server is having some issue,<br> check this out";                      //HTML Body
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap   = 50; // set word wrap
$mail->MsgHTML($body);
$mail->AddAddress("support@purabtech.in/files/", "support-wordpressapi");
$mail->IsHTML(true); // send as HTML
$mail->Send();

} else {
 echo "Message sent!";
}
}
?>

I created above function in this function I am using SMTP mail function for sending email but in case mail is not going using SMTP details.
you can also send emails using your gmail account details. you can edit or use this function as your requirement.

You can pass following parameters to sendmail_wordpressapi function:

sendmail_wordpressapi($to,$from,$subject,$body,$id);

You can send email to multiple email addresses. I am sure using above code sending emails through PHP is very easy. If you have any quries..write to me.

How to create redial blur effect in Photoshop

In this article we are going to show you how to create radial blur effect to text, How to create redial blur effect in Photoshop

Just follow my step

First step

Select first text tool from tool bar

Then write a text in your workspace “radial blur”

You can write any name in your workspace

Second step

Select filter >> blur >> radial blur

In radial blur having many more options

Which you can select

Blur method like spin, zoom

In quality option having

Draft, good, best qualities

And the important is you can give your specific amount for that

Here I select spin blur method, good quality & amount is 40

It will look like below

Create fixed side or top buttons in wordpress theme

We can easily introduce fixed side buttons in wordpress or any website. In this article I given code snippet for Create fixed side buttons in wordpress.

Create fixed side buttons in wordpress

Fixing the position of any document element using CSS this is very old technique but still  I am going to give you some good trick to show the twitter button or search buttons or add new button on bottom or sidebar.

Using following CSS code you can integrate the any button on sidebar.


#submit_news { background:transparent url(images/submit-news.png) no-repeat scroll 0px 0px; height:170px; left:0; position:fixed; top:180px; width:40px; }
div#submit_news:hover { background-position:-41px 0px; }
#submit_news a{ display:block; height:170px; width:40px; }

In this tutorial we are using the only one image as a button. As we know we are using the sprite image css here.

Open your header.php file from wordpres theme. and under body tag put following code.

<div id=”submit_news”><a href=”/submit-news” ></a></div>

First you need to create the submit news page in wordpress.

If you want to add the submit news button for your site you can download from here.

how to create diffuse effect to text in photoshop

In this tutorial we show, how to create diffuse effect to text in photoshop. we given the step by step with their screen shot and with their explanation.

Step1. Open photoshop first and or choose the 400×400 image and write the text what ever you want.

how to create diffuse effect to text in photoshop

Here I written the diffuse, Select a text write on workspace “diffuse”

how to create diffuse effect to text in photoshop
how to create diffuse effect to text in photoshop

Scale this shape by selecting edit >> transform >> scale &

Go to layer >> select type >> & select type

Now you can modify text

Now select filter >>  >> select distort

It will look like this

In diffuse filter having more option

Normal, darken only, lighten only & anisotropic

It will give different type of effect of our text

Now select layer >> duplicate layer

Give it name art_1

It will helpful you to give a name to each layer

Now make some more duplicate layers

how to create diffuse effect to text in photoshop
how to create diffuse effect to text in photoshop

I made some different diffuse filter it will look like this

get alexa rank using php code

There is nice wordpress plugin which will help you to add alexa rank in your website. In this article, we briefly explained to get alexa rank using php code

get alexa rank using php code

if you want to check the your website ranking as per alexa so you can use the following code in your php projects. There is very nice wordpress plugin which will help you to add the alexa rank in wordpress site. In this article I given PHP code for adding the alexa rank block on any website.

Alexa Rank Widget

http://data.alexa.com/data?cli=10&dat=s&url=wordpressapi

“http://data.alexa.com/data” this file will return the xml format output. Using the XML output or reading xml file we can easily fetch the our or any website ranking.

Use the following function for getting the alexa website ranking.

<?php
function AlexaRank( $url )
{
preg_match( '#<POPULARITY URL="(.*?)" TEXT="([0-9]+){1,}"/>#si', file_get_contents('http://data.alexa.com/data?cli=10&dat=s&url=' . $url), $p );
return ( $p[2] ) ? number_format( intval($p[2]) ):0;
}

echo "purabtech.in/files/ Rank as per alexa.com: ";
echo AlexaRank('purabtech.in/files/');

?>

if you want to check the multiple website ranking in one shot than use the following PHP code.

<?php
$domains = array( 'google.com', 'ask.com', 'yahoo.com', 'bing.com' );

foreach ( $domains as $domain )
echo $domain, ' - ', AlexaRank( $domain ), '<br />', PHP_EOL;

?>
get alexa rank using php code
get alexa rank using php code

Have fun!

add event to all image elements using javascript

In this article we will show to attach event to DOM element and add event to all image elements using javascript. Using javascript we can attach the event to any dom element.

add event to all image elements using javascript

You many times heard about getElementsByTagName javascript method but you did not tried.

getElementsByTagName function is very useful when you are working with javascript events. Using this function you can attach the event to any tag which is present in HTML body.

Here I am going to give the image example. Following script will attach the mouseover event to all images which are present in DOM.


var all_images = document.getElementsByTagName('img');

for (var i = 0; i < all_images.length; i++) {

addEvent(all_images[i], 'mouseover', myfunction, false);

}

function myfunction () {

 alert('this is image mouse over alert');

}

/**
 * cross-browser event handling for IE5+, NS6 and Mozilla
 * By Scott Andrew
 */
//This addEvent function we are using for external class use
this.addEvent = function(elm, evType, fn, useCapture) {
 if (elm.addEventListener) { // Mozilla, Netscape, Firefox
 elm.addEventListener(evType, fn, useCapture);
 return true;
 } else if (elm.attachEvent) {  // IE5 +
 var r = elm.attachEvent('on' + evType, fn);
 return r;
 } else {
 elm['on' + evType] = fn;
 }
}

</script>
add event to all image elements using javascript
add event to all image elements using javascript

If you copy paste the following code in your document and If you mouseover on any image then alert with message will come.

This script will work in any browsers. (IE 6,7,8, FF3,2, Safari3,4)

check html 5 is support in browsers through javascript

HTML5 is new version of HTML and many people does not aware of it.  In article, we checked html 5 is support in browsers through javascript. HTML5 is a new version of HTML and XHTML. The HTML5 draft specification defines a single language that can be written in HTML and XML.

check html 5 is support in browsers through javascript

It attempts to solve issues found in previous iterations of HTML and addresses the needs of Web Applications, an area previously not adequately covered by HTML.

check html 5 is support in browsers through javascript
check html 5 is support in browsers through javascript

In one of my project I need to use HTML5 methods and properties through javascript. So first I need to check or dectect with multiple browsers is there way to find HTML5 compability with browsers.I

Main introduced features are like canvas, video, or geolocation. Using that we can easily dectect the browsers compability.


if (navigator.geolocation) {
/* geolocation is available */
} else {
alert("I'm sorry, but geolocation services and HTML5 are not supported by your browser.");
}

or

if (window.postMessage) {
/* postMessage method is available */
} else {
alert("I'm sorry, but postMessage method and HTML5 are not supported by your browser.");
}

I am still searching for better solution…

Rich Text Editor in Rails Application

Many times you need normal CMS for your application. You want to need some pages data need to handle through CMS.

It is really very easy to Install any RTE in the Rails. There are many open source RTE available.

I used the Cross-Browser Rich Text Editor for my project.
I downloaded files from there and uploaded to my public folder. I added required CSS and JS file to my layout.

You need to add following lines to your application.html.erb file.(you will find this file in view/layout/ folder)

1. You can add that files on conditional base also. Means for that particular page.
2. Add following lines to your form
:html => { :name => ‘BlogRTE’, :onsubmit => “submitForm();”}

In to add following lines to your form;

<script language=”JavaScript” type=”text/javascript”>
<!–
function submitForm() {
updateRTEs();
return false;
}
initRTE(“/cbrte/images/”, “/cbrte/”, “”, true);
//–>
</script>
<noscript><p><b>Javascript must be enabled to use this form.</b></p></noscript>
<script language=”JavaScript” type=”text/javascript”>
<!–
//build new richTextEditor
var rte1 = new richTextEditor(‘text_content’);
rte1.html = ‘Write your thoughts here.’;
rte1.toggleSrc = true;
rte1.width = 500;
rte1.build();
//–>
</script>

You will get the text_content params in Rails.

For Edit page functionality i got error in form. So you need to this default code for all languages.
rte1.html =””;

When i used this i got an error.

But i solved this issue after some R&D and modification in code.

Use following code for Rails(Edit functionality)
rte1.html =”<%=text_content.gsub(/”/, “‘”).gsub(/\n/, ”).gsub(/\r/, ”) %>”;

this will solve your problem.

Full code: (IF YOUR CODE NOT WORKS THAN USE MY FULL CODE)

<script language=”JavaScript” type=”text/javascript”>
<!–
function submitForm() {
updateRTEs();
return false;
}
initRTE(“/cbrte/images/”, “/cbrte/”, “”, true);
//–>
</script>
<noscript><p><b>Javascript must be enabled to use this form.</b></p></noscript>
<script language=”JavaScript” type=”text/javascript”>
<!–
//build new richTextEditor
var rte1 = new richTextEditor(‘text_content’);
rte1.html = ‘Write your thoughts here.’;
rte1.toggleSrc = true;
rte1.width = 500;
rte1.build();
//–>
</script>

setting up basic authentication apache

Two months back, we got the requirement of do basic authentication for testing site server. We given steps and code for setting up basic authentication apache. So Google or any search engine site cannot index the testing sites.

setting up basic authentication apache

We are using Fedora as Operating System and Apache as web server on our testing machine. We hosted more than fifteen test sites on that server.

I successfully created basic authentication on server.

Use following commands:

#su

#ROOT_PASSWORD

#vi /etc/httpd/conf/httpd.conf

in that file you need to insert following lines.

AccessFileName htaccess.acl .htaccess
# htpasswd -c /home/USER/pwd.txt USER(you can define your user of stystem.)
New password: mypassword
Re-type new password: mypassword

That sit. Your username and password is set for popup.

Now you need to only create or update your .htaccess  file. You can create or find .htaccess file in your project folder.

Use or copy and paste following code in that file: (.htaccess file)

AuthUserFile /home/USER/pwd.txt
AuthName "Protected"
AuthType Basic


require valid-user

I following exact method for my server and projects. It is working perfect for me.