开发者

passing php variables in query strings

I have a number of url's with different query strin开发者_C百科gs such as

 view.php?id=5
 view.php?id=6
 view.php?id=7

on another php page Im using file_get_contents as below:

 $page = file_get_contents('view.php?id=5');
 $file = 'temp/form.html';
 file_put_contents($page, $file);

This of course only writes the first id '5', so how can i retrieve the 'id' variable on this page and write it in my file_get_contents line so I dont have to write out all the id's in seperate lines

thanks rifki


If I understand correctly, in the situation you demonstrate you could use a for loop or something like that. But that only works if the IDs are numeric and follow each other up.

Example:

for($i = 5; $i <=7; $i++) {
  $page = file_get_contents('view.php?id='.$i);
  $file = 'temp/form.html';
  file_put_contents($page, $file);
}

Updated: If your ID comes from database you could select all IDs and loop through that. Eg.

$sql = 'SELECT id FROM tablename;';
$res = mysql_query($sql);
while($row = mysql_fetch_assoc($res)) {
  $page = file_get_contents('view.php?id='.$row['id']);
  $file = 'temp/form.html';
  file_put_contents($page, $file);
}


If those urls are used to browse some pages you can use the $_GET array (Official PHP Manual for the $_GET method).
It simply gets the value of a variable passed via the get method (i.e. page.php?var1=1&var2=2) so, if you need to get the id value for your page the code should be something like this:

$id = $_GET['id'];
$request = 'view.php?id='.$id;
$page = file_get_contents($request);
$file = 'temp/form.html';
file_put_contents($page, $file);

The first line gets the id passed via url, then the second creates the request string to pass to your file_get_contents function, then the other are like your code.
This is the case if you request the data from inside of such pages, if, for example, you know all of the pages needed then you can use a for clause to solve this problem.
One of the solutions might be:

$first_page = 5;
$last_page = 7;    
for ($i = $first_page; $i <= $last_page; $i++) {
    $request = 'view.php?id='.$i;
    $page = file_get_contents($request);
    $file = 'temp/form.html';
    file_put_contents($page, $file);
}

With this you simply set the first and the last page you want to request, then you use these values to cycle through the pages and then call your function to do your... "stuff" :D
This is a good approach because then you can set in runtime the values for the for statement so you won't have to change that file every time.
However I think that using an identification different from Integers for your pages would be better, like id=home, or something like that.


If you get the id in your query string I mean url, you should write something like this:

$page = file_get_contents('view.php?id='.$_GET['id']);
$file = 'temp/form_'.$_GET['id'].'.html';
file_put_contents($page, $file);


To retrive the variable from a query string use

$_GET['variable_name']

By default, whenever you make a request to the server it is GET method which is called unless you explicitly specify that the form method to be POST.

//variable_name is the name of the variable in the query string

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜