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

Sunday, March 20, 2011

Show posts from your SMF fourm in your home/index page

SMF forum is quit popular open source forum application used by many websites as their main bulletin board. Some time we need to grab posts,topics from the board and show them on our home/index page or anywhere on our site. To make the things much easier, SMF provides all this in one script where we call a function with arguments and dress it up according to our needs. File is SSI.php and can be found in the root folder of the forum. You can also add extra parameters/piece of code to your SSI.php. In this post i will show you how to retrieve posts from SMF board with additional information like board, post author, date & time plus top poster and board statistics.

Read more

MMA

Wednesday, December 22, 2010

Enable mod_rewrite on openSUSE 11.3 [Linux]

Last night i installed vanilla forum the latest version(2.0.16), but before that i got into trouble like ‘cannot install vanilla forum because the database host provided is wrong’ some thing like that type of error. Which was basically by .htaccess file. I finally installed it but after some time figuring out the mod_rewrite ghost. Actually it’s quite easy to enable it, there was one thing missing, which took some of my time to figure it out. Lets start how to enable the ghost.

Read more...

MMA

Saturday, October 24, 2009

Email Parsing and download attachment with IMAP in PHP

Last couple of days, i was working on a script where i need to download an attachment from a windows mail server and then read the contents inside the attachment and save it in database.

Tasks:
1- Read all unseen messages in the inbox.
2- Search for a specific subject.
3- Download the attachment(zip file) to local PC.
4- Unzip the file and extract the file inside it.
5- Read its contents inside the file.
6- Save the contents to a table(MySQL database).
7- Delete the extracted file after saving entries into table.
8- Move the zip file to a backup directory.
9- Do calculations on the data saved in the table.

Actually i had to do this for one of my project, where the sensors gateway will send mail to the windows server each hour, and i have to read the mail from a Linux machine.

As most of the work is completed on these scripts, i tested it under cron on local PC, and is working like a charm. So i am thinking to share the scripts with the community, maybe would help someone in the near future.

I have written 3 PHP scripts(pure PHP) with some MySQL tables to use for data saving.
I will start in the next post from the 1st script which is mail_parsing.php. I used PHP IMAP function to access the mail server.

Until then have a good time.
See you in the next post.

ANL

Monday, October 12, 2009

Listbox values in PHP with MySql

Today got some free time to write a simple tutorial for PHP with MySql for listbox.
So lets start.

Mostly we use drop down on many occasions, populated from database dynamically or giving it values statically. And then use PHP code to parse it and store it in database or get our desired results.
One type of that dropdown is listbox, where we can select multiple entries.
Here is a sample example of it:


In the above code:
multiple: represent multiple list for values
size: will show 2 values in the list, and others will be shown by scrolling.
name: is holding name in array.

Now lets take a MySql example:


Now lets jump to PHP to get the desired results.
There are many ways to get the results like:
foreach, for and etc. As we know, POST is also an array. So the best bet is to use array.

$drop_list = $_POST['drop_list'];
$_SESSION['drop_list'] = $drop_list;
if (in_array(1, $drop_list)) { //id 1
$query1 = "SELECT * FROM test2 where id='1'";
$res1 = mysql_query($query1) or die(mysql_error());
//You can do some other stuff here too
}


You can use multiple if statements to verify the variable resulted value.
You can save the value in session to use it globally in your scripts.
Example:
if (in_array(2, $drop_list)) { //id 2
$query1 = "SELECT * FROM test3 where id='2'";
$res1 = mysql_query($query1) or die(mysql_error());
//You can do some other stuff here too
}
if (in_array(3, $drop_list)) { //id 3
$query1 = "SELECT * FROM test4 where id='3'";
$res1 = mysql_query($query1) or die(mysql_error());
//You can do some other stuff here too
}


Apart from the above example, there are many ways like foreach, if you don't want to just use one by one. Like:

foreach($_POST['drop_list'] as $drop_list){
//Do your stuff here like, updating multiple records at one shot.
}


Best of luck.
ANL

Thursday, April 2, 2009

$_SERVER['PHP_SELF'] in PHP

Hi to all, After a long time i m back to this blog too.

$_SERVER is an array defined in PHP and it stores information about your server and execution environment information.

$_SERVER['PHP_SELF'] is an array element points out the filename of the currently executing script. For example, if you run www.example.com/index.php, $_SERVER['PHP_SELF'] would be /index.php. This is relative to the document root. This is useful to referring HTML forms and other element.

for example:


ANL

Thursday, December 11, 2008

Hide .php from users to make your url more friendly

I have written an article named "How to rewrite URL in apache".
Today i am sharing with my readers and PHP programmers.
First of all we will check that either mod_rewrite in apache load modules is enabled or NOT.
In Linux openSUSE:
By default, openSUSE doesn't enable the mod_rewrite rewrite module. It's installed, but not enabled.
So lets enable it,
1. Edit the file /etc/sysconfig/apache2 as root:
i. search for APACHE_MODULES, you should find a line like this
APACHE_MODULES="suexec access actions alias auth auth_dbm autoindex cgi dir env expire include log_config mime negotiation setenvif userdir ssl php5"
ii. Add 'rewrite'(without quotes) to the content in the list between the "".
iii. Save the changes and quit
2. run SuSEconfig to update the apache configuration files.(From terminal)
3. run /etc/init.d/apache2 restart to restart the Apache server.(From terminal)
To check either mod_rewrite is enabled or not. phpinfo();, write in editor, save it in the root directory and open it in browser.
Search for word "mode_rewrite"(without quotes). If you find it, it mean rewrite is enabled. if not, then check the steps again and try.

Now you have to do some tweaking of default-server.conf file.
1. Edit the file /etc/apache2/default-server.conf with your prefered editor.
i. Search for AllowOverride - it should be below the line Directory "/srv/www/htdocs"
ii. Change AllowOverride None to AllowOverride All - this will allow custom .htaccess rewrite rules.
iii. Save your changes and exit
2. run SuSEconfig to update the apache configuration files.
3. run /etc/init.d/apache2 restart to restart the Apache server.

Now every thing is fine and running, lets test it. that it is working or not.
Create a directory with any name, i create mod_rewrite, open editor and write the following code there.

if($_GET['link']==1){echo"You are not using mod_rewrite";}
elseif($_GET['link']==2){echo"Congratulations!! Your Apache mod_rewrite test is successful";}
else{echo"Linux Apache mod_rewrite Test";}

//make it url as by href method.
"rewrite.php?link=1"LINK1 = rewrite.php?link=1
"link2.html"LINK2 = link2.html

save it as rewrite.php

Now write:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^link([^/]*).html$ rewrite.php?link=$1 [L]

and save it as .htaccess in the same directory.
Open rewrite.php in browser and click on link1, and then on link2. If you see the message "Congratulations!! Your Apache mod_rewrite test is successful", means that, its working.

In WINDOWS:
How to enable mod_rewrite module in apache in xampp, wamp?

1) Find the “httpd.conf” file under the “conf” folder inside the Apache’s installation folder.
2) Find the following line “#LoadModule rewrite_module modules/mod_rewrite.so” in the “httpd.conf” file.You can do this easily by searching the keyword “mod_rewrite” from find menu.
3) Remove the “#” at the starting of the line, “#” represents that line is commented.
4) Now restart the apache server.
5) You can see now “mod_rewrite” in the Loaded Module section while doing “phpinfo()”.



The rest of the testing is same as above.

ANL

Thursday, December 4, 2008

Round function

A simple post to show how to use round function in PHP.

Lets take a variable holding a number with decimals:
$number = 21.25116;

Now want to round this number to 3 decimal.
So:
$number = round($number,3);
echo $number;
The result will be something like: 21.251

If $number = 21.2569
then result will be: 21.257

If $number = 21.259652;
Then result will be: 21.26

ANL

Tuesday, November 11, 2008

Explode Function

So here we go for the 1st post in this blog.

Explode function, i think many of us already know of this function.
Explode is very useful function, when it comes to breaking up lines or words.
There are several ways to use this function.
I will give some examples, that how to use it.

lets assume that you stored datetime in database as datetime. the way your date looks is:
2008-11-11 12:25:20

But now you want, to breakup the date and time into two parts.
So lets break it:
$date = "2008-11-11 12:25:20";
$datetime = explode(" ",$date);

The above code already break your datetime into 2 parts now lets echo it as it is in array.
echo $datetime[0]; For date
echo $datetime[1]; For time

The 1st echo will give you just date: 2008-11-11
The 2nd echo will print just time: 12:25:20

You can break this into more parts, if you want, like seconds, minutes, hours and same with date.
For example lets break time.
$time = explode(":",$datetime[1]);
echo $time[0]; For hours
echo $time[1]; For minutes
echo $time[2]; For seconds

I will also extend this to the other post later "Calculating Unix time in PHP (part-1)". which is here: Link

Regards
ANL