Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

December 26, 2015

Embed Javascript code in your PHP code


I can't tell where exactly you can apply this knowledge or what may call for it. Am compelled to write on this subject so here we are.
Javascript basically is executed by the client's (your visitor's) machine (i.e browser). PHP on the other hand is parsed by PHP server; then rendered onto the browser. With this basic introduction, I guess we are good to go.
The code snippet below shows a mathematical operation performed within the open and closing braces of our PHP tag.
<?php

echo "<script>
var num1,num2,ans;
num1 = 3;
num2 = 4;
ans = num1 + num2;
alert(ans);
</script>";

?>
This may not mean much to you right now but when you think about what you can do with it, you will know its worth. Take note of the double quotation mark immediately after the echo statement. The closing quotation mark for that comes after the greater than sign (>)in the closing tag for script. Whatsoever is between the open script and the closing script is what will be executed by the client's machine as our Javascript. In our case, an alert box will pop-up with the value 7.

December 17, 2015

Port 80 Already in use

If you have Skype on your PC along with your local server, I believe you must face the challenge of running your local server to test your web applications. Personally I ran into that a couple of years ago. The way I took as a solution wasn't favorable. I had to sacrifice using Skype on my PC for as long as I was working on my project.

Your local server listens on port 80 same with Skype and other application. Port 80 is meant to be their default listening port. So when you have multiple applications of such, they will run into a competition of who to use port 80. To tackle this problem, you may have to go into "httpd.conf" file in your Apache to configure the default settings so that the local server can have a dedicated port. Follow the steps below to have this done successfully.

STEP 1

  1. Start your XAMPP server or whatever server it is that you are using. 
  2. Start your Apache server. 
  3. Click Config button left of Admin.
  4. Look for httpd.conf and click on it to open. It will open in a notepad.

STEP 2

  1. In your notepad. Search for ServerName
  2. Make sure you choose the one with localhost:80 in front of it.
  3. Change that 80 to any number of your choice. Let's say 81 for example.


STEP 3

  1. Carry out another search. This time search for Listen.
  2. Make sure you choose the one with 80 in front of it.
  3. Change that 80 to the same number you used in the ServerName. 81 was what I used in that example. You can use something else but make sure they are the same.

NOTE:
In your httpd.conf, "#" means comment. This part of code will not be executed by the server.
Stop Apache and restart it to effect the change. Now you are good to go! Congrats you've made it!

December 14, 2015

How to send email using PHP

The ability to send emails straight from your website has its benefits. In as much as we have 
some of the IT companies offering this service free of charge; it will be wise for web developers 
to understand embedding email functionality on their site or that of their clients.

One of the benefits of this feature is sign up verification. If you've noticed, before you complete registration on most website, a link is generated dynamically and sent to your email account for verification. This refrains fraudsters or illegal use of someone else account. Making you the sole owner of that email account since you possess both the username and password.

In addition to account verification; you can use this feature to send newsletter to your subscribers. Keeping in touch with them.

The list can go on and on depending on what you intend to use this PHP email feature to do. Let's quickly dive into how to implement this on your website or blog.

First things first. You cannot be able to send mails using this PHP email feature on your local server. Your files for this exercise must be uploaded to a live server (i.e on the internet) before this feature can be used.

The PHP mail function is the core brain behind this operation. It returns a true or false value depending on the outcome of the task at hand. This function basically takes three parameters or arguments in addition to an optional one 
making four arguments in all. The first argument is of string data-type and refers to the recipient of the mail. This is commonly referred to as "TO". Whosoever you intend to send the mail to. Next to that is the subject of the mail; also of string data-type. Following is the body of the mail. That is the content of the mail. Lastly is an optional 
parameter which could be the Bcc or Cc feature. These parameters must be followed sequentially without interchanging their places to make sure the operation runs successfully.

Lets look at a little demo below to fully understand what this is all about.

//Variable defintion (Email information)
$to = "audience@talkingmail.com";
$subject = "Sending email...";
$body = "I will like to use this PHP email feature.";
$headers = "From: talktojoegee@gmail.com";

//send mail
if(mail($to,$subject,$body,$headers)){
 echo"Mail sent successfully";
}else{
 echo"Something went wrong.";
}
NOTE: Though headers is optional while sending email. If its not explicitly declared in your PHP script that would handle the email processing; it must be clearly defined in your php.ini file. It specifies parameters like From, Cc, Bcc. The additional parameters should be separated with a CRLF (\r\n) Since mail function returns a boolean value (i.e true or false), we are using an if conditional statement to determine whether the mail was delivered or not. One more thing before we wrap up on this. If you notice, the mail you receive most times displays maybe the company name instead of the email address of the sender. For example, talktojoegee@gmail.com sent an email to you. Instead of seeing the email address, the company name; lets say Light Technologies which is the company name for the talktojoegee@gmail displayed. To do that, we'll have to modify the header a little. Modify the header to -
$headers = "From: Light Technologies <talktojoegee@gmail.com>";
With this, we can say that we've come to the end to this sending email with PHP lesson. Explore more on this in areas like sending email to multiple people at the same time, sending an email with an attachment among others and feel free to share your findings here with us. Thank you.

December 13, 2015

Formatting Date in PHP from MySQL


Formatting date is one area where most PHP beginners face challenge. One thing that we ought to understand is that PHP and MySQL store dates in different formats. Where PHP uses Day,Month and Year in that order of arrangement; MySQL does the reverse of that. It starts with Year, Month and Day. This makes things a little bit confusing. Understanding their default formats will help you achieve whatever you want to use the date stored in the database to achieve whatever you want.

The benefit of this is that, dates can be displayed in a user-friendly manner that can be understood by virtually anybody.

The criteria to use this PHP functionality is to declare the MySQL column either as DATETIME or DATE data-type during table creation

The following will normalize the MySQL date format:
$defaultdate = strtotime($getmysqldefaultdate);
$mydatestyle = date( 'Y-m-d H:i:s', $defaultdate);

The line
$defaultdate = strtotime($getmysqldefaultdate);
accepts a date string (probably from MySQL database; $getmysqldefaultdate denotes that variable) and performs a series of operations to turn that string into UNIX timestamp. This is achieved using the
strtotime(date vairable);
PHP function.
The line below does the little magic that presents your date and time in a friendly manner.
$mydatestyle = date('Y-m-d H:i:s', $defaultdate);
It uses that timestamp and PHP's date function to turn that timestamp back into MySQL's standard date format.
Here is what each format character in the string above represents:

  • l = Full name for day of the week (lower-case L).
  • F = Full name for the month.
  • j = The day of the month.
  • Y = The year in 4 digits. (lower-case y gives the year's last 2 digits)
  • m = Numeric with leading zero.
  • d = Numeric with leading zero.
  • H = 24 hour time. numeric with leading zero.
  • i = Minute, numeric with leading zero.
  • s = Seconds; numeric with leading zero.
Considering the day and time of this post; our displayed date and time will be:


The table below will help you to format date into any style of your choice.

Day of Month
dNumeric, with leading zeros01-31
jNumeric, without leading zeros1-31
SThe English suffix for the day of the month st, nd or th in the 1st, 2nd or 15th.
Weekday
lFull name  (lowercase 'L')Sunday - Saturday
DThree letter nameMon - Sun
Month
mNumeric, with leading zeros01-12
nNumeric, without leading zeros1-12
FTextual fullJanuary - December
MTextual three lettersJan - Dec
Year
YNumeric, 4 digitsEg., 1999, 2003
yNumeric, 2 digitsEg., 99, 03
Time
aLowercaseam, pm
AUppercaseAM, PM
gHour, 12-hour, without leading zeros1-12
hHour, 12-hour, with leading zeros01-12
GHour, 24-hour, without leading zeros0-23
HHour, 24-hour, with leading zeros00-23
iMinutes, with leading zeros00-59
sSeconds, with leading zeros00-59
TTimezone abbreviationEg., EST, MDT ...
Full Date/Time
cISO 86012004-02-12T15:19:21+00:00
rRFC 2822Thu, 21 Dec 2000 16:01:07 +0200 



The table above will help you solve almost every problem related to date.