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.
