1

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.

7
  • Are these pages in array format? Commented May 4, 2014 at 10:05
  • No. I have saved them as "change=1.php"...and so on...! Commented May 4, 2014 at 10:06
  • Saved them where? Database? Files? Commented May 4, 2014 at 10:07
  • no...nothing like that.! We save files in php like: "change.php"... But I have used "=1"..so it becomes "change=1.php". And so I have created more pages by just incrementing the number.. Commented May 4, 2014 at 10:11
  • I really don't understand, could you paste that PHP part? Commented May 4, 2014 at 10:13

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.