2

I'm very new to the MySQL world so I'm sorry if this might be something obvious!

What I'm trying to do is a Query that outputs price data on different pages, at the moment it works fine - however on each page i use the query I have to change the productpage name in the query. How can I automatically get the filename of the page (excluding .PHP) and insert that name into the query? (I have a corresponding column in the DB with all the different page names)

$query=("SELECT * 
        FROM database e 
        JOIN validdate1 r ON e.datevalid1=r.id 
        JOIN validdate2 d ON e.datevalid2=d.id 
        WHERE productpage='page01' 
        ORDER BY price2, 

So rather than manually entering page01, i want it to be fetched from the file name (in this case is page01.php).

Thank you in advance!

Barmar
  • 741,623
  • 53
  • 500
  • 612
Tobi San
  • 33
  • 5
  • have a look at the options: `print_r($_SERVER);` –  Jan 23 '15 at 01:40
  • if the only thing that changes is the file name, you only need one page and then use mod rewrite to send all traffic to it. –  Jan 23 '15 at 01:46
  • possible duplicate of [Get the current script file name](http://stackoverflow.com/questions/4221333/get-the-current-script-file-name) – kittykittybangbang Jan 23 '15 at 01:53

4 Answers4

1

echo __FILE__; outputs the current filename.

echo preg_replace('/\.php$/', '', __FILE__); outputs the current filename without '.php'.

Get the current script file name

kittykittybangbang
  • 2,380
  • 4
  • 16
  • 27
1

Use variable interpolation in your query:

$pageName = basename(__FILE__, '.php');
$query=("SELECT * FROM database e JOIN validdate1 r ON e.datevalid1=r.id JOIN validdate2 d ON e.datevalid2=d.id WHERE productpage='$pageName' ORDER BY price2");

...or use a prepared statement and submit the page name as an argument

0

Read the php page name using below code

$page=basename($_SERVER['PHP_SELF']); /* Returns PHP File Name */
$page_name=str_replace(".php","",$page);

$query=("SELECT * 
    FROM database e 
    JOIN validdate1 r ON e.datevalid1=r.id 
    JOIN validdate2 d ON e.datevalid2=d.id 
    WHERE productpage='$page_name' 
    ORDER BY price2, 
Sameeraa4ever
  • 568
  • 7
  • 20
  • This did the trick! Thank you very much Sameeraa4ever (and everyone else that replied for that matter!) – Tobi San Jan 23 '15 at 02:35
0

If the page link is some thing like this: http://yourhost/abc/page1

You can get /abc/page1 by using this:

$path = parse_url($url, PHP_URL_PATH)

Depends on the structure of your URL, you can use explode to get page 1

HoangND
  • 398
  • 2
  • 12