Membuat Judul Halaman & Menu Dinamis pada Web Statis


Status
Not open for further replies.

Fadli

Poster 2.0
saya coba buat:
PHP:
	<?php
		$here = basename($_SERVER['SCRIPT_FILENAME']);
		$pages = array('index.php', 'about.php', 'contact.php');
		
		$current = array_search($here, $pages) === false  ?  'current'  :  'none';
	?>
	<ul>
	   <li class="<?php echo $current; ?>"><a href="index.php">Home</a></li>
	   <li class="<?php echo $current; ?>"><a href="about.php">About</a></li>
	   <li class="<?php echo $current; ?>"><a href="contact.php">Contact</a></li>
	</ul>

tapi malah ga bisa mas. dimana salahnya?
 

galuh82

Hosting Guru
Verified Provider
pastikan dulu variable $here .. apakah valuenya ada yang sama dengan $pages ..

tampilkan saja isi $here dulu untuk memastikan
 

Fadli

Poster 2.0
:D nemu cara lain. navigasinya:
PHP:
		<ul>
			<li <?php if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="current"';?>><a href="index.php">Home</a></li>
			<li <?php if (strpos($_SERVER['PHP_SELF'], 'about.php')) echo 'class="current"';?>><a href="about.php">About</a></li>
			<li <?php if (strpos($_SERVER['PHP_SELF'], 'contact.php')) echo 'class="current"';?>><a href="contact.php">Contact</a></li>
		</ul>

di title:
PHP:
<title><?php
if (strpos($_SERVER['PHP_SELF'], 'index.php'))
	echo '';
else if (strpos($_SERVER['PHP_SELF'], 'about.php'))
	echo 'About >';
else if (strpos($_SERVER['PHP_SELF'], 'contact.php'))
	echo 'Contact >';
?> <?php echo $title; ?></title>

cuma saya masih penasaran, ada cara ga untuk menyingkat scriptnya? kok kayaknya mubazir yah banyak nulis strpos($_SERVER['PHP_SELF'] berulang kali??

trus dari segi resource apa ini banyak membebani server ga yah??
 

galuh82

Hosting Guru
Verified Provider
ya php_self juga bisa, detilnya beberapa variable server bisa dilihat dari phpinfo

kalo triknya sudah ketemu tinggal anda cari optimasi-nya biar scriptnya lebih singkat :)
 

dino

Beginner 2.0
coba dibuat fungsi php aja

PHP:
<?php
function current_class(){
	$file = $_SERVER["SCRIPT_NAME"];
	
	if 	 ($file == '/about.php') { echo ' class="current"'; }
	elseif ( $file == '/contact.php' ) { echo ' class="current"'; }
	else 	( $file == '/index.php' ) { echo ' class="current"'; }

}
?>
<head>
<title>HOME</title>
</head>
<body>
<ul>
     <li><a href="index.php" <?php current_class(); ?>>HOME</a></li>
     <li><a href="about.php" <?php current_class(); ?>>About</a></li>
     <li><a href="contact.php" <?php current_class(); ?>>Contact</a></li>
</ul>  
</body>
 

dino

Beginner 2.0
saya coba buat:
PHP:
	<?php
		$here = basename($_SERVER['SCRIPT_FILENAME']);
		$pages = array('index.php', 'about.php', 'contact.php');
		
		$current = array_search($here, $pages) === false  ?  'current'  :  'none';
	?>
	<ul>
	   <li class="<?php echo $current; ?>"><a href="index.php">Home</a></li>
	   <li class="<?php echo $current; ?>"><a href="about.php">About</a></li>
	   <li class="<?php echo $current; ?>"><a href="contact.php">Contact</a></li>
	</ul>

tapi malah ga bisa mas. dimana salahnya?

kalo pake kodenya Tuan ganti
PHP:
 $pages = array('index.php', 'about.php', 'contact.php');

dengan

PHP:
 $pages = array('/index.php', '/about.php', '/contact.php');
 

pasarhosting.com

Beginner 1.0
maaf kalu therad-nya sudah lama...
sy mau sedikit manambahkan pendapat...

untuk title, bisa juga diakali dengan cara mengirim value berupa title-nya dengan method get, nanti di bagian title di set juga kode php untuk menampung value tersebut tapi cara ini tidak disarankan untuk halaman yg ada login-nya, atau proses pengiriman data penting lainnya...

ada beberapa cara lain dari teman2 kita diatas yg bisa juga dipraktekkan, yg jelas saran sy, gunakan kode se-simpel mungkin, untuk mengurangi resource dan waktu load page...CMIIW...
 
Status
Not open for further replies.

Top