Zend_Mail attachment missing line October 14, 2008
Posted by ordinarywebguy in PHP, Zend Framework.3 comments
If you may ever find hard to make it work using Zend_Mail file attachment and following the documentation here http://framework.zend.com/manual/en/zend.mail.attachments.html, there’s a missing line.
$mail = new Zend_Mail();
$at = $mail->createAttachment($myImage);
$at->type = ‘image/gif’;
$at->disposition = Zend_Mime::DISPOSITION_INLINE;
$at->encoding = Zend_Mime::ENCODING_8BIT;
$at->filename = ‘test.gif’;$mail->send();
This should be
$mail = new Zend_Mail();
$myImage = file_get_contents($myImagePath);
$at = $mail->createAttachment($myImage);
$at->type = ‘image/gif’;
$at->disposition = Zend_Mime::DISPOSITION_INLINE;
$at->encoding = Zend_Mime::ENCODING_8BIT;
$at->filename = ‘test.gif’;$mail->send();
This should work fine now.
Calendar with Zend_Date October 10, 2008
Posted by ordinarywebguy in PHP, PHP Frameworks, Zend Framework.add a comment
As I’ve started to learn mvc zend framework, I’ve also started to use it as a stack framework library for some of my side projects. This module utilizes the Zend_Date component. See code:
class Calendar {
protected $year;
protected $month;
protected $day;
private $ctr = 0;public function __construct() {
$date = Zend_Date::now();
$this->year = $date->get(Zend_Date::YEAR);
$this->month = $date->get(Zend_Date::MONTH);
$this->day = $date->get(Zend_Date::DAY);
unset($date);
}public function viewMonth($year = NULL, $month = NULL, $computation = 0) {
$year = self::_setYear($year);
$month = self::_setMonth($month);$date = new Zend_Date(
array(
‘year’ => $year,
‘month’ => $month,
‘day’ => 01
)
);$monthDays = $date->get( Zend_Date::MONTH_DAYS );
$monthName = $date->get( Zend_Date::MONTH_NAME );
$firstWeekDay = $date->get( Zend_Date::WEEKDAY_DIGIT );$html = ‘<table border=”1″ cellpadding=”4″ cellspacing=”0″>’;
$html .= ‘<tr><td colspan=”7″ align=”center”><b>’ . $monthName . ‘ ‘ . $year . ‘</b></td></tr>
<tr>
<td>Sun</td>
<td>Mon</td>
<td>Tue</td>
<td>Wed</td>
<td>Thu</td>
<td>Fri</td>
<td>Sat</td>
</tr>’;$html .= ‘<tr>’;
$fWd = $firstWeekDay;
while ($fWd != 0) {
$html .= ‘<td> </td>’;
$fWd–;
}for ($i = 1; $i <= $monthDays; $i++) {
$this->ctr++;
$is7 = $firstWeekDay + $i;
$html .= ‘<td>’.$i.’</td>’;// Populate remaining <td>
if ($i == $monthDays) {
$date2 = new Zend_Date(
array(
‘year’ => $year,
‘month’ => $month,
‘day’ => $monthDays
)
);$tds = $date2->get( Zend_Date::WEEKDAY_DIGIT );
while ($tds != 6) {
$html .= ‘<td> </td>’;
$tds++;
}unset($date2);
}if ( ($is7 % 7) == 0) {
$html .= “</tr>\n<tr>”;
}}
$html .= ‘</tr>’;
$html .= ‘</table>’;return $html;
}public function viewYear($year = NULL, $month = NULL, $howManyMonths = NULL) {
$year = self::_setYear($year);
$howManyMonths = ($howManyMonths == NULL) ? 12 : (int) $howManyMonths;$html = ‘<table border=”0″ cellpadding=”4″ cellspacing=”2″>’;
if ($month != NULL) {
$howManyMonths++;
}$tmp = 1;
$col = 3;for ($i = 1; $i <= $howManyMonths; $i++) {
if ($tmp == 1) {
$html .= ‘<tr valign=”top”><td>’;
} else {
$html .= ‘<td>’;
}if ($month == NULL) {
$html .= self::viewMonth($year, $i, $i);
} else {
$html .= self::viewMonth($year, $month, $i);if (($month % 12) == 0) {
$year++;
}
$month++;
}if ($tmp == $col) {
$tmp = 1;
$html .= ‘</td></tr>’;
} else {
$tmp++;
$html .= ‘</td>’;
}
}$html .= ‘</html>’;
return $html;
}public function getMonth() {
return $this->month;
}public function getDay() {
return $this->day;
}public function getYear() {
return $this->year;
}private function _setYear($year = NULL) {
return ($year == NULL) ? $this->year : $year;
}private function _setMonth($month = NULL) {
return ($month == NULL) ? $this->month : $month;
}
}$cal = new Calendar;
$month = isset($_REQUEST['month']) ? (int) $_REQUEST['month'] : $cal->getMonth();
$day = isset($_REQUEST['day']) ? (int) $_REQUEST['day'] : $cal->getDay();
$year = isset($_REQUEST['year']) ? (int) $_REQUEST['year'] : $cal->getYear();echo $cal->viewMonth($year, $month);
It has still a lot of improvements. Feel free to modify it.
I love Zend Framework! It really rocks!!! April 22, 2008
Posted by ordinarywebguy in PHP, PHP Frameworks, Zend Framework.3 comments
Yes, I just can’t stop loving to learn it and being inspired by Dohdoh with his tutorials.
http://www.dohdoh.net/category/zend-framework/
By Zend
http://framework.zend.com/manual/videos
And by others
http://akrabat.com/zend-framework-tutorial/
WHY I LOVE IT?
For some reason, I just get tired of what they say “reinventing the wheel”, building my own PHP framework MK-PHP. Though MK-PHP really makes me better at PHP making everything you want on it (workaround) and so was ZF with lots of ready-made kick-ass vital libraries on it. And I don’t have to rewrite it. Just RTFM and viola!!! ![]()
PHP Level: +1
How to beautifully retrieve arrays in PHP? February 29, 2008
Posted by ordinarywebguy in PHP.2 comments
How?
Cast it to object type.
See example:
$info = array(
‘first_name’ => ‘Mitch Khenly’,
‘last_name’ => ‘Pascual’,
‘age’ => 1
);
$info = (object) $info; # the secret
You can now access it like this:
$info->first_name;
$info->last_name;
$info->age;
I find and see it more beautiful to retrieve / access arrays that way. It’s more readable. Agree?
Multiple Constructor in PHP January 31, 2008
Posted by ordinarywebguy in PHP, PHP 5, Programming.5 comments
How?
See code:
class MultipleConstructor {
private $info = ”;
function __construct() {
$argv = func_get_args();
switch( func_num_args() )
{
default:
case 1:
self::__construct1($argv[0]);
break;
case 2:
self::__construct2( $argv[0], $argv[1] );
break;
}
}function __construct1($value) {
$this->info = $value;
}function __construct2($value, $value2) {
$this->info = $value . ” ” . $value2;
}function get() {
return $this->info;
}
}$a = new MultipleConstructor(‘Value 1′);
echo $a->get();$b = new MultipleConstructor(‘Value 1′, ‘Value 2′);
echo $b->get();
Viola!!!
It’s Not a Bug :-p August 3, 2007
Posted by ordinarywebguy in PHP, Programming.4 comments
Just a follow up that I indeed deferred to my post: in_array PHP Function Bug.
I posted the said blog post in PHPUGPH and lead me to fully understand that it was not a bug. As Cruizer said:
if it’s not a bug, then it’s a gotcha. i don’t think that would be obvious to any programmer.
bad design, i’d say.
I definitely agree with that statement.
To clear up everything that it was not a bug. See code:
$str = “this is a string”;
if ($str == 0) {
print “true”;
} else {
print “false”;
} # end if
Output: true
Wonder why the output is “true”?
See here: http://www.php.net/manual/en/types.comparisons.php
In addition, RJ said:
As I understand the checking by PHP for the loose comparison of a string and an integer, the PHP engine will call the intval() function for the string before comparing it with the integer. Therefore “any string” == 0 will actually be intval(“any string”) == 0 which will result to 0 == 0. It means that it is not a boolean comparison.
Again, that’s not a bug, gotcha, or whatever you call it but instead a bad design.
in_array PHP Function Bug July 27, 2007
Posted by ordinarywebguy in PHP, Programming.6 comments
See the code:
<?php
$array1 = array(‘php’, ‘java’, ‘ruby’, ‘asp’);
$array2 = array(‘php’ => ‘PHP.net’, ‘java’ => ‘java.com’, 0 => ‘javascript’, 1 => ‘actionscript’);foreach ($array2 as $key => $value) {
if ( in_array($key, $array1) ) {
print “in array: $key <br />”;
} else {
print “not in array: $key <br />”;
} # end if} # end foreach
?>
Output:
in array: php
in array: java
in array: 0
not in array: 1
Expected Output:
in array: php
in array: java
not in array: 0
not in array: 1
Though the expected output above can be achieve via supplying TRUE to its third paramater which will set strict typing comparison between the value and array:
<?php
$array1 = array(‘php’, ‘java’, ‘ruby’, ‘asp’);
$array2 = array(‘php’ => ‘PHP.net’, ‘java’ => ‘java.com’, 0 => ‘javascript’, 1 => ‘actionscript’);foreach ($array2 as $key => $value) {
if ( in_array($key, $array1, TRUE) ) {
print “in array: $key <br />”;
} else {
print “not in array: $key <br />”;
} # end if} # end foreach
?>
How come that there’s a “0″ value on array $array1? That’s why I consider it a bug. I don’t know if someone has already experience this. I hope somebody could further explain this to me. I’ll also post this bug at PHP.net.
Note:
The script runs in PHP Version: 5.1.1
GeoAddress – WordPress Plugin 1.0 with Easy Google Map Class July 26, 2007
Posted by ordinarywebguy in GMaps, Google Maps, PHP, PHP Classes, Wordpress, Wordpress Plugin.4 comments
Firma Konrad Priemer had developed a WordPress Plugin named GeoAddress w/ the integration of EasyGoogleMap and HN Captcha. Both were class entries at phpclasses.org.
At the moment EasyGoogleMap has 1827 downloads already. I hope I’ve helped those 1827 users.
I have plans to improve EasyGoogleMap Class including the new features of Google Maps (draw lines, street view, traffic, etc). Time is only what I look and wait to pursue this.
Anyway, about GeoAddress:
Geo ADDRESS is a WordPress Plugin for providing a contact/address list in a Google map by means of markers to be represented can. The representation the Google map and/or the address form can be affected in the Adminbereich by means of numerous options.
By default under the Google map a complete list (activated) of the addresses is spent. Who would like to rather pack this list into a Sidebar, this can realize by means of the provided GeoAddress Sidebar Widget.
See more details:
http://www.php-developer-blog.de/50226711/geoaddresswordpressplugin_far_googlemaps.php
http://wp.php-guru.de/10/geoaddress-wordpress-plugin-10/
View Demo:
http://wp.php-guru.de/adressliste/
Download:
http://wp.php-guru.de/download-geoaddress/
Thanks Conny for using EasyGoogleMap Class!
Storing Objects to Sessions in PHP July 15, 2007
Posted by ordinarywebguy in PHP.6 comments
Its been a quite some time since doing something different other than having a CRUD Application projects. And this is all about to a simple shopping cart application. Yeah! So yummy having me to learn something new which I crave for most. I have this book of Larry Ullman titled “PHP Advance: For the World Wide Web”. It includes a section regarding ecommerce especifically shopping cart. Simple shopping cart without payment integration. It helped me a lot into completion of this project.
Take a look to its code:
ShoppingCart.class.php
<?php
class ShoppingCart {# public vars
var $mItems;# private vars
var $mPrice;
var $mName;function ShoppingCart() {
$this->mItems = array();
$this->mPrice = array();
$this->mName = array();
} # end functionfunction AddItem($item) {
if ($this->mItems[$item]) {
$this->mItems[$item] += 1;
} else {
$this->mItems[$item] = 1;
} # end if
} # end functionfunction DropItem($item) {
$this->mItems[$item] = 0;
} # end functionfunction ChangeQuantity($item, $quantity) {
if ($quantity == 0) {
$this->DropItem($item);
} else {
$this->mItems[$item] = $quantity;
} # end if
} # end functionfunction DisplayCart() {
global $config, $_SESSION;if (count($this->mItems) > 0) {
$prod = & new Data(’scart_product’);
$fields = $prod->GetFields();$ret =
‘
<script type=”text/javascript” src=”js/common.js”></script>
<script type=”text/javascript”>
function check_min_total() {
if (document.cart_frm.total.value < ‘ . $config['MIN_PRICE_ORDER'] . ‘) {
alert(“Cannot Proceed Checkout! \n Minimum Price Order: ‘ . $config['CURRENCY_SYMBOL'] . $config['MIN_PRICE_ORDER'] . ‘”);
return false;
} else {
//return true;
document.location = “checkout.php”;
} // end if
} // end function
</script><form name=”cart_frm” action=”cart.php?do=change” method=”post”>
<table>
<tr>
<td align=”center”><strong>Product Name</strong></td>
<td align=”center”><strong>Price Per Unit</strong></td>
<td align=”center”><strong>Quantity</strong></td>
<td align=”center”><strong>Total</strong></td>
<td align=”center”><strong>Delete</strong></td>
</tr>
‘;foreach ($this->mItems as $k => $v) {
if ($v != 0) {$data = $prod->GetOneData($fields, “WHERE product_id = $k”);
$sub_total = sprintf(“%01.2f”, ($v * $data['product_price']));
$total += $sub_total;$this->mPrice[] = $data['product_price'];
$this->mName[] = $data['product_name'];$ret .= “
<tr>
<td align=\”center\”>{$data['product_name']}</td>
<td align=\”center\”>{$config['CURRENCY_SYMBOL']}{$data['product_price']}</td>
<td align=\”center\”><input type=\”text\” name=\”id[$k]\” value=\”{$v}\” size=\”2\” maxsize=\”3\” /></td>
<td align=\”center\”>{$config['CURRENCY_SYMBOL']}{$sub_total}</td>
<td align=\”center\”><a href=\”cart.php?id=$k&do=drop\” onclick=\”return confirmation(‘Are you sure you want to delete product: {$data['product_name']}?’);\”><img src=\”images/button_delete.gif\” border=\”0\” title=\”delete\” /></a></td>
</tr>\n
“;
} # end if
} # end foreach$ret .= ‘
<tr>
<td colspan=”5″> </td>
</tr>
<tr>
<td align=”right” colspan=”3″><strong>Total</strong></td>
<td align=”center”><strong>’ . $config['CURRENCY_SYMBOL'] . sprintf(“%01.2f”, $total). ‘</strong></td>
<td align=”center”> </td>
</tr>
<input type=”hidden” name=”total” value=”‘ . $total . ‘” />
<input type=”hidden” name=”do” value=”change” />
</table><br />
<!–Alter the quantities by changing the above values and clicking here –>
<center><input type=”submit” name=”submit” value=”Change Quantities” /> <input type=”button” value=”Back to shopping” onclick=”document.location=\’retail_food_products.php\’” /> <input type=”button” value=”Proceed to checkout” onclick=”return check_min_total();” style=”font-weight: bold;” /></center>
</form>
<noscript><div align=”center” style=”font-weight: bold; color: #FF0000;”>In order to proceed checkout: Please turn on javascript in your browser. </div></noscript>
‘;$_SESSION['min_price_order'] = $total;
} else {
$ret .= ‘<br /><font color=”#CC0000″><big>Your shopping cart is currently empty.</big></font>’;
} # end ifreturn $ret;
} # end function
} # end class
?>
Take note: I’ve already modified it.
cart.php
<?php
require “config.php”;
require $config['LIBS_DIR'] . “MySqlDb.class.php”;
require $config['LIBS_DIR'] . “Data.class.php”;
require $config['INC_DIR'] . “db_open.php”;
require $config['DOC_ROOT'] . “config_settings.php”;
require $config['LIBS_DIR'] . “ShoppingCart.class.php”;
require $config['LIBS_DIR'] . “functions.php”;if (isset($_SESSION['cart'])) {
$cart = unserialize($_SESSION['cart']);
} else {
$cart = & new ShoppingCart();
} # end if$do = trim($_GET['do']);
$id = (int) trim($_GET['id']);switch ($do) {
case ‘add’:
$cart->AddItem($id);
print $cart->DisplayCart();
header(“Location: {$_SERVER['PHP_SELF']}”);
break;case ‘drop’:
$cart->DropItem($id);
unset($cart->mItems[$id]);
print $cart->DisplayCart();
header(“Location: {$_SERVER['PHP_SELF']}”);
break;case ‘change’:
if ($_SERVER['REQUEST_METHOD'] == “POST”) {
foreach ($_POST['id'] as $k => $v) {
if ($v == 0) {
$cart->DropItem($k);
unset($cart->mItems[$k]);
} else {
$cart->ChangeQuantity($k, $v);
} # end if
} # end foreach
print $cart->DisplayCart();
} # end if
header(“Location: {$_SERVER['PHP_SELF']}”);
break;case ‘display’:
default:
print $cart->DisplayCart();
break;
} # end switch$_SESSION['cart'] = serialize($cart);
?>
Notice on cart.php file on this line:
$_SESSION['cart'] = serialize($cart);
Its the way of storing objects to sessions.
And when retrieving it:
if (isset($_SESSION['cart'])) {
$cart = unserialize($_SESSION['cart']);
} else {
$cart = & new ShoppingCart();
} # end if
That’s it. Now I have my simple shopping cart application.
Path in choosing the right PHP Framework April 25, 2007
Posted by ordinarywebguy in PHP, PHP Blogs, PHP Frameworks.add a comment
Day by day as you code all the way you would wish to deliver your web projects in a more sophisticated and standardized coding style/pattern. I somehow feel that I need to use a suitable PHP Framework someday to one of my future web projects. But still looking for the right and best one.
Hasin Hayder is a well known PHP Guru from Bangladesh. His blog entry “Prelude to foundation: Its time to go for a better PHP Framework” tell us his story how he found the right PHP Framework for his projects. We may also find the same experience as Hasin do.
Read the full entry here: http://hasin.wordpress.com/2007/04/23/prelude-to-foundation-its-time-to-go-for-a-better-php-framework/
bad design, i’d say.