jamjam Posted July 19, 2010 Share Posted July 19, 2010 Hi I am PHP newbie. I am developing a website with PHP and MYsql. Say this website has two pages, both use query strings to retrieve data from MYsql database. Page 1: http://www.mydomain.com/?value=500 Page 2: http://www.mydomain.com/something/?value=500 I want to echo a link to page 1 so that once it is click it will take the user to page 2, carrying the value of the current query string, something like this. echo href=http://www.mydomain.com/something/$currentquerystring Is this possible? I will appreciate your help. Thanks Quote Link to comment Share on other sites More sharing options...
lemmin Posted July 19, 2010 Share Posted July 19, 2010 echo 'href="http://www.mydomain.com/something/?"'.$_SERVER['QUERY_STRING']; Or if it is always going to be 'value', you can just do it like this: echo 'href="http://www.mydomain.com/something/?value="'.$_GET['value']; Quote Link to comment Share on other sites More sharing options...
otuatail Posted July 19, 2010 Share Posted July 19, 2010 I think something like this should work $val = "Location: /something/?value=" . $_POST['value']; header($val); Desmond. Quote Link to comment Share on other sites More sharing options...
jd307 Posted July 19, 2010 Share Posted July 19, 2010 Welcome jamjam! You will find this concept used throughout PHP consistently, so this idea will be something you will use in many of your scripts where you insert the value of some data into something that gets echo'd out. Lemmin has already given you two methods which are quite standard for this kind of thing, just to add to this and hopefully help your understanding for later uses I will include the following: $currentquerystring = $_GET['value']; // This could also be $_POST depending on the method you are using to obtain your input echo 'My current query string is: ' . $currentquerystring; This will obtain your value (assume your value = 'Hello') and assign it to $currentquerystring, it will then "echo" out the words "My current query string is: Hello". This works for HTML as well, so with Lemmin's example, he has written the HTML anchor code to create an HTML and then echos this out as part of the URL, which of course, if 'Hello' changed to 'Goodbye' it would append whatever the data was to the URL in the final output. Quote Link to comment Share on other sites More sharing options...
jamjam Posted July 19, 2010 Author Share Posted July 19, 2010 Hi Thanks for the quick reply Ok I have the echo this to page 1 'href="http://www.mydomain.com/something/?"'.$_SERVER['QUERY_STRING']; Once I click it I get echo this in the address bar http://www.mydomain.com/something/? For the second and third solutions I get this in the address http://www.mydomain.com/something/?date= Basically none of them carry the value of the query string from page 1 to page 2. Have I done something wrong? Also otuatail can you please spell out your code so I can copy and paste. This all very new to me. Thanks Quote Link to comment Share on other sites More sharing options...
jamjam Posted July 19, 2010 Author Share Posted July 19, 2010 Thanks for the explanation jd307 I properly should learn PHP in more detail, but right now I am just after a quick fix to this problem. What would the echo statement look like? Please! On my site the name of the value will always be 'date'. But the value will change everyday for example today it will be 19-07-2010 http://www.mydomain.com/?date=19-07-2010 Thanks Quote Link to comment Share on other sites More sharing options...
jd307 Posted July 19, 2010 Share Posted July 19, 2010 If you could submit your code that you have it would be much easier for us to help you, use the CODE tags on the forum to post it so it is easy to read. Is your code retrieving the data from the URL? The example I gave was literally that, an example and wouldn't really be very useful for your code. As your parameter is "date" you will need to extract it like so: $date = $_GET['date']; This would put your value into the variable $date. Now if you were to do the echo statement as posted before, but substitute $currentquerystring with $date: echo 'http://www.mydomain.com/?date=' . $date; This will now echo out "http://www.mydomain.com/?date=19-07-2010". If your only parameter is the date... and it is always updated to display today's date... and it is going to appear on every page, why not look into the date() function in PHP. That way you can retrieve the date internally on your page instead of having it displayed on the end of your URL. Depending on the reason why you are storing this date for every page a user could quite simply change the date within the URL... which may cause problems. But of course that does depend on why you are using the date on each page. Quote Link to comment Share on other sites More sharing options...
jamjam Posted July 19, 2010 Author Share Posted July 19, 2010 Hi The website I am developing will display TV Listings data. The page I am working on has 7 links to represent days of the week, like this Mon 19th Tue 20th Wed 21st Thu 22nd Fri 23rd Sat 24th Sun 25th Once a linked is clicked this returns schedule for that day. All this is working. My problem is that I wish to separate the data into 2 parts. Prime Time Programming and Day Time Programming. The Prime Time is default so it is automatically displayed when the page is loaded. For the user to see Day Time Programming they must click another link called DAY. This is the link I wish to encode the "current query string" into. So when the user clicks this they will be taking to a second page which uses the value in the current query string. This my code. Please forgive, I don't how to use "tags" </a><br /><br /><table style="text-align: left; width: 553px; height: 11px;" border="0" cellpadding="2" cellspacing="2"><tbody><tr> <?php // set default to 'today' if (!isset($_GET['date'])) $_GET['date'] = date('d-m-Y', time() - 0); // can be a HTML colour code or a literal name, ie: green, red, blue $highlightedColour = '#F54997'; // show today $today = date("d-m-Y", strtotime('today')); echo '<td style="vertical-align: middle;"><big><span style="font-weight: bold;"><a href="?date='.$today.'"'.((isset($_GET['date']) && $_GET['date'] == $today) ? ' style="color:'.$highlightedColour.'"' : '').'>Today </a> </span></big></td>'; // show all the other days for ($time = strtotime('+1 days'), $i=0; $i < 6; $time = strtotime('+1 days', $time), $i++) { $date = date("d-m-Y", $time); echo '<td valign="middle"><big><span style="font-weight: bold;"><a href="?date='.$date.'"'. ((isset($_GET['date']) && $_GET['date'] == $date) ? ' style="color:'.$highlightedColour.'"' : '') .'>' . date("D jS", $time) . "</a></span></big></td>\n"; } ?> <td style="vertical-align: middle; text-align: center;"><big><span style="font-weight: bold;"><span style="color: rgb(245, 73, 151);">EVE </span></big></td><td style="vertical-align: middle; text-align: center; color: rgb(102, 102, 102);"><big><span style="font-weight: bold;">|</span></big></td><td style="vertical-align: middle; text-align: center;"><big><span style="font-weight: bold;"> DAY</span></big></td><td style="vertical-align: middle; text-align: center;"></td></tr></tbody></table> Thanks Quote Link to comment Share on other sites More sharing options...
Jessica Posted July 19, 2010 Share Posted July 19, 2010 To use the tags highlight your code and click the button that has php written on it. You need to make a link for DAY for one thing. <a href="dayPage.php?date=<?php echo $date; ?>">DAY</a> Quote Link to comment Share on other sites More sharing options...
jamjam Posted July 19, 2010 Author Share Posted July 19, 2010 Thanks for the tips jesirose. I think am making progress. Here is the code in tags </a><br /><br /><table style="text-align: left; width: 553px; height: 11px;" border="0" cellpadding="2" cellspacing="2"><tbody><tr> <?php // set default to 'today' if (!isset($_GET['date'])) $_GET['date'] = date('d-m-Y', time() - 0); // can be a HTML colour code or a literal name, ie: green, red, blue $highlightedColour = '#F54997'; // show today $today = date("d-m-Y", strtotime('today')); echo '<td style="vertical-align: middle;"><big><span style="font-weight: bold;"><a href="?date='.$today.'"'.((isset($_GET['date']) && $_GET['date'] == $today) ? ' style="color:'.$highlightedColour.'"' : '').'>Today </a> </span></big></td>'; // show all the other days for ($time = strtotime('+1 days'), $i=0; $i < 6; $time = strtotime('+1 days', $time), $i++) { $date = date("d-m-Y", $time); echo '<td valign="middle"><big><span style="font-weight: bold;"><a href="?date='.$date.'"'. ((isset($_GET['date']) && $_GET['date'] == $date) ? ' style="color:'.$highlightedColour.'"' : '') .'>' . date("D jS", $time) . "</a></span></big></td>\n"; } ?> <td style="vertical-align: middle; text-align: center;"><big><span style="font-weight: bold;"><span style="color: rgb(245, 73, 151);">EVE </span></big></td><td style="vertical-align: middle; text-align: center; color: rgb(102, 102, 102);"><big><span style="font-weight: bold;">|</span></big></td><td style="vertical-align: middle; text-align: center;"><big><span style="font-weight: bold;"> DAY</span></big></td><td style="vertical-align: middle; text-align: center;"></td></tr></tbody></table> So I have changed the link to the one you suggested. It successful passed the a query string to the 2nd page as I wanted and everything works. But it only passes the date 25-07-2010. I am guessing this is because this the last date on the list. If this can be modified to pass whatever the currently selected date is then I will have found my solution. Thanks Quote Link to comment Share on other sites More sharing options...
jamjam Posted July 19, 2010 Author Share Posted July 19, 2010 YES. I worked it out. I think am genius now. <a href="/day/?date=<?php $date = $_GET['date']; echo $date; ?>">DAY</a> Thanks everyone for all your help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.