html & php question (get command)

2019-09-05 17:10发布

问题:

Want to include another value in my urls

so far I have

   # valid pages
  $page = array('splash', 'portraits', 'weddings', 'studio', 'about', 'mobile',      'personal');

 # validate GET, default to splash if not provided
 $_GET['q'] = (isset($_GET['q'])) ? strtolower(trim($_GET['q'])) : 'splash';
 if (!in_array($_GET['q'], $page))
 $_GET['q'] = 'splash';

require_once "{$_GET['q']}.html";

but I want to include sections into my site so I need an extra value (ie. ?q=portraits?z=studio )

I assume I could probably just duplicate the $get command and replace q with z... but suppose I don't want the second value to be necessary (so the default pages will display without extra commands) and i want it to target a div or frame within the page? Not sure if I can even target divs.

回答1:

Just use a default value:

if (isset($_GET['z'])) {
    $z = $_GET['z'];
}
else {
    $z = NULL; // or 'default' or whatever you want.
}

You can in short write the above in one line:

$z = isset($_GET['z']) ? $_GET['z'] : NULL;

I am not sure what you mean with "target a div", but if you mean an anchor, you can do that via identifiers (id).

Call your PHP script via index.php?q=splash#text and in your splash.html file you somewhere have <div id="splash">. The browser should scroll there.



标签: php get