iBlog random » mysql 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 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
Let's Make A Website Together http://blog.zurka.us/lets-make-a-website-together/ http://blog.zurka.us/lets-make-a-website-together/#comments Tue, 13 Jan 2009 11:55:01 +0000 nasal http://blog.zurka.us/?p=790

I thought about a nice and simple project.. I will write a step by step tutorial on how to create a simple website. Why? Because I’m bored and don’t know what to do in my free time.

I will show you how to design a simple layout with CSS and how to use PHP and a MySQL database to make a simple website with news, users and comments.

I will do this in different posts, so be sure to subscribe to my RSS feed or check back often. Posts will be coming daily I guess.. And we will start with a simple design.

I am going to show you the code and explain it step by step so you will understand it clearly (I hope). I will also provide you with examples of course. All you need is a web server with php and mysql installed on it.. You can set this up on your machine using software like xampp (apache, php, mysql and more in one pack, install and your done) or find a web hosting service that will give you the space. I would suggest you Globat since they offer a very cheap and stable service. You will pay about $8 per month for their hosting. As for the domain, you can buy one on Go Daddy (domains for a low price) or sign up for a free account on afraid.org and get yourself a free subdomain.

Edit: I forgot to tell you how I will structure it.. well, no one read the post so it’s no problem :) )

1. This post is the introduction <-- we are here
2. Designing a simple interface in HTML and CSS
3. Use PHP to display page content
4. Setting up the database and connecting the site with it
5. We will add the register, login and logout features
6. We will add the add news, delete news and edit news features
7. Commenting the news
8. Adding features that you suggest (but if I guess right there won’t be many of them).

Hope you will enjoy this project. I will try to make it interesting :)

PS. Ideas are more than welcome. Feel free to add a comment if you have any!

]]>
http://blog.zurka.us/lets-make-a-website-together/feed/ 3
How to str_replace in mysql http://blog.zurka.us/how-to-str_replace-in-mysql/ http://blog.zurka.us/how-to-str_replace-in-mysql/#comments Mon, 08 Dec 2008 15:51:31 +0000 nasal http://blog.zurka.us/?p=755

I recently bought a database with 170.000 entries, all of which had some stupid link at the end of one of their fields. Let’s say it was a cocktail recipes database and the links in the end were some advertisements like “if you want more recipes visit blahblah”.

I of course didn’t want these links to appear in my posts since this is my database now and I don’t like lame links in it. What to do, to remove each of these links, without the need to manually edit every single line?

It was actually a very easy task after some researching. I found out that mysql has the same function as php’s, str_replace function.

In php, “Hello_world_!” with this function

echo str_replace('_', '-', 'Hello_world_!');

Becomes “Hello-world-!”. In mysql, to get this result, we use it’s replace() function. So to reproduce the effect of that php function we would write something like:

[mysql]
update content set recipe = replace(recipe, ‘_’, ‘-’);
[/mysql]

So if we want to delete those stupid links from our recipe fields, we would have to use:

[mysql]
update content set recipe = replace(recipe, ‘stupid link’, ”);
[/mysql]

And this would change all our records, deleting the “stupid link” where it is present. It is a useful function when we need to change, giving another example, all of our old tags to the new ones (<b> to <strong> or i to em etc..).

]]>
http://blog.zurka.us/how-to-str_replace-in-mysql/feed/ 0
Create an array for a tag cloud in PHP http://blog.zurka.us/create-array-tag-cloud-php/ http://blog.zurka.us/create-array-tag-cloud-php/#comments Fri, 10 Oct 2008 18:22:35 +0000 nasal http://blog.zurka.us/?p=579

Everyone tells us how to create a tag cloud with an array.. but how to actually create the array with the data that we fetch from our database?

This is how I do it. The table we are working in is “pictures” and the field with the tags is “tags“. The tags are separated with a space (for example: sunset beautiful evening).

$query = mysql_query('select * from pictures where tags != "" group by tags');

while ($fetch = mysql_fetch_assoc($query)) {
	$ex = explode(" ", $fetch['tags']);
	$c = count($ex);
	for ($i = 0; $i < $c; $i++) {
		$tags[] .= $ex[$i];
	}
}

What I do is fetch the data from all the fields named “tags” that are not empty. Then I separate the tags with the explode() function and I get an array. I count this array and I add every member of this array to my $tags array, which is the array that will include all the tags and which I’m going to use.

Then the usual code comes into play:

$c = array_count_values($tags); //count the array

$max_size = 32; //max size of the font in pixels
$min_size = 11; //min size of the font

$max_qty = max(array_values($c));
$min_qty = min(array_values($c));

$spread = $max_qty - $min_qty;
if ($spread == 0) {
	$spread = 1;
}

$step = ($max_size - $min_size) / ($spread);

foreach ($c as $key => $value) {
	$size = round($min_size + (($value - $min_qty) * $step)) . 'px';
	$title = $value . ' pictures with the tag ' . $key;
echo '<span style="font-size: ' . $size . '" title="' . $title . '">' . $key . '</span> ';
}

This is it, all you need is a field in the table with the name tags and then you save your tags there, separated with a blank space.

If you have any questions feel free to ask, if you think I don’t know how to write in english please tell me and I will try harder :D If you think this code sucks and have a better one please post it in the comments!

]]>
http://blog.zurka.us/create-array-tag-cloud-php/feed/ 0
How to update multiple mysql rows with PHP http://blog.zurka.us/update-multiple-mysql-rows-php/ http://blog.zurka.us/update-multiple-mysql-rows-php/#comments Tue, 07 Oct 2008 12:45:41 +0000 nasal http://blog.zurka.us/?p=563

Did you ever want to be able to update a dynamic number of rows in your mysql table and didn’t know how? For example: like on myspace when you edit your photo album, you have a gallery and you list one of your albums with x pictures in it. You want to be able to edit all the pics at once and then save all the changes with just one click, without the need to save every picture, one by one. How to do it?

This is how I did it, I don’t think this is the best way, so if you know a better one you are very welcome to put it into the comments.

We need the form, so let’s start with it:

<form method="post">
	<input type="text" name="name[]" value="picture 1" /><br/>
	<input type="hidden" name="id[]" value="1" />
	<input type="text" name="name[]" value="picture 2" /><br/>
	<input type="hidden" name="id[]" value="2" />
	<input type="text" name="name[]" value="picture 3" /><br/>
	<input type="hidden" name="id[]" value="3" />
	<input type="submit" value="Save changes" name="edit" />
</form>

What we did here is make a form with six (6) input fields in it, three text inputs and name them as name[] and three hidden inputs with the id of the picture, so when we submit our form, we get an array as the result. We could make any number of fields we want this way (if we read an album we would make a loop that displays an input for every picture in it). The array will look like this:

Array
(
    [name] => Array
        (
            [0] => picture 1
            [1] => picture 2
            [2] => picture 3
        )

    [id] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [edit] => Save changes
)

Now we have our beautiful array with new picture names and their ids. What we do next is use php to save these values to our database. Let’s say we have our pictures information in the table named pictures.

<?php
for ($i = 0; $i < count($_POST['name']); $i++) {
	mysql_query('update pictures set
	name = "' . mysql_real_escape_string($_POST['name'][$i]) . '"
	where id = "' . $_POST['id'][$i] . '"');
}
?>

What I did here is count the array “name”:

count($_POST['name'])

and do a loop that will repeat itself that many times:

for ($i = 0; $i < count($_POST['name']); $i++) {
}

In the loop, mysql updates the name of the picture with id $i with the name of the field $i. So it goes like:

update pictures set name = "picture1" where id = "1"

This is it, the loop in this case repeats 3 times but it would work the same way for 10 or 15 fields.

I don’t know if what I wrote is understandable, I hope it is and it will help you. If you don’t understand something, you are very welcome to ask it in the comments!

]]>
http://blog.zurka.us/update-multiple-mysql-rows-php/feed/ 1