简体   繁体   English

引用PHP代码部分

[英]Referencing section of php code

I have the following PHP code on my site which basically accesses a PHP script that pulls out information from a mysql table; 我的网站上有以下PHP代码,该代码基本上访问一个PHP脚本,该脚本从mysql表中提取信息。

<?php include('event_connect.php'); ?>

Is it possible to include a reference to a specific section of code in this script? 是否可以在此脚本中包含对特定代码部分的引用?

Basically I have to create 39 different scripts for individual dropdown eg in the "XXX" drop down, only records with an identifier "xxx" will show up. 基本上,我必须为单个下拉列表创建39个不同的脚本,例如在“ XXX”下拉列表中,仅显示标识符为“ xxx”的记录。

What i thought would be easier than createing 39 scripts is (if possible) just add some sort of reference value to the above code depending on where in the page it is placed. 我认为比创建39个脚本更容易的是(如果可能的话),只需根据上面的代码在页面中的位置添加某种参考值即可。 Kind of like how VBA allows you to call a selected function from another... 有点像VBA如何让您从另一个调用选定的函数...

Edit: This is the PHP i use to access the mysql database, with the code provided by niet added in - works a treat! 编辑:这是我用来访问mysql数据库的PHP,其中添加了niet提供的代码-很好! Thank you. 谢谢。

<?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>";}

?> 

This runs one query fine, but having trouble getting multiple queries running.. 这样可以很好地运行一个查询,但是无法运行多个查询。

Maybe something like this? 也许是这样的吗?

$section = 1;
include("event_connect.php");

And in your event_connect file: 并在您的event_connect文件中:

switch($section) {
case 1:
    // do things
    break;
case 2:
    // do other things
    break;
// ...
}

how about... 怎么样...

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.

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