所以我在这样的页面上有某种代码
<?php
$a = $_GET['act'];
if ($a){
include('show.php');
} else {
include('show.php');
}
?>
以及像这样的其他页面
<?php
$sql2 = "SELECT * from DONE ";
$b = mysqli_query ($con,$sql2);
$r = mysqli_fetch_assoc($b);
.......
?>
<?php
$sql3 = "SELECT * from ONGOING ";
$b = mysqli_query ($con,$sql3);
$r = mysqli_fetch_assoc($b);
.......
?>
我的问题,是否有可能在满足条件时执行第一个查询,当条件不满足时,执行第二个查询,当两个查询都放在同一页面上时?
答案 0 :(得分:1)
不是在主页面中添加条件,而是将其添加到包含的页面中,以便您的查询可以条件执行 主页
<?php
$a = $_GET['act'];
include('show.php');
?>
<强> show.php 强>
<?php
if ($a){
$sql2 = "SELECT * from DONE ";
$b = mysqli_query ($con,$sql2);
$r = mysqli_fetch_assoc($b);
//.......
}
else
{
$sql3 = "SELECT * from ONGOING ";
$b = mysqli_query ($con,$sql3);
$r = mysqli_fetch_assoc($b);
//.......
}
?>
答案 1 :(得分:1)
这正是PHP has functions的原因。在您包含的文件中,将这两件事组织成单独的代码块:
<?php
function fromDone($con) {
$b = mysqli_query($con,"SELECT * from DONE ");
$r = mysqli_fetch_assoc($b);
.......
}
function fromOngoing($con) {
$b = mysqli_query ($con,"SELECT * from ONGOING");
$r = mysqli_fetch_assoc($b);
.......
}
?>
然后在你的主文件中你可以这样做:
<?php
if ($_GET['act']) {
fromDone($con);
} else {
fromOngoing($con);
}
?>
答案 2 :(得分:0)
<?php
$a = $_GET['act'];
if($a)
$sql = "SELECT * from DONE ";
else
$sql = "SELECT * from ONGOING ";
$b = mysqli_query ($con,$sql);
$r = mysqli_fetch_assoc($b);
.......
?>
答案 3 :(得分:0)
使用变量来判断要运行的查询。
<?php
$a = $_GET['act'];
if ($a){
include('show.php');
$code = 1;
}
else {
include('show.php');
$code = 2;
}
?>
<?php
if($code == 1){
$sql = "SELECT * from DONE ";
}
else{
$sql = "SELECT * from ONGOING ";
}
?>
<?php
$b = mysqli_query ($con,$sql);
$r = mysqli_fetch_assoc($b);
.......
?>
答案 4 :(得分:0)
<?php
$a = $_GET['act'];
if ($a){
include('show.php?condition=ok');
} else {
include('show.php?condition=notok');
}
?>
以及像这样的其他页面
<?php
if($_GET['condition']=='ok')
{
$sql2 = "SELECT * from DONE ";
$b = mysqli_query ($con,$sql2);
$r = mysqli_fetch_assoc($b);
.......
}
else if($_GET['condition']=='notok') {
$sql3 = "SELECT * from ONGOING ";
$b = mysqli_query ($con,$sql3);
$r = mysqli_fetch_assoc($b);
.......
} else {
//other stuff
}
?>
答案 5 :(得分:0)
Main.php
<?php
$a = $_GET['act'];
if($a)
$table='tableone';
else
$table='tabletwo';
include('show.php?table=$table');
?>
show.php
<?php
$sql = "SELECT * from ".$_GET['table'];
$b = mysqli_query ($con,$sql);
$r = mysqli_fetch_assoc($b);
?>