Monthly Archives: February 2011

Attachment Email with PHP

Attachment Email with PHP

In this article I will try to share how to send an Attachment Email with PHP. You can use this for sending attachment like file backup. In this case I just we already implementing my previous article Mysql Backup with PHP, and have a file with the name $database_$current.sql.gz.

The Attachment Email with PHP code should be like this:

$all_to = "bla@bla.com;ble@ble.com";
$arr_to = explode(";",$all_to);
foreach($arr_to as $to)
{
	$subject = "Attachment Email";
	$random = md5(date('r', time()));
	$headers = "From: mhseptiadi.wordpress.com";
	$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-$random\"";
	$attachment = chunk_split(base64_encode(file_get_contents("$database_$current.sql.gz")));

	$content = "
	--PHP-mixed-$random
	Content-Type: application/gzip; name=$database_$current.sql.gz
	Content-Transfer-Encoding: base64
	Content-Disposition: attachment

	$attachment
	--PHP-mixed-$random--";

	if(@mail($to, $subject, $content, $headers))
	{echo "Mail sent successfully to ".$to."
";}else{echo "Mail sent failed to ".$to."
";}
}

You can change $database and $current with anything that you need, and also you need to change the $all_to variable. You can send multiple emails by give all the contacts with sparator “;” in $all_to variable. At the end, we just made code to send Attachment Email with PHP.

Good Luck.

http://septiadi.com/2011/02/28/attachment-email-with-php/


Mysql Backup with PHP

Mysql Backup with PHP

Database is one of the most important thing in building a website. Therefore, making database backup is also very important. Database backup will avoid loss if there is a crash in our database.

In this article I will try to share a method that i use to make Mysql Backup with PHP. The frist thing that we need to do is making the code it self.

The php code:

$current = date("Y-m-d_H-i-s");
$host = "";//fill it with host name
$user = "";//fill it with user name
$pass = "";//fill it with password
$database = "";//fill it with database name
exec("mysqldump --opt --host=$host --user=$user --password=$pass $database | gzip --best > $database_$current.sql.gz");

That is it, the code will automatically make a backup with extention .sql.gz and save it with the same directory with your code.

You can combine this code with Attachment Email with PHP

Cogratulation you just learn how to make Mysql Backup with PHP.

Good luck.

http://septiadi.com/2011/02/28/mysql-backup-with-php/


Zebra Strip with CSS3

Zebra Strip with CSS3

In this tutorial, I would like to share about how to make Zebra Strip with CSS3. Just to keep in mind, not all browser are compatible with CSS3.

Fist, type the CSS3 code:

.septiadi tr:nth-of-type(even){
	background: lightblue;
}

Second, make the table with html code:

<table class="septiadi">
<tbody>
<tr>
<td>asdfadfas</td>
</tr>
<tr>
<td>asdfadfas</td>
</tr>
<tr>
<td>asdfadfas</td>
</tr>
<tr>
<td>asdfadfas</td>
</tr>
<tr>
<td>asdfadfas</td>
</tr>
<tr>
<td>asdfadfas</td>
</tr>
<tr>
<td>asdfadfas</td>
</tr>
</tbody>
</table>

We make a class in this css code to avoid the css style is implemented by other table.

The page should be like:

zebra strip

zebra strip

You can combine this css style with Highlight Table Row While Mouse Hover

We just made a simple Zebra Strip with CSS3 with less than 5 minutes 🙂

Good luck.

http://septiadi.com/2011/02/18/zebra-strip-with-css3/


Highlight Table Row While Mouse Hover

Highlight Table Row While Mouse Hover

In this post I want to share about how to Highlight Table Row While Mouse Hover. No javascript but only CSS.

Here are the CSS code:

tr:hover{
	background-color:red;
}

Next the HTML code:

<table border="1">
<tr><td>test</td></tr>
<tr><td>test</td></tr>
<tr><td>test</td></tr>
<tr><td>test</td></tr>
<tr><td>test</td></tr>
<tr><td>test</td></tr>
<tr><td>test</td></tr>
</table>

Done, we just make a simple Highlight Table Row While Mouse Hover.

Good luck.

http://septiadi.com/2011/02/17/highlight-table-row-while-mouse-hover/


Connecting PHP to Mysql

Connecting PHP to Mysql

Connecting PHP to Mysql is very simple and easy to do.

What you need is to type code below into your php file:

$host = 'localhost';//host name, in this case we use localhost
$user = 'root';//user name, in this case we use root
$pass = 'password';//password, in this case we use password

$conn = mysql_connect($host, $user, $pass)
or die('Connection Failed');//will show "Connection Failed" if the connection failed

$db_name = 'member';//declare the database that will be used
mysql_select_db($db_name);//selecting the data base

Finish. We just done Connecting PHP to Mysql.

http://septiadi.com/2011/02/17/connecting-php-to-mysql/


Simple PHP Paging

Simple PHP Paging

In this post, I’ll try to share how to make a Simple PHP Paging. The first step we have to do is to prepare data that will be shown. In this case, I use mysql to store the data.

Let say, we have a table in mysql database with the name ‘match’ and have 999 rows. And, We want to show 20 members each page. I asume we already connect the php file with database. See (Connecting PHP to Mysql).

The first code is to define the links of page:

if(!isset($_GET['page'])){$page = 1;}else{$page = $_GET['page'];}//get the page
$row_per_page = 20;//defined rows per page
$rows = mysql_num_rows(mysql_query("SELECT * FROM  `match`"));// find out how many rows available
$pages = ceil($rows/$row_per_page);//find out how many pages
$width_links = 3;//defined width links

$start_link = $page - $width_links;//start link
if($start_link<1)$start_link=1;//set start link into 1 if less than 1
if($start_link1){
	echo '<a href="?page=1">First</a>&nbsp;.....';
}
for($i=$start_link;$i<$page;$i++){
	echo '&nbsp;<a href="?page='.$i.'">'.$i.'</a>';
}

echo '&nbsp;&nbsp;&nbsp;<strong>'.$page.'</strong>&nbsp;&nbsp;';

$end_link = $page + $width_links;
if($end_link>$pages)$end_link=$pages;//set end link into max page if more than max page
for($i=$page+1;$i<=$end_link;$i++){
	echo '&nbsp;<a href="?page='.$i.'">'.$i.'</a>';
}
if(($end_link)<$pages){
	echo '&nbsp;.....&nbsp;<a href="?page='.$pages.'">Last</a>';
}

In our code here, we will see not all of the available links. Because if the available page is … let say, 50, then it will be problem if the links for page 1 till page 50 is shown. Therefore we make a cut for the links. Then it will show links like:

Optional Code (CSS) to make no underline for links:

.link{
	text-decoration:none;
}

The second code is to show the data:

$limit = ($page - 1)*$row_per_page;//set the limit $row_per_page means that we will use defined rows in $row_per_page per page
$e=mysql_query("SELECT * FROM `match` LIMIT ".$limit.",".$row_per_page);//the query will show 20 rows each page
echo '<table><tbody>';
while($r = mysql_fetch_array($e)){	echo '<tr><td>'.$r['match_id'].'</td><td>'.$r['desc'].'</td></tr>';}
echo '</tbody></table>';

TARRRA… we just make a Simple PHP Paging.

Good Luck.

http://septiadi.com/2011/02/17/simple-php-paging/


Publish Code in WordPress

Publish Code in WordPress

It is may be dificult for people to Publish Code in WordPress. I have tried to use <textarea> and it wont work perfectly. Change < into &lt; is not user friendly. However, Publish Code in WordPress is not that hard.

First, know what code you are using, it might be one of these codes:

Bash —> bash
C++ —> cpp
C# —> csharp
CSS —> css
Delphi —> delphi, pascal
Diff —> diff
Groovy —> groovy
Java —> java
JavaScript —> jscript, javascript
Perl —> perl
PHP —> php
Plain text —> plain, text
Python —> python
Ruby —> ruby
Scala —> scala
SQL —> sql
VB —> vb
XML/HTML —> xml, html, xhtml

Second, type the wordpress tag