PHP’s Now I know #2
Sometime ago, for mysql queries such as: “SELECT * FROM data where id IN (123,456,789)” I had to iterate through the ids, to get 123,456,789. So, my code usually looked like this:
$ids = array(123,456,789);
$idstr = ”;
if(is_array($ids)){
foreach($ids as $id){
$idstr .= $id.’,';
}
}
$idstr = rtrim($idstr,’,');
$sql = “SELECT * FROM data where id IN ($idstr)”;
But instead of doing that, what you can do here is glue the pieces of your array together using the implode function. Like this:
$ids = array(123,456,789);
if(is_array($ids)){
if($idstr = implode(’,',$ids)){
$sql = “SELECT * FROM data where id IN ($idstr)”;
}
}
There you have it folks.. another workaround to an easier life with shorter codes.
If you know of a workaround you might want me to add.. you can always buzz me up.
Happy coding!
It’s a date then..
One of my most loved PHP built-in functions is the date function. Yesterday, as I was working on a birth date string, I had the usual ‘0000-00-00′ format. When I was really new to PHP, what I did was, to change that into something readable as January 31, 2008, I’d explode or split the string, and then switch the array into their other forms.
So instead of splitting the string, I found out that we can do this instead:
strftime(”%B %d, %Y”, strtotime($birthday))
Hope this helps!
PHP Arrays
My very first encounter with arrays were very traumatic, I was certain I’ve just met my first programming nightmare. You see, I was asked to report on arrays, and I must say, I didn’t understand a word I was saying. But that was ancient history. Now, I’m such an array fanatic, I use arrays whenever possible.
In programming, in one way or another, you will need data containers. It is most appropriately known as variables. (Because their values vary a lot.)
Examples: $var, $name, $age.
Arrays are actually just like variables, only a little better because you have more than one value.
For example, if you have:
$title = ‘valeriejoy’;
$href = ‘http://blog.3rdbee.com’;
Alternatively, you can use arrays to hold both values as:
$blog = array(’title’=>’valeriejoy’, ‘href’=>’http://blog.3rdbee.com’);
So why is this so important? Personally, I use arrays when I need a more condensed code (like loops and stuff).
For example, instead of saying:
echo ‘January, February, March, April, May, June, July, August, September, October, November December’;
You can do:
$months = array(’January’, ‘February’, ‘March’, ‘April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘September’, ‘October’, ‘November’, ‘December’);
foreach($months as $month){
echo $month;
}
Foreach is a special loop for arrays. It takes arrays, and arrays ONLY.
Last year, I helped create a facebook app that has attracted a lot of people. I am really, really proud to be part of the team, but, there’s a very big BUT.. there were a lot of foreach errors, that were.. honestly, my fault.
You see, foreach can get really really strict and will issue errors if it finds out that you’re looping in a ‘non-array’ variable. From then on, I use foreach with extra precaution.. that is, I use a workaround.
if(is_array($array)){
foreach($array as $arr){
//do something
}
}
That is it, before you ‘foreach’ an array, check if it’s really an array. (I mean, especially if your supposed-to-be array was just derived from something or your database, there’s still this small chance that it’s returning something empty or not an array).
Hope this helps!
Happy Coding!
PHP’s Now I know #1
I was already in my 3rd year in College when I first heard of PHP. Notice the word “heard” (hehe
). We were actually taught PHP (as it was part of the curriculum). I guess I wasn’t just listening that much.
Unfortunately, we had a big project whose credit was as big as the Final Exam. I think what made it really “big” was that I had to use sessions. Sessions are PHP’s simple way of knowing if you’re logged in or not. (You can also use cookies if you want.)
Anyway, as I was saying, my first encounter on sessions was terrible. I had to face a lot of errors, most were “headers already sent” ones. Our instructor suggested we use ob_start. And just like yesterday’s post, the “workaround” really didn’t get me. I just used it and didn’t bother learning more about it.
Yesterday, I was working on a PHP script that had to use sessions. Sure enough, the years-ago error showed up once again :(. Then I remembered ob_start (start output buffering). You see, I think we get “headers already sent” errors, because, before the sessions, output had already been sent to the browser.. thus when we used sessions, it sends another set of headers, which triggers these errors. What you could do here is (1) check for something else before the opening <?php and closing ?> tag. (2)If this fails, you can use ob_start to start output buffering.
According to php.net (referring to ob_start):
This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.
The contents of this internal buffer may be copied into a string variable using ob_get_contents(). To output what is stored in the internal buffer, use ob_end_flush(). Alternatively, ob_end_clean() will silently discard the buffer contents.
..and then release the contents of the internal buffer using ob_end_flush.
On a side note, ob_start and ob_get_contents would be very useful if you need to return output from a function.
Oh?!
I think I got so occupied in my own world lately that I was missing on a few stuff. First, there was this ZTE Broadband Deal in the country. I think I have heard it before, I was just not that interested at first. So, when they were talking about it at work this morning, I was clueless. Well, it appears that somebody is trying to cover up something stinky. If it’s stinky, it will still show or smell for that matter.
As for this post’s workaround:
If you’re working with PHP and Facebook, and runs api calls more than once, you might wanna get the result of this calls. I think setting $GLOBALS[’facebook_config’][’debug’] will give you the values you need. After that, you can use Firebug to check the results, actually, just to disable the display:none; declaration. And ofcourse, unset $GLOBALS[’facebook_config’][’debug’] when you’re done.
As for our web design section, I found this really nice website (10 Tools to help you select a Web 2.0 Color Palette). Which then led me to Colourlovers, if you’re starting on web design and stuck on picking up colors, Colourlovers is a huge huge help!