如何在url php中使用参数?

时间:2014-05-04 09:51:16

标签: php url parameters

我有一些页面

change=1.php
change=2.php
change=3.php

它们都有类似的编码,但留下1或2个变量值。 我知道这是一个非常糟糕的主意!如何使链接工作如下:

change.php?id=1
change.php?id=2
change.php?id=3

http://oi62.tinypic.com/708gfm.jpg

<?php
include 'connection.php';
session_start();
include 'details.php';
/*$pkmn_id = $_SESSION['pkmn_id'];
$poke = $_SESSION['path'];*/


$data = mysql_query(" SELECT * FROM user_pokemon_db WHERE team = 1 AND user_id = '".$id."' "); 
while($rows = mysql_fetch_array($data))
{ 
 $rep_id = $rows[0];
 $pkmn_id = $rows['pkmn_id'];
 $path = mysql_query(" SELECT * FROM pokemons WHERE pk_id = '".$pkmn_id."' ");
 $poke = mysql_result($path, 0, "path");
 echo $poke; 
 echo "<br />";
 $level = $rows['level']; 
 echo $level;
 echo "<br />";
 $exp = $rows['exp']; 
 echo $exp;
 echo "<br />";
 echo "<br />";
 }

$data = mysql_query(" SELECT * FROM user_pokemon_db WHERE user_id = '".$id."' AND team = 0");
while($rows = mysql_fetch_assoc($data))
{ 
 $db_id = $rows['id'];
 $array[] = $db_id;

 $level = $rows['level'];
 $array1[] = $level;

 $exp = $rows['exp'];
 $array2[] = $exp;

 $pkmn_id = $rows['pkmn_id'];

 $data1 = mysql_query(" SELECT * FROM pokemons WHERE pk_id = '".$pkmn_id."' ");
 while($rows = mysql_fetch_assoc($data1))
{ 
 $poke = $rows['path'];
 $array3[] = $poke;
}
}

$team = 1;
$_SESSION['team'] = $team;
$_SESSION['rep_id'] = $rep_id;
?>

我的PHP代码。

2 个答案:

答案 0 :(得分:1)

您可能想要使用GET变量,您需要将所有文件合并为一个名为change.php的文件。在此文件中,您需要行$foo = $_GET["id"],它将在网址change.php?id=1中获取变量“id”的值。

if (isset($_GET["id"])) {

    $foo = $_GET["id"];

//your code here

}else{
    echo 'ERROR!!! No id in URL';
    }

您可以在网址中包含多个变量,如下所示:change.php?id=1&a=bar&b=toofoo

答案 1 :(得分:0)

您可以获取当前脚本的文件名并解析整数。

__FILE__

给出当前脚本的名称。然后,

$myStr = preg_replace('/\.php$/', '', __FILE__);
$result = preg_replace('/change=$/', '', $myStr);
echo $result; // it's your id