简体   繁体   中英

Working with $_GET[page_id]

Okay, this might be a basic question. Here is my code:

<?php $pageid = $_GET[page_id]; ?>

And then:

<a href="" <?php if($pageid == '2' OR $pageid == '9') { echo...

So this makes it so that if they are on page ID 2 or 9, then it will echo a message. Now what I want to do is that I want to add index.php as one of those pages. But index.php does not have a page id. So I want <?php if($pageid == '2' OR $pageid == '9' OR index.php) { echo... How the heck do I do that? Just placing the filename wont work obviously.

if($pageid == '2' || $pageid == '9' || __FILE__ == 'index.php')

如果index.php没有pageid ,那么只需将你的if()放到......

<?php if($pageid == '2' || $pageid == '9' || !isset($_GET['page_id'])) { echo...

I would start your script with

 $pageid = false;

Then in your if statement you ca do

 <a href="" <?php if($pageid == '2' OR $pageid == '9' OR !$pageid) { echo...

That way if you never set pageid (aka index.php) then you are set in your if statement!

<?php $pageid = (isset($_GET[page_id]) ? $_GET[page_id] : 'index'); ?>

然后$ pageid将包含page_id或'index'(如果未提供)。

I would say:

if (!isset($_GET['page_id']) {
    $pageid = 0;
}

so you're always working with page ids, even when one isn't supplied.

You can check if they are on the index by using the base URI. So do this:

if($pageid == '2' || $pageid == '9' || $_SERVER['REQUEST_URI']=='/')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM