Deprecated: Assigning the return value of new by reference is deprecated in /home/inh2ive/devtalk/wp-settings.php on line 472

Deprecated: Assigning the return value of new by reference is deprecated in /home/inh2ive/devtalk/wp-settings.php on line 487

Deprecated: Assigning the return value of new by reference is deprecated in /home/inh2ive/devtalk/wp-settings.php on line 494

Deprecated: Assigning the return value of new by reference is deprecated in /home/inh2ive/devtalk/wp-settings.php on line 530

Deprecated: Assigning the return value of new by reference is deprecated in /home/inh2ive/devtalk/wp-includes/cache.php on line 103

Deprecated: Assigning the return value of new by reference is deprecated in /home/inh2ive/devtalk/wp-includes/query.php on line 21

Deprecated: Assigning the return value of new by reference is deprecated in /home/inh2ive/devtalk/wp-includes/theme.php on line 623
DevTalk » 2008 » October

A nifty Javascript library


Deprecated: Function split() is deprecated in /home/inh2ive/devtalk/wp-content/plugins/google-analytics-for-wordpress/googleanalytics.php on line 379

I recently found a Javascript library which adds a nice effect to a website. It’s called jParalax and what it does is create layers from an element and its children to give a nice effect. The layers are absolutely positioned and move in response to the mouse, to give a parallax effect. They have the script available for download, and many nice demos.

Here is the link.

Posted in: Javascript

Fun with Javascript


Deprecated: Function split() is deprecated in /home/inh2ive/devtalk/wp-content/plugins/google-analytics-for-wordpress/googleanalytics.php on line 379

Deprecated: Function split() is deprecated in /home/inh2ive/devtalk/wp-content/plugins/google-analytics-for-wordpress/googleanalytics.php on line 379

I must admit I always felt fascinated by the limitless possibilities of creating rich user interfaces using Javascript. You can see all sorts of examples out there, but it’s always a lot of fun to mess around with the code to get your own results, plus, as a web developer, it keeps you in shape.

The latest experiment resembles the igoogle interface, meaning you can drag and drop divs around, and also have certain areas where a div can snap to a predefined position.

So, you can check it out here, and if you want to look at the source code, click here.

I didn’t have any idea what to call this one, so I named it DivOps, short for Div Operations…

I used Firefox for development, so please don’t comment on the way it works on IE and other browsers. I have checked it on IE just for the hell of it, and except the fact that it calculates to margins differently, it works just the same.

Posted in: Javascript

AJAX shopping cart


Deprecated: Function split() is deprecated in /home/inh2ive/devtalk/wp-content/plugins/google-analytics-for-wordpress/googleanalytics.php on line 379

Deprecated: Function split() is deprecated in /home/inh2ive/devtalk/wp-content/plugins/google-analytics-for-wordpress/googleanalytics.php on line 379

Deprecated: Function split() is deprecated in /home/inh2ive/devtalk/wp-content/plugins/google-analytics-for-wordpress/googleanalytics.php on line 379

Hello. This time we’ll discus writing a simple AJAX-driven shopping cart. Again, a challenge that has many solutions, but I prefer to write my own code. Especially for these posts… Like always, we need to discus the languages used and then we can talk about the code. Also, I will provide the full source code this time. You can find it here.

Also, you can see the project here.

Since we’ll use AJAX, we’ll have to write PHP and Javascript code, and of course HTML with a bit of CSS (for the interface). The JS part of the application will send requests to a PHP script, which takes the id of the item that needs to be added, and then display the response text in a container, which is our cart. The JS function sends via XMLHttpRequest two parameters, the first being the id of the item, and the second being a add/subtract ’switch’ (1 - add, 0 - subtract).

Now for the server side part. Since we need to manipulate a series of items, the first thing that comes to mind, is an array. And furthermore, since we need to pass this item from one file to another, we can uses sessions. So, we use a session-stored array to hold out items like this:

Array(
    [item_id1] => quantity1
    [item_id2] => quantity2
    [item_id3] => quantity3
    . . .
);

This is how it looks in PHP:

<?php
session_start();
$_SESSION['items'] = array();
?>

This way we can keep all the information in one array (rather than keeping item_id’s and quantity separately, or using objects).

So, what happens when we send a request? Well, first, the server side script recieves the two parameters, the id and the ’switch’; If the id that was sent is equal to 0, then it means the user wants to empty the shopping, so we do just that, we empty the array and then we exit.

session_start();
$id = $_POST['id'];

if($id == 0)
{
	array_splice($_SESSION['items'], 0);
	exit();
}

If that’s not the case, we need to manipulate an item. We do this by checking if a key equal to the sent id hasn’t been added to the array. In this case, then we simply add an item, else if the switch is equal to 1, we increase the quantity of the item by 1, and if it’s equal to 0, we subtract by 1.

if(!array_key_exists($id, $_SESSION['items']))
	$_SESSION['items'][$id] = 1;
else
	($_POST['add']) ? $_SESSION['items'][$id]++ : $_SESSION['items'][$id]--;

The final step is sending the response text back to the client side, so we just display the result. We parse the array, and display the data in the form we want it. In this case I have used a table:

<table>
<?php
foreach($_SESSION['items'] as $key => $quantity)
{
	if($quantity <= 0)
		unset($_SESSION['items'][$key]);
	else
	{
	?>
	<tr>
		<td width="50"><div class="item<?php echo $key;?>"></div></td>
		<td width="50">x <?php echo $quantity;?></td>
		<td width="150" align="right">
			<a href="#" onclick="sendRequest(<?php echo $key;?>,1);">+</a>
			 /
			<a href="#" onclick="sendRequest(<?php echo $key;?>,0);">-</a>
		</td>
	</tr>
	<?php
	}
}
?>
</table>

Ok. Now that the text is formated nicely, we can add it to the cart, I mean, we let the client side script handle that… So that’s basically it. I won’t go into to much detail about the code, since you can find the source code here.

Feel free to experiment with it and post you version here. If you need any further info, please ask.

Posted in: AJAX