Question

I have a number pages namely

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

They all have similar coding but leaving 1 or 2 variable values. And I know its a very bad idea! How can I make a link work like below:

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;
?>

My PHP code.

Était-ce utile?

La solution

You probably want to use GET variables, for which you need to combine all the files into one, named change.php. In this file you need the line $foo = $_GET["id"] which will get the value of the variable "id" in the url change.php?id=1.

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

    $foo = $_GET["id"];

//your code here

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

You can have several variables in the URL like this: change.php?id=1&a=bar&b=toofoo

Autres conseils

You can get current script's file name and parse integer.

__FILE__

gives current script's name. Then,

$myStr = preg_replace('/\.php$/', '', __FILE__);
$result = preg_replace('/change=$/', '', $myStr);
echo $result; // it's your id
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top