Archives for "Tutorials"
Get Unlimited Parent and Child in PHP
Well, let me ask you first. Have you already known about recursion? If not, recursion mean reuse function in this function.
How is it ? For eg Like
function one_function ($var){
one_function(332);
}
OK , So , how to related with this post title ? I would like to say absolutely related with recursion. Let’s say, we would like to create category with parent and child.so, we have one table in database, category table. Table Structure Like:
| id | name | p_id |
|---|---|---|
| 1 | Main Parent | 0 |
| 2 | Sub Parent | 1 |
| 3 | Child | 2 |
| 4 | Parent 2 | 0 |
| 5 | Child 2 | 4 |
| 6 | Child 3 | 4 |
| 7 | Child 4 | 5 |
MySQL next and previous record of the current record
I have thought a lot these days what I should write some tips for beginner. Now, I got it. Have you ever noticed when you visiting and reading on some blogs? You will see previous and next links of current post. I don’t mean pagination links. If you have seen , have you ever thought how to retrieve those data from MySQL ? OK , If you haven’t thought and tried yet. Let’s see how to do it.
Let says, we have user table. Normally, we write SQL query like :
SELECT * FROM user WHERE user_id = 111
OK, If your user_id is added by auto increment, it’s simply write 111 – 1 and 111 + 1 in your program. If not, how could we got it?
Let’s see.
SELECT *, (SELECT user_id FROM user WHERE user_id < u.user_id ORDER BY user_id DESC LIMIT 1) AS user_prev, (SELECT user_id FROM user WHERE user_id > u.user_id ORDER BY user_id ASC LIMIT 1) AS user_next FROM user AS u WHERE u.user_id=111
Enjoy !!! I hope it would be useful for you.
How to return more than one value or multi value in PHP
Some of PHP beginners (my friends) always ask me how to return two or more values from function. So , I thought, I may need to write the beginner tip at The Tech Space Blog. If your the advanced PHP programmer, you will say. So easy, It’s a piece of cake. For beginner, it would be a little difficult. So, let’s see how to write it.
For normal function ..
<?
function calc ( $a, $b)
{
$sum = $a+$b;
return $sum;
}
echo $calc(2,3);
?>
So, If you would like to return two or more value from that function, just only return as an array. Let’s see.
<?php
function calc($a,$b){
$sum = $a+$b;
$multiply = $a * $b;
$divided = $a/$b;
$modulus = $a%$b;
return array( 'sum'=>$sum, 'multiply'=>$multiply, 'divided'=> $divided, 'modulus'=>$modulus );
}
$data = calc(3,4);
echo 'Sum value: '.$data['sum'].'<br />';
echo 'Multiply value: '.$data['multiply'].'<br />';
echo 'Divided value: '.$data['divided'].'<br />';
echo 'Modulus value: '.$data['modulus'].'<br />';
?>
Enjoy !!!
Output XML using PHP from MySQL database
Firstly, Let me talk about this post. If your advance level php programmer, this is a piece of cake for you. I would like to say this post for beginner level. It would be a little difficult for a beginner. You may need xml output when you want to integrate with flash and MySQL records such as photo gallery, playlist for flash mp3 player, RSS Feeds, and so on.
PHP Pagination
I have look for php pagination source. I got one very nice source code from Stranger Studio. That very nice code if your have a lot of records. You can see at sample preview.
![]()
However, this is a lot of lines in the file. If you want to put into a lot of pages, all of code you need to put into those pages. We have to take care about this. That’s while I have changed to class and functions. That’s very easy to use. I will share it back.
Made by Symmetric Web
Distributed by Smashing Magazine