我正在为学校开展一个项目,主要是数据管理和演示。我有一段很长的代码需要复制并粘贴到项目的不同部分,所以我想我会把它变成一个函数。我可以将在函数外部工作的代码复制并粘贴到一个空函数中,它不会产生代码在函数外部生成的效果(但不会导致错误)。这是我第一次在php中使用一个函数,看起来很简单,但我无法让它工作。
以下是有效的代码:
<?php
//function display_question($problem_id, $slide_num, $attempt, $admin_id, $result){
//}
include('header.php');
$result = "";
if(isset($_POST['problem_id'])){
if (!isset($_SESSION['slide']) || empty($_SESSION['slide'])){
$_SESSION['slide'] = 1;
}
$slide_num = $_SESSION['slide'];
$problem_id = $_POST['problem_id'];
$admin_id = $_SESSION['user_id'];
if (!isset($_SESSION['phase']) || empty($_SESSION['phase'])){
$_SESSION['phase'] = 1;
}
$phase = $_SESSION['phase'];
if($phase == 1){ //show problem attempt#1
$attempt = 1;
}elseif($phase == 2){ //graph attempt#1
}elseif($phase == 3){ //match Students into groups
}elseif($phase == 4){ //show problem attempt#2
$attempt = 2;
}elseif($phase == 5){ //graph attempt#2
}else{
$result = 'phase Error';
}
//display_question($problem_id, $slide_num, $attempt, $admin_id, $result);
$update = mysql_query("UPDATE users SET active=1, slide_id='$problem_id', attempt='$attempt' WHERE id='$admin_id'");
$query = mysql_query("SELECT * FROM problem WHERE id='$problem_id'");
if($update && $query){
$row = mysql_fetch_row($query);
$topic = $row[2];
$problem = $row[3];
$solution_a = $row[4];
$solution_b = $row[5];
$solution_c = $row[6];
$image = $row[7];
if(!empty($image)){
$result = 'Admin ID: '.$admin_id.'</br><div align="right">Slide '.$slide_num.'</div>
Attempt #: '.$attempt.'
<h1>'.$topic.'</h1>
<img src="'.$image.'" width="500"/>
<h2>'.$problem.'</h3>
<h3><ol type="A">
<li>'.$solution_a.'</li>
<li>'.$solution_b.'</li>
<li>'.$solution_c.'</li>
</ol></h3>';
}else{
$result = 'Admin ID: '.$admin_id.'</br><div align="right">Slide '.$slide_num.'</div>
Attempt #: '.$attempt.'</br>
<h1>'.$topic.'</h1>
<h2>'.$problem.'</h3>
<h3><ol type="A">
<li>'.$solution_a.'</li>
<li>'.$solution_b.'</li>
<li>'.$solution_c.'</li>
</ol></h3>';
}
}else{
$result = "Error";
}
$result .= ' </br>
<div id="outer"><div class="inner"><form action="slide.php" method="post">
<input type="number" value="'.$id.'" hidden="hidden" name="problem_id"/>
<input type="submit" value="Prev Slide" class="submit"/>
</form></div>
<div class="inner"><form action="slide.php" method="post">
<input type="number" value="'.$id.'" hidden="hidden" name="problem_id"/>
<input type="submit" value="Next Phase" class="submit"/>
</form></div>
<div class="inner"><form action="slide.php" method="post">
<input type="number" value="'.$id.'" hidden="hidden" name="problem_id"/>
<input type="submit" value="Next Slide" class="submit"/>
</form></div></div>';
} else {
$result = "problem_id not set";
}
?>
<!DOCTYPE HTML>
<html>
<title>Presentation</title>
<link rel="stylesheet" type="text/css" href="css/header.css">
<style type="text/css">
div.main{
width: 500px;
margin-top: 25px;
margin-left: auto;
margin-right: auto;
padding: 20px;
background-color: white;
border: 2px solid black;
border-radius: 5px;
}
#outer{
width:100%;
text-align: center;
}
div.inner{
display: inline-block;
}
form.problem> input{
font-size: 1.1em;
margin-top: 10px;
}
form.nav{
font-size: 1.1em;
margin-top: 10px;
display: inline;
}
input.submit{
width: 120px;
height: 40px;
color: white;
background-color: green;
border: none;
border-radius: 5px;
margin-top: 25px;
}
input.submit:hover{
background-color: #006600;
}
</style>
</head>
<body>
<div class="main">
<?php echo $result;?>
</div>
</body>
</html>
这是代码副本并粘贴到不起作用的函数中:
<?php
function display_question($problem_id, $slide_num, $attempt, $admin_id, $result){
$update = mysql_query("UPDATE users SET active=1, slide_id='$problem_id', attempt='$attempt' WHERE id='$admin_id'");
$query = mysql_query("SELECT * FROM problem WHERE id='$problem_id'");
if($update && $query){
$row = mysql_fetch_row($query);
$topic = $row[2];
$problem = $row[3];
$solution_a = $row[4];
$solution_b = $row[5];
$solution_c = $row[6];
$image = $row[7];
if(!empty($image)){
$result = 'Admin ID: '.$admin_id.'</br><div align="right">Slide '.$slide_num.'</div>
Attempt #: '.$attempt.'
<h1>'.$topic.'</h1>
<img src="'.$image.'" width="500"/>
<h2>'.$problem.'</h3>
<h3><ol type="A">
<li>'.$solution_a.'</li>
<li>'.$solution_b.'</li>
<li>'.$solution_c.'</li>
</ol></h3>';
}else{
$result = 'Admin ID: '.$admin_id.'</br><div align="right">Slide '.$slide_num.'</div>
Attempt #: '.$attempt.'</br>
<h1>'.$topic.'</h1>
<h2>'.$problem.'</h3>
<h3><ol type="A">
<li>'.$solution_a.'</li>
<li>'.$solution_b.'</li>
<li>'.$solution_c.'</li>
</ol></h3>';
}
}else{
$result = "Error";
}
}
include('header.php');
$result = "";
if(isset($_POST['problem_id'])){
if (!isset($_SESSION['slide']) || empty($_SESSION['slide'])){
$_SESSION['slide'] = 1;
}
$slide_num = $_SESSION['slide'];
$problem_id = $_POST['problem_id'];
$admin_id = $_SESSION['user_id'];
if (!isset($_SESSION['phase']) || empty($_SESSION['phase'])){
$_SESSION['phase'] = 1;
}
$phase = $_SESSION['phase'];
if($phase == 1){ //show problem attempt#1
$attempt = 1;
}elseif($phase == 2){ //graph attempt#1
}elseif($phase == 3){ //match Students into groups
}elseif($phase == 4){ //show problem attempt#2
$attempt = 2;
}elseif($phase == 5){ //graph attempt#2
}else{
$result = 'phase Error';
}
display_question($problem_id, $slide_num, $attempt, $admin_id, $result);
$result .= ' </br>
<div id="outer"><div class="inner"><form action="slide.php" method="post">
<input type="number" value="'.$id.'" hidden="hidden" name="problem_id"/>
<input type="submit" value="Prev Slide" class="submit"/>
</form></div>
<div class="inner"><form action="slide.php" method="post">
<input type="number" value="'.$id.'" hidden="hidden" name="problem_id"/>
<input type="submit" value="Next Phase" class="submit"/>
</form></div>
<div class="inner"><form action="slide.php" method="post">
<input type="number" value="'.$id.'" hidden="hidden" name="problem_id"/>
<input type="submit" value="Next Slide" class="submit"/>
</form></div></div>';
} else {
$result = "problem_id not set";
}
?>
<!DOCTYPE HTML>
<html>
<title>Presentation</title>
<link rel="stylesheet" type="text/css" href="css/header.css">
<style type="text/css">
div.main{
width: 500px;
margin-top: 25px;
margin-left: auto;
margin-right: auto;
padding: 20px;
background-color: white;
border: 2px solid black;
border-radius: 5px;
}
#outer{
width:100%;
text-align: center;
}
div.inner{
display: inline-block;
}
form.problem> input{
font-size: 1.1em;
margin-top: 10px;
}
form.nav{
font-size: 1.1em;
margin-top: 10px;
display: inline;
}
input.submit{
width: 120px;
height: 40px;
color: white;
background-color: green;
border: none;
border-radius: 5px;
margin-top: 25px;
}
input.submit:hover{
background-color: #006600;
}
</style>
</head>
<body>
<div class="main">
<?php echo $result;?>
</div>
</body>
</html>
变量$ result除了之后添加的内容外,没有打印任何内容。从我读到的,函数args默认传递给byval所以我不认为这是问题,我已经检查了变量的所有名称,以确保它们在函数中定义或传递到函数中,并通过我能看到的他们是。我不知道还有什么可以寻找的。它可能很容易让我看起来很愚蠢,但我是php的新手,所以请帮助我。
答案 0 :(得分:1)
您没有将结果作为参考传递。
你需要像这样定义你的函数:
function display_question($problem_id, $slide_num, $attempt, $admin_id, &$result)
^
答案 1 :(得分:0)
如果您希望函数修改它,则必须通过引用传递$ result。现在,您声明函数的方式(函数display_question($ problem_id,$ slide_num,$ attempt,$ admin_id,$ result))是按值传递的,用于所有参数。试试这个:
function display_question($problem_id, $slide_num, $attempt, $admin_id, &$result)