[英]Referencing section of php code
我的网站上有以下PHP代码,该代码基本上访问一个PHP脚本,该脚本从mysql表中提取信息。
<?php include('event_connect.php'); ?>
是否可以在此脚本中包含对特定代码部分的引用?
基本上,我必须为单个下拉列表创建39个不同的脚本,例如在“ XXX”下拉列表中,仅显示标识符为“ xxx”的记录。
我认为比创建39个脚本更容易的是(如果可能的话),只需根据上面的代码在页面中的位置添加某种参考值即可。 有点像VBA如何让您从另一个调用选定的函数...
编辑:这是我用来访问mysql数据库的PHP,其中添加了niet提供的代码-很好! 谢谢。
<?php
//Get te CSS styling
// Make a MySQL Connection
mysql_connect("xxxx", "xxxx", "xxxx", "xxxx") or
die(mysql_error());
mysql_select_db("xxxx") or die(mysql_error());
// Retrieve all the data from the "xxxx" table
switch($section) {
case 1:
$result = mysql_query("SELECT * FROM `Events_List` WHERE `Value` = 'value_1' LIMIT 0 , 30") or die(mysql_error());
break;
case 2:
$result = mysql_query("SELECT * FROM `Events_List` WHERE `Value` = 'value_2' LIMIT 0 , 30") or die(mysql_error());
break;
// store the records of the "xxxx" table into $row array and loop through
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $name = $row['name'];
$Event = $row['Event'];
$Date = $row['Date'];
$Type = $row['Type'];
$Description = $row['Description'];
$Event = htmlspecialchars($row['Event'],ENT_QUOTES);
$Date = htmlspecialchars($row['Date'],ENT_QUOTES);
$Type = htmlspecialchars($row['Type'],ENT_QUOTES);
$Description = htmlspecialchars($row['Description'],ENT_QUOTES);
echo " <div id='ev_con'>
<div id='ev_img'> </div>
<div id='Event'> $Event </div>
<div id='Date'> $Date </div>
<div id='Type'> $Type </div>
<div id='Description'> $Description </div>
</div>";}
?>
这样可以很好地运行一个查询,但是无法运行多个查询。
也许是这样的吗?
$section = 1;
include("event_connect.php");
并在您的event_connect文件中:
switch($section) {
case 1:
// do things
break;
case 2:
// do other things
break;
// ...
}
怎么样...
function get_event_connect($eventID){
switch($eventID){
case "1" :
// output code
break;
case "2" :
//output code
break;
}
}
$eventID = "1";
echo get_event_connect($eventID);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.