2013-04-28 173 views
-1

我想知道你如何使用? (问号)在你的网站,使它根据url中的变量做不同的东西。 像: http://example.com/php/learn/kinder.php?level= $级别 就像那叫什么,你如何使用它? 我假设有一个switch语句 这是我的代码在momment:使用带变量的URL来生效代码

<php 
$con=mysqli_connect("host","username","pass","db"); 
    //Check connection 
if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

$level = mysqli_query($con,"SELECT gradek FROM profiles"); 
?> 
<html> 
<head> 
<title> Redirecting </title> 

<meta http-equiv="refresh" content="1; url=http://teachertechtutor.com/php/learn/kinder.php?level=$level> 
</head> 
<body> 
</body> 
</html> 

然后我就会有一个开关......我知道这是没有意义的。我只想知道以供将来参考。

所以我的问题是: 如何在html中使用php变量重定向 如何使用?更改代码。

我用bolli的代码,这就是出现在网页:

`<meta http-equiv="refresh" content="1; url=http://teachertechtutor.com/php/learn/kinder.php?level2=> </head> <body> </body> </html> 

,它仍然无法正常重定向

+0

你的问题是什么? -1 – michi 2013-04-28 00:30:50

回答

0

在kinder.php地方:

$level = $_REQUEST['level']; 
echo $level; 

并阅读有关POST和GET请求。 http://www.tizag.com/phpT/postget.php

或者你的意思是如何将该变量放在URL中?

要么

<meta http-equiv="refresh" content="1; url=http://teachertechtutor.com/php/learn/kinder.php?level=<?php echo $level;?>> 

echo "<meta http-equiv="refresh" content="1; url=http://teachertechtutor.com/php/learn/kinder.php?level=".$level; 

编辑/ UPDATE

你试图让变量并在此基础上,切换页面内容?

你可以做这样的事情吗?

<?php 

//you could load header here 
//include 'header.php'; 

/* 
*Load content based on $_get parameter 
*/ 

    // Get the $variable from the url after the level= 
$page = (isset($_GET['level'])) ? $_GET['level'] : 'index'; 

//Check to see if the file exist 
if (!file_exists($page . '.php')) 
{ 
//echo "File does not exist" . $page; 
} 

switch ($page) 
{ 
case 'test': 
include('test.php'); 
break; 

case 'test2': 
include('test2.php'); 
break; 

case 'test3': 
include('test3.php'); 
break; 

//Defualt start page 
default: 
include('index.php'); 

} 

// You could load footer here 
//include 'footer.php'; 
?> 
+0

我如何使用这个开关?只需在url中插入一个带有变量的开关,对吧? – James 2013-04-28 00:38:49

+0

我会从你的第一个代码中获得变量,对吧? – James 2013-04-28 00:40:35

+0

是的,您可以通过$ _GET ['level']或$ _REQUEST ['level]获得请求。你也可以在switch语句中使用它,或者如果你还有其他东西或者你喜欢的东西。 请尝试更好地解释你在做什么,也许我们可以帮助进一步。 – Bolli 2013-04-28 02:58:30