Oct
28
2008
2

Read and Download a XML File

I was working on a project when i needed to download an XML file and store it localy, to prevent slow scripts, because this XML file had 47 items.

When i would store it my script would work much faster! So i searched, and asked and found this:

<?php
$geturl = 'http://www.site.com/example.xml';
$savelocal = 'file.xml';
file_put_contents( $savelocal, file_get_contents( $geturl ) );
?>

It is this simple and it works totaly fine!

Written by Rene Pot in: PHP Problems | Tags: , , , , , ,
Oct
23
2008
0

Get timestamps out of week no. and year.

Today i had a problem i only had a week number and a year. And i needed timestamps to get all results from a database in that week.

So, i searched on google and found this as first result:
http://tzzz.wordpress.com/2006/08/14/8/

The code he provided wasn’t exactly what i have been looking for but in the comments there was a result that was very close.

function getFirstDayOfWeek($year, $weeknr){
 
   $offset = date('w', mktime(0,0,0,1,1,$year));
   $offset = ($offset < 5) ? 1-$offset : 8-$offset;
   $monday = mktime(0,0,0,1,1+$offset,$year);
   return strtotime('+' . ($weeknr - 1) . ' weeks', $monday);
}

Now i changed this code to provide me the first and last day of the week:

function getFirstDayOfWeek($year, $weeknr){
 
    $offset = date('w', mktime(0,0,0,1,1,$year));
    $offset = ($offset < 5) ? 1-$offset : 8-$offset;
    $monday = mktime(0,0,0,1,1+$offset,$year);
    $sunday = mktime(0,0,0,1,7+$offset,$year);
 
    $day['first']=strtotime('+' . ($weeknr - 1) .  'weeks', $monday);
    $day['last']=strtotime('+' . ($weeknr - 1) . ' weeks', $sunday);
 
return($day);
}

This solved my entire problem, so i could search my database with weeks without having it to store it in the database. That would be a solution that would cost me to much data.

Written by Rene Pot in: PHP Problems | Tags: , , , , , , ,
Oct
14
2008
3

MySQL issue with character set

Today i stumbled upon an issue with inserting text into a database, and the character set wasn’t taking it. No mather what i changed in the database, it was inserted wrong time after time.

Characters like â and é are shown wrong. In the variable it is correct, but in database it isn’t.

This is what i had:

<?php
   $string = "é á â ê ô ç ö";
   $db->query(" UPDATE mytable SET mytxt = '".$string."' WHERE id=1"); // using ezSQL
 
?>

But when i looked into the database it wasn’t correct. Wrong encoding. The tabel was set UTF-8 so that couldn’t be wrong.

This was my solution:

<?php
   $string = "é á â ê ô ç ö";
   $db->query(" SET CHARACTER SET utf8 ");
   $db->query(" UPDATE mytable SET mytxt = '".$string."' WHERE id=1"); // using ezSQL
 
?>

Now it was correct inserted, so i could use it properly.

Written by Rene Pot in: PHP Problems, Sugestions | Tags: , , , , ,
Oct
06
2008
2

Function Exists

Today i gotten into a problem. I am working on a project that supports multiple modules. And every module has it own functions. But sometimes a function is made double, because both modules need this function.
But you never can be sure both modules are implemented in the website. So, you need to include them twice.

But because PHP gives an error when a function is double redeclared, they have brought up a solution. It’s called function_exists.
Let me show you:

<?php
    if (function_exists('functionA')) {
       echo('It does');
    }
?>

This piece of code checks if functionA exists. But, ofcourse you want to use it so you will never include a double function. So you could use it this way:

<?php
   if (!function_exists('functionA')) {
       function functionA() {
           echo('it does not');
       }
   }
?>

Note the ! before function_exists. This turns an “if” into a “if not”. You can put it in front of every if quote.

Written by Rene Pot in: PHP Problems | Tags: , , , ,

Powered by WordPress | Aeros Theme | TheBuckmaker.com WordPress Themes