Archive for the 'PHP' Category

A Charity Donation Recognition System Using PHP, MySQL, JavaScript And DOM

Recently asked on Yahoo! Answers:

Divide photo in 5,000 pieces with rollover text?
we are raising money and selling support tickets. To show our progress we are going to start with a black and white image split in 5,000 pieces then as each piece is sold we want to turn it into color.

When you roll over each piece the person who purchased that support ticket will have their name show up as well as a personal message.

any opinions on how this should be built? The admin section will all be managed manually, so we’ll just need a backend for the admin person to go in and make the changes manually.

This seemed an interesting challenge to me; not difficult, but with multiple steps that no beginner could hope to properly tackle on his own without some help.

This project calls on almost all the basic skills a competent Web developer should possess: Image editing; database design and simple queries; DOM manipulation; security and back-end systems, and therefore, it gives me my first real opportunity in quite some time to address a project in full scope.

As always, I’ll have a link to a working demo and code you can download at the end of this entry.

Read the rest of this entry »

Multilingual Web Pages Via PHP, Arrays And MySQL

Asked recently on Yahoo! Answers:

What is the best method to create a multi-lingual website?
Do we use xml, php, or what??? and how, help appreciated.

There are many different ways to go about having a Web page appear in multiple languages, and I may address others in upcoming articles. The approach I am going to focus on in this post assumes you already have your translations in hand, and simply want to give users a choice of languages in which your page can appear.

In the future, I may address ways of determining the default language to present to a user, based on his browser’s user-agent string; translating text on-the-fly via AJAX and one of the free translation Web sites (even ones that don’t have an API), and using alternative data sources, depending on interest in this article.

For now, two demos using PHP: The first will employ an array, the second will use a database as its back end. As always, I will have a link to a working demo and code you can download at the end of the discussion.

Read the rest of this entry »

A Simple Link Rewriting Script In PHP

Recently asked in Yahoo! Answers:

Javascript using var to enter domains?
My work uses this great code at the top their page….
<script type=”text/javascript”>
var imghost = ‘http://blah.images.com;
var ahost = ‘http://www.website.com;
var khost = ‘http://www.website.com/future/;
</script>
Then in the HTML code the following is put to make it work:
<img src=”[imghost]/lp/blahblah.jpg”

When the page loads and the user views it it reads: src=” http://blah.images.com /lp/blahblah.jpg”

It automatically puts the domain information in case you need to change it. In my case, I’d like all items to point to “.com/future/” then when I’m done with the new site simply change that original java script to remove the “future” and push the testing site live. My company uses .pd files…but that shouldn’t make a difference. I’ve analyzed all the code and can’t make it work on my site, doesn’t look like I’m missing anything. Suggestions? Or maybe a different way to do this? I’m using .php for my site. Thanks!

Actually, this is an example of it making more sense to use a server-side scripting language rather than JavaScript. The client needs to enable JavaScript; if he hasn’t enabled it, the links won’t be updated. PHP ensures we get good links.

So, let’s do this. No code to download this time; you’ll need to pay attention and copy and paste properly from the code here.

Read the rest of this entry »

Variable PHP Recordset / Results Set Pagination

Recently asked on Yahoo! Answers:

Paginate in php: 3 items on front page-then 1 item on next pages?

I know how to paginate mysql results, but have only been able to have the same number of results appear on page 1 and the remainder of the clicked pages. However, I want to have a different set of results on the first page, and then on pages 2 and up, I want a different number of results. For example, on page 1, I want 3 results, then on pages 2 and up, just one result. Thanks.

I’ve covered this several times before, but never as a seperate topic, and it’s often asked: How do you paginate a PHP recordset?

It’s fairly simple, using mysql_data_seek() and a few checks to ensure our variable stays in range. We begin by assuming you will use one page to display your recordset, and will keep passing a page number parameter via querystring to that page, like this:

http://www.mysite.com/records.php?page=1

If you want to move to the second page of records, the url would be:

http://www.mysite.com/records.php?page=2

And so on. Let’s begin with the basics of PHP results set pagination; after that, the specific request.

Read the rest of this entry »

An Image-Based ‘Checkbox’ Via JavaScript / DOM

Recently asked on Yahoo! Answers:

Use a custom image for check boxes???? Please help, I”m going insane over such a simple thing.?

Heya.

Don’t worry, I’m not really going insane - it’s just kind of frustrating because such a simple little thing is giving me trouble. I want to use custom images for check boxes on a HTML page.

The best way I can describe this is that I want a bright red flashing gif to be there by defualt, then for that to change to a green flashing gif when it’s clicked.

I don’t care if it’s done using a real checkbox, I don’t need it to actually work. I only need the picture to change when it’s clicked, and stay changed after that unless clicked again.

Apparently either it’s such a simple thing to do that no one has ever talked about it, or no one has ever needed to do it before and never given it any thought, because I’ve looked for help with this all afternoon and tried 6 different ideas of my own with no luck. I can write a fully functional & good looking page in notepad but not get a dumb little image to change when it’s clicked. Go figure.

Please help! Thank you :)

I’ve written about using JavaScript / Document Object Model (DOM) manipulation to change images onclick, onmouseover or via virtually any other event on many occasions. But it’s the idea of using an image as a checkbox that got me curious: Sure, we can change any image onclick, but can we also get the id attributes of all the checked items for use in form processing?

Yes, we can! So we’ll tackle this question in three parts: Example 1 will simply change images on click; Example 2 will let us find out which images are “checked,” via a JavaScript array; and Example 3 will assign the checked images to regular, old HTML check boxes, so you can process the form as you would via any HTML postback.

As always, I will provide links to working demos at the end of each example.

Read the rest of this entry »