Welcome to Dream.In.Code
Become a PHP Expert!

Join 136,910 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,735 people online right now. Registration is fast and FREE... Join Now!




PHP variable in url

 
Reply to this topicStart new topic

PHP variable in url

Jon1001
24 Mar, 2008 - 09:25 AM
Post #1

New D.I.C Head
*

Joined: 31 Jan, 2008
Posts: 17



Thanked: 1 times
My Contributions
I am working on a page which uses a number of variables in the url, i.e. domain/file?variable=integer&variable2=integer. The problem is that I need to change one of the variables without affecting the others. Normally, I'd use PHP_SELF but since the variable already exists, it would produce something like this... domain/file?variable1=integer&variable2=integer&variable1=integer. Is there a way around this?

This post has been edited by Jon1001: 24 Mar, 2008 - 09:26 AM
User is offlineProfile CardPM
+Quote Post

spearfish
RE: PHP Variable In Url
24 Mar, 2008 - 10:33 AM
Post #2

Monkey in Training
Group Icon

Joined: 10 Mar, 2008
Posts: 746



Thanked: 2 times
Dream Kudos: 225
My Contributions
Umm.... what? Can you clarify what you need?

The information stored in the URL is "get" data....
User is offlineProfile CardPM
+Quote Post

Jon1001
RE: PHP Variable In Url
25 Mar, 2008 - 08:28 AM
Post #3

New D.I.C Head
*

Joined: 31 Jan, 2008
Posts: 17



Thanked: 1 times
My Contributions
Sorry, I really should have explained it better. My actual problem is that I have made a mysql query which gets lots of data from the database and puts it into a table. I have written a function that splits the query up so that it is viewed over a number of pages i.e. Prev/Next buttons by using a GET variable in the url called page. When the prev/next links are used, I need it to reload the page but increment/decrement the $_GET['page'] variable without changing any of the other GET variables. I am currently using a load of if statements which, retrieve the possible GET variables from the url and, if they exist, add them to the new url. Just wondering if there is a simpler way of doing this. Hope this makes a bit more sense.
User is offlineProfile CardPM
+Quote Post

spearfish
RE: PHP Variable In Url
25 Mar, 2008 - 09:16 AM
Post #4

Monkey in Training
Group Icon

Joined: 10 Mar, 2008
Posts: 746



Thanked: 2 times
Dream Kudos: 225
My Contributions
So, let me make sure I understand your problem.

Right now you have several unique pages generated based off of what the query string is. Meaning you have a URL like:
http://www.example.com/index.php?page=4&am...=72&key=moo

Then you want to change just one part of the query string? Wouldn't just linking to the URL in question do the trick? Meaning, rather than doing anything fancy to change the page number; simply link using an HTML anchor to
http://www.example.com/index.php?page=5&am...=72&key=moo
User is offlineProfile CardPM
+Quote Post

Jon1001
RE: PHP Variable In Url
25 Mar, 2008 - 11:50 AM
Post #5

New D.I.C Head
*

Joined: 31 Jan, 2008
Posts: 17



Thanked: 1 times
My Contributions
Yes that is what I am trying to do and I am using an html anchor but the GET variables (other than 'page') aren't always the same so the url must be generated by the php depending on what the variables are. Using your example, here is what I have done at the moment:

CODE
$url = 'http://www.example.com/index.php?';

if ($_GET['uid']) {
    $url .= 'uid='.$_GET['uid'].'&';
}

if ($_GET['key']) {
    $url .= 'key='.$_GET['key'].'&';
}

$url .= 'page ='.($_GET['page'] + 1);


This works fine but if there are lots of GET variables, it can get quite long. I just wanted to know if there was another way of doing it similar to $PHP_SELF but with the ability to change a variable.
User is offlineProfile CardPM
+Quote Post

spullen
RE: PHP Variable In Url
25 Mar, 2008 - 12:49 PM
Post #6

D.I.C Regular
Group Icon

Joined: 22 Mar, 2007
Posts: 330



Thanked: 1 times
Dream Kudos: 50
My Contributions
You can get the page number and just do a query on that page using the mysql limit function.
So
CODE

$num_of_records = 30; //the number of things you want to show on a page
if($_GET['page']){
    $page = $_GET['page'];
}
else{
    $page = 0;
}
$start = $page * num_of_records;
$end   = $start + num_of_records;
if($start > 0){
    $start = $start + 1; //this is for the offset on pages not starting with 0
}
$sql = 'SELECT * FROM SomeTable WHERE name = ' . $query . ' LIMIT ' . $start . ', ' . $end;
//do query and stuff...


I haven't tested that code, but it should work/should be what you're trying to do.
Also you should read up on pagination (that's what this is), which might give you a better way of solving this problem.

Edit:
Also the links to next and previous should be generated by using the current page number. So generate the link with ?page=<? $page-1 ? for prev and ?page=<? $page + 1 ?> for next

This post has been edited by spullen: 25 Mar, 2008 - 12:51 PM
User is offlineProfile CardPM
+Quote Post

Jon1001
RE: PHP Variable In Url
28 Mar, 2008 - 10:08 AM
Post #7

New D.I.C Head
*

Joined: 31 Jan, 2008
Posts: 17



Thanked: 1 times
My Contributions
Thanks for the replies. I had already written the mysql LIMIT stuff but I asked a friend about the other problem and he recommended a way similar to what I have done so I think I will leave it as it is. Thanks anyway for your help. Jon.

This post has been edited by Jon1001: 28 Mar, 2008 - 10:09 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 09:57PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month