iBlog random » php http://blog.zurka.us Just another WordPress weblog Tue, 28 May 2013 20:28:47 +0000 en-US hourly 1 http://wordpress.org/?v=3.6 Generate QR Codes with php (EASY) http://blog.zurka.us/generate-qr-codes-with-php-easy/ http://blog.zurka.us/generate-qr-codes-with-php-easy/#comments Thu, 24 Jun 2010 22:30:19 +0000 nasal http://blog.zurka.us/?p=1079

There you go, a function that will generate a QR Code with the help of a function hosted on the Google API servor.

function generate_qr($url, $size ='150', $imgsize = false, $EC_level='L', $margin='0') {
    $url = urlencode($url);
    return '<img src="http://chart.apis.google.com/chart?chs='.$size.'x'.$size.&cht=qr&chld='.$EC_level.'|'.$margin.'&chl='.$url.'" alt="QR code" width="'.($imgsize ? $imgsize : $size).'" height="'.($imgsize ? $imgsize : $size).'" />';
}

You call it like this:

<?php echo generate_qr('http://blog.zurka.us'); ?>

And get something like this:

blog.zurka.us qr code

Try pointing your favorite code reader at it!

PS: I just noticed that it doesn’t work well because of the dark background, try it here.

]]>
http://blog.zurka.us/generate-qr-codes-with-php-easy/feed/ 4
MySQL variant for PHP’s in_array http://blog.zurka.us/mysql-variant-for-phps-in_array/ http://blog.zurka.us/mysql-variant-for-phps-in_array/#comments Wed, 29 Jul 2009 13:54:56 +0000 nasal http://blog.zurka.us/?p=1021

So you have an array, let’s call it $array.

$array = array('one', 'two', 'three');

And you want to do a MySQL query that will check if one of the values in the array are present or similar or whatever. In PHP it would look like this:

if (in_array('one', $array))

and it would return true. Now to do this in MySQL we can use (along other things but I use this one) the IN statement.

mysql_query('select something from table where one IN ("' . implode('","', $array) . '")');

Why is this useful? Let’s say you’ve got an array of file extensions (pictures) and you have a database with some files in it, pictures, music, videos. You want to display only the pictures, because you want to display the last 15 uploaded pictures. Let’s say you have the filename and extension (filename = hack.jpg) stored in your database. Let’s see an example of what you could do…

mysql_query('select id, name, filename from uploads where substr(filename, -3) IN ("' . implode('","', $pics) . '") order by id desc limit 12');

And that’s it. Easy peasy!

Have swing!

]]>
http://blog.zurka.us/mysql-variant-for-phps-in_array/feed/ 1
Fetch MP3 Tags from Last.fm Using PHP http://blog.zurka.us/fetch-mp3-tags-from-last-fm-using-php/ http://blog.zurka.us/fetch-mp3-tags-from-last-fm-using-php/#comments Sun, 21 Jun 2009 15:20:08 +0000 nasal http://blog.zurka.us/?p=1014

Today I’m gonna show you a script that I did for a project of mine that fetches the tags from LastFM and updates the database inserting the top 5 tags from their database and leaves your tags as they are.

I believe it could be a lot less messy but I am too lazy to make it more understandable :D You update the tags using yoursite.com/?autotag=id (of the song from your database). You could as easily do a loop and update every song in the database. But why would you :D So let’s go with my messy code!

Ps: you must get your API key on their site.

.

$f = mysql_fetch_assoc(mysql_query('select name, tags from songs where id = "' . $_GET['autotag'] . '"'));
$e = explode('-', $f['name']);
$url = @simplexml_load_file('http://ws.audioscrobbler.com/2.0/?method=track.getinfo&api_key=YOUR API KEY&artist=' . $e[0] . '&track=' . trim($e[1]));
$t_cnt = count($url->track->toptags->tag);
$t = explode(',', $f['tags']);
$tags = array();
for ($i = 0; $i < count($t); $i++) {
    if ($t[$i] != '') $tags[] .= trim($t[$i]);
}
for($j = 0; $j < $t_cnt; $j++) {
    array_push($tags, (string)$url->track->toptags->tag[$j]->name);
}
$tags = array_unique($tags);
$ct = count($tags);
for ($k = 0; $k <= $ct; $k++) {
    if ($tags[$k] != '') $tagss .= ($k >= 1 ? ', ' : '') . $tags[$k];
}
//mysql_query('update songs set tags = "' . $tagss . '" where id = "' . $_GET['autotag'] . '"');
exit($tagss);

It reads the name of the song from the database. The name should be Artist – Title, as it then explodes this at the – and presumes the first piece is the artist and the second is the title of the song. This are the info that LastFM wants you to include in the URL if you want to get the song info.

Next, we read the xml file with the song information. We explode our preexistent tags from the database and we count the tags in the xml file.

We create a new array $tags, which will include our preexistent tags (should be separated by commas (,)). We push our tags into it and then we push the tags from LastFM into it.

We use the array_unique function to delete duplicate tags, if there are any. When we finish doing that, we echo the tags into a new variable, $tagss.

At the end, we could update the database entry (if you uncomment the commented line) or just exit the function and echo our new $tagss variable.

That’s it, hope you understood anything.

Have swing!

]]>
http://blog.zurka.us/fetch-mp3-tags-from-last-fm-using-php/feed/ 2
Help me Test my Mailing List System http://blog.zurka.us/help-me-test-my-mailing-list-system/ http://blog.zurka.us/help-me-test-my-mailing-list-system/#comments Thu, 28 May 2009 10:55:15 +0000 nasal http://blog.zurka.us/?p=942

I’m developing my very own mailing list system and need you to help me test it :) All you have to do is subscribe to it on the right, it is free, quick and doesn’t hurt (you will also get a verification email, which you should check and click the link included).

Since it’s new I need a lot of people to test the mail queuing and other stuff.

If you would like to be a beta tester (:D), test it and create your own campaigns I can give you access for free.

It is something like aweber but since I don’t have access to it I’m developing it on my own, differently.

Features in v0.3

- add and edit campaigns
- subscribe, verify and unsubscribe
- an automatic email is sent on subscription and verification
- possibility to send an email to every subscriber in the campaign
- you can add links so you can track clicks on them
- email scheduling
- email importing (deletes duplicates and incorrect addresses)
- possibility of embedding the subscription form anywhere
- possibility of redirecting the user to your own thank you page on subscription
- possibility of sending attachments
- html support
- you can attach a file to send when the user verifies his email
- teoretically the mail queue should work if you try to send an email to more then 50 users, but it is untested

Wanna test? Drop a comment!

]]>
http://blog.zurka.us/help-me-test-my-mailing-list-system/feed/ 1
Make Your Titles Dynamic with PHP http://blog.zurka.us/make-your-titles-dynamic-with-php/ http://blog.zurka.us/make-your-titles-dynamic-with-php/#comments Tue, 26 May 2009 15:25:00 +0000 nasal http://blog.zurka.us/?p=938

When developing your website, if you want to rank well in search engines it is crucial that you use different titles for every sub-page you’ve got online. This is because these search engines (Google, MSN etc) love your titles and categorize your site based on them. This is good for SEO.

This is a simple thing to accomplish using PHP but it really is very effective. I will put dumb text into them but you should use titles rich with keywords related to your niche.

$title = 'Welcome to our website!';
if (key($_REQUEST) == 'register') { $title = 'Register now'; }
if (key($_REQUEST) == '404') { $title = 'Error 404: The page is unavailable'; }
if (key($_REQUEST) == 'about') { $title = 'Who we are, what we do'; }
if (key($_REQUEST) == 'news') { $title = 'Recent news'; }

This would work if you are using urls like yoursite.com/?register, ?news etc. What it does is set a variable $title which will include the text your define according to the page we are watching. Now we need to display it in our browser.

<head>
    <title><?= $title; ?> - Your Company Name</title>
</head>

Pretty easy, isn’t it?

Oh and today I’ve found a FREE product that helps you make some money with google, all you have to pay is less then $3 for shipping and handling and they mail it to you. The offer is only valid for the US though. Click here if you are interested. It can do no harm if you’re serious about making some money online and $3 really isn’t something you could not afford!

Have swing!

]]>
http://blog.zurka.us/make-your-titles-dynamic-with-php/feed/ 2
Use PHP to Fetch an Album Artwork From Amazon http://blog.zurka.us/use-php-fetch-artwork-cd-from-amazon/ http://blog.zurka.us/use-php-fetch-artwork-cd-from-amazon/#comments Sun, 17 May 2009 22:59:16 +0000 nasal http://blog.zurka.us/?p=930

It’s actually pretty easy once you find out how to do it. By having the artist and album name you can fetch the album artwork from Amazon in three different sizes.

With php5 it’s very easy since it has an integrated xml parser. All we have to do is this:

$url = simplexml_load_file('http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=12DR2PGAQT303YTEWP02&Operation=ItemSearch&SearchIndex=Music&Artist=ARTIST&ResponseGroup=Images&Keywords=ALBUMNAME');

Please note that in the url I’ve put ARTIST and ALBUMNAME, which you should replace with what you’re searching for. Doing so, we get an xml file from which we can display the image like this:

echo '<img src="' . $url->Items->Item[0]->LargeImage->URL . '" />';

If we want the large one, that is. If we would like the medium one we would use MediumImage and SmallImage for the small one.

That’s it, it’s very useful if you are organizing your mp3 files or maybe building an online community or something.

Hope it helps somebody! If you find it useful, digg it!

ps: I forgot to mention that if you are using php4 this wont work, you will have to use another xml parser to fetch the image location from the xml provided by Amazon.

Have swing!

]]>
http://blog.zurka.us/use-php-fetch-artwork-cd-from-amazon/feed/ 5
Check if A Number is Even or Odd with PHP http://blog.zurka.us/check-number-even-odd-php/ http://blog.zurka.us/check-number-even-odd-php/#comments Thu, 14 May 2009 15:04:29 +0000 nasal http://blog.zurka.us/?p=921

I’m going to show you a very simple method for finding out if a number is even or odd in php. You could use this for many things but usually this is mostly used when you have a, let’s say, table and you want to color the background of every second row differently.

The php code looks like this:

if ($number % 2) {
    echo 'The number is even';
} else {
    echo 'The number is odd';
}

In real life we would use something like this:

$i = 0;
while ($something = mysql_fetch_assoc($query)) {
    echo '<div style="padding: 5px; background: #' . ($i % 2 ? 'eee' : 'ddd') . ';">Content</div>';
    $i++;
}

And of course, likewise you could check if a number is divisible by another number using this:

if ($number % 3 == 0) {
    echo 'The number is divisible by 3';
} else {
    echo 'The number is not divisible by 3';
}

Easy peasy, as always!
Have swing!

]]>
http://blog.zurka.us/check-number-even-odd-php/feed/ 0
Simple Way of Implementing BBCode with PHP http://blog.zurka.us/simple-way-of-implementing-bbcode-with-php/ http://blog.zurka.us/simple-way-of-implementing-bbcode-with-php/#comments Fri, 08 May 2009 22:46:06 +0000 nasal http://blog.zurka.us/?p=916

When posting on forums, we usually use BBcode for styling our texts. When developing webpages, if we want to let our users style their profiles but don’t want to give them permission to style directly with CSS or html (<strong> etc..), we could simply use this piece of code to let them use those BBcodes.

Please note that we could do this using REGEX but as I used the word ‘simple’ in the title of the post, I’m going to make it really simple :)

The first thing we do is create an array with the tags we are going to use. In my case, my array is $bbcode and I’m going to use codes in square brackets, just as forum platforms do. Take a look at the code, you will see it really is simple.

$bbcode = array(
    "[big]" => "<big>",
    "[/big]" => "</big>",
    "[b]" => "<strong>",
    "[/b]" => "</strong>",
    "[i]" => "<em>",
    "[/i]" => "</em>",
    "[u]" => "<u>",
    "[/u]" => "</u>",
    "[img]" => "<img src=\"",
    "[/img]" => "\" border=\"0\" />",
    "[sup]" => "<sup>",
    "[/sup]" => "</sup>",
    "[sub]" => "<sub>",
    "[/sub]" => "</sub>",
    "[quote]" => "<span class=\"quote\">",
    "[/quote]" => "</span>",
);

As you can see, for the quotes I use a SPAN element with class=”quote” but it could be a DIV as well, since I’m styling it as a block element in my CSS file.

Next, to change our text into the styled one, all we have to do is use the strtr function and use our array as the – guess what – array with replace pairs.

echo strtr('Hello. I am [b]bolded[/b], I am [u]underlined[/u] and I am [big]big[/big]!', $bbcode);

That’s it! As Duke Nukem would say.. piece of cake.

Have swing!

ps. I know it doesn’t include the useful codes as [url] but for that we should already use regex.

]]>
http://blog.zurka.us/simple-way-of-implementing-bbcode-with-php/feed/ 1
2 Methods for Finding File Extensions in PHP http://blog.zurka.us/2-methods-for-finding-file-extensions-in-php/ http://blog.zurka.us/2-methods-for-finding-file-extensions-in-php/#comments Wed, 25 Feb 2009 17:25:31 +0000 nasal http://blog.zurka.us/?p=859

Two very simple methods for getting to know which is the file extension of the file, using two very simple PHP functions.

1. Method: function end

The end function finds the last member of an array. So if we want to get the file extension, we will first explode our filename at the dots (.) and then use this function to get the last array object, which will obviouly be the extension.

Example:

<?php
$file = 'something.jpg';
$explode = explode(".", $file);
echo end($explode); // will return jpg
?>

Or shortened:

<?php
$file = 'something.jpg';
echo end(explode(".", $file)); // will return jpg
?>

2. Method: function strrchr

This function finds the last occurrence of a character in a string. Using this function, our php code would look like this:

<?php
$file = 'something.jpg';
echo substr(strrchr($file,"."),1);
?>

Easy!

]]>
http://blog.zurka.us/2-methods-for-finding-file-extensions-in-php/feed/ 3
Use PHP To Display Page Content (LMAWT part 2) http://blog.zurka.us/use-php-to-display-page-content-lmawt-part-2/ http://blog.zurka.us/use-php-to-display-page-content-lmawt-part-2/#comments Fri, 23 Jan 2009 15:27:16 +0000 nasal http://blog.zurka.us/?p=826

If you missed the previous posts, you can read the Introduction and First part to catch with us.

What we are going to do today is make our links work and display the content depending on what link we click.

First let’s make our links go live. In our final result of part 1, our links were dead. To make a link work, we must use this syntax:

<a href="./">Home</a>
<a href="./?news">News</a>
<a href="./?contact">Contact</a>
*** notice we use ?news for the news, ?contact for the contact page etc, will be explained later ***

So let’s change all of our links to this. Our final result will be this. As you can see, the links are underlined and you can click on them. But if you click on them you will notice nothing really changes on the page, except for the URL in the browser.. that’s because we need our PHP code that will “read” the URL and display the content that comes along.

Let’s see the PHP code that we put into our #main-content div. To load different contents depending on what click we link on, we are going to use the switch function, which is not hard to understand, is very useful and works fast!

<?php
switch (key($_REQUEST)) {

	case 'news':
		echo 'We will display our news here';
	break;

	case 'members':
		echo 'This will be the member list';
	break;

	// we continue like this for all our links
	// and finish with the default statement

	default:
	echo 'Welcome to my first website';

}
?>

As I said before, put this code between inside your #main-content DIV.

<div id=”main-content”>
<?php switch () {} ?>
</div>

What will this piece of code do?
It will display different contents based on the page we are on. If, for example, we will click on our “news” link, the URL will change to “domain.com/?news” and the content that will be shown will be “news.php“, because of the ‘case’ we wrote. In this case, what comes after the question mark in the URL is called a “query string” and we will work with these.

In this example we are using the “key($_REQUEST)” but we could change this to react to something like “domain.com/?page=news” and in this case we would use a different switch. Tell me if you want to know about it and I’ll write an example for this.

Anyway, check what we accomplished now. We made our first working website, we could actually put whatever we would like inside, we are done. The content is changing and everything is working fine.

Any questions? Feel free to ask!

]]>
http://blog.zurka.us/use-php-to-display-page-content-lmawt-part-2/feed/ 4