Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Sunday, December 27, 2009

PHP: Send information through URL

This lesson deals with a very important part of PHP and that is using the URL to send information between pages. I will be using very basic examples to show how things work. But it is up to you how well you can use these things for advance use.

To start with, there are different ways to pass information between pages. One very crude method will be to send the information through URL. For example you may have seen a lot of URL’s like this: http://www.domainname.com/index.php?id=1&name=xyz

This is what we call sending information through the URL. In the above example, two separate values are being passed, i.e. the “id” with a value of 1 and also the “name” with a value xyz.

We can fetch these values in another page and then use them for anything. So, to do that, let’s look at some code.

The plan is to make a link on one page which will pass the information to the second page. We will not be using a very complicated HTML code, so anyone can understand this.

So, the name of my first page is "tutorialPage01.php" and here is the code for the same:

<?php
echo '<a href="tutorialPage02.php?id=1&username=John">Click here to pass the information to the next page</a>';
?>

This is where I am sending the information with different variables. Now, we will set up the next page which is “tutorialPage02.php”. This is the page which we have linked to in the link tag. This page will take the information from the URL and will display the same on the page.

<?php
echo $_GET ['id'];
echo "<br />";
echo $_GET ['username'];
?>

Here we are doing very simple things. As the variable's values are already in the URL which is passed, we are directly displaying the values of the same on the page. First is the printing of the values stored in ‘id’, then we used a break operator to go to the next line and then we are printing the value stored in 'username'.

Tuesday, October 13, 2009

Reseting the auto increment value n an mySql database field

I came across this problem that I wanted to reset my auto increment number for the id field which was my primary key. I deleted a few records and so it was now difficult to get the number to start from 1 again and get a nice sequence. One obvious thing to do was manually edit it as my content was not huge. But still I found a correct and automated way to do that:

I made it possible by using PHP's while loop. [NOTE: I will just give the main structure.]

Select ID from
order by ID

then use a while/for loop to iterate through the results, and update each row with a new number for the ID.. you could use a PHP variable to track the current ID number, and increment it with each loop...

Update
set ID=???