该如何写循环

daiweilovezj 2011-08-09 04:40:04
我现在碰到这样的问题。我现在要做12条执行语句,但是显然一条条做很废时间,时间很久,希望可以得到循环帮助 我的代码如下:
       list($tqc)=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
$sqll="select distinct crew_code,t_name from tro_crew where sel_area='$tqc'";
$st1=$dbh->prepare("$sqll");
$st1->execute();

list($tqc1)=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
$sqll1="select distinct crew_code,t_name from tro_crew where sel_area='$tqc1'";
$st2=$dbh->prepare("$sqll1");
$st2->execute();

list($tqc2)=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
$sqll2="select distinct crew_code,t_name from tro_crew where sel_area='$tqc2'";
$st3=$dbh->prepare("$sqll2");
$st3->execute();

list($tqc3)=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
$sqll3="select distinct crew_code,t_name from tro_crew where sel_area='$tqc3'";
$st4=$dbh->prepare("$sqll3");
$st4->execute();

list($tqc4)=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
$sqll4="select distinct crew_code,t_name from tro_crew where sel_area='$tqc4'";
$st5=$dbh->prepare("$sqll4");
$st5->execute();

list($tqc5)=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
$sqll5="select distinct crew_code,t_name from tro_crew where sel_area='$tqc5'";
$st6=$dbh->prepare("$sqll5");
$st6->execute();

list($tqc6)=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
$sqll6="select distinct crew_code,t_name from tro_crew where sel_area='$tqc6'";
$st7=$dbh->prepare("$sqll6");
$st7->execute();

list($tqc7)=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
$sqll7="select distinct crew_code,t_name from tro_crew where sel_area='$tqc7'";
$st8=$dbh->prepare("$sqll7");
$st8->execute();

list($tqc8)=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
$sqll8="select distinct crew_code,t_name from tro_crew where sel_area='$tqc8'";
$st9=$dbh->prepare("$sqll8");
$st9->execute();

list($tqc9)=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
$sqll9="select distinct crew_code,t_name from tro_crew where sel_area='$tqc9'";
$st10=$dbh->prepare("$sqll9");
$st10->execute();

list($tqc10)=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
$sqll10="select distinct crew_code,t_name from tro_crew where sel_area='$tqc10'";
$st11=$dbh->prepare("$sqll10");
$st11->execute();

list($tqc11)=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
$sqll11="select distinct crew_code,t_name from tro_crew where sel_area='$tqc11'";
$st12=$dbh->prepare("$sqll11");
$st12->execute();
...全文
187 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
daiweilovezj 2011-08-23
  • 打赏
  • 举报
回复
大家可以帮忙看看吗?
daiweilovezj 2011-08-23
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 wswssx 的回复:]
有很多重复的东西,为什么不能放在一个循环里呢。
[/Quote]
因为我一个页面上有12个下拉框,分别将这些内容塞入。
wswssx 2011-08-23
  • 打赏
  • 举报
回复
有很多重复的东西,为什么不能放在一个循环里呢。
caizhenyu888 2011-08-23
  • 打赏
  • 举报
回复
那么多的重复代码。。。。 难道for不能解决嘛!
daiweilovezj 2011-08-10
  • 打赏
  • 举报
回复
额。大家帮忙一起看看吧。
daiweilovezj 2011-08-10
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 xuzuning 的回复:]
select distinct crew_code,t_name, tgc
from tro_crew t1,
( select substr(sel_area,1,3) as tgc from tro_crew where employee_no='39221' ) t2
where t1.sel_area=t2.tqc
limit 12;

我认为这样写就可以了
……
[/Quote]
由于是用ORACLE+PHP写的
下面是源代码,麻烦各位有空的话可以看下。
//取出区域 tro_sel
for($i=0;$i<12;$i++)
{
list($tqc[$i])=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
$sqll[$i]="select distinct crew_code,t_name from tro_crew where sel_area='$tqc[$i]'";
$st[$i+1]=$dbh->prepare("$sqll[$i]");
$st[$i+1]->execute();

echo "1[$sqll[1]]";
echo "2[$sqll[2]]";





//取出区域 tro_sel 结束

echo " <center> ";
echo "<table border=1 width=400 BORDERCOLOR=gray>";
echo "<tr>";
//echo "<td width=300>作番地址</td>";
echo "<td>作番数</td>";
echo "<td>保养员</td>";
//echo "<td>服务系数</td>";
//echo "<td>平均服务系数</td>";
echo "</tr></table>";
echo " <center> ";
echo "<table border=1 width=400 BORDERCOLOR=gray>";
echo "<tr>";
echo "<td width=105><input name='fangshi' type='text' id='fangshi' size=12/></td>";

echo "<td width=105>";
//select框的显示
echo "<select name='duoxuan' onchange='
fangshi.value= this.options[this.selectedIndex].test;'>";
echo "<option value=''></option>";
while(list($tcrew,$tcname)=$st[$i]->fetch())
{
{
list($tt)=fields("select count(distinct mfg_no) from pda_mugn_mobile where mmcd='$tcrew'");
echo "<option value='".$tcrew."' name=qname test='$tt'>$tcrew-$tcname</option>";
}
}
echo "</select></td>";
echo "</tr>";
//select框的显示结束

//1
echo "<tr>";
echo "<td width=105><input name='fangshi1' type='text' id='fangshi1' size=12/></td>";
echo "<td width=105>";
//select框的显示
echo "<select name='duoxuan1' onchange='
fangshi1.value= this.options[this.selectedIndex].test;'>";
echo "<option value=''></option>";
while(list($tcrew1,$tcname1)=$st[$i]->fetch())
{
{
list($tt1)=fields("select count(distinct mfg_no) from pda_mugn_mobile where mmcd='$tcrew1'");
echo "<option value='".$tcrew1."' name=qname test='$tt1'>$tcrew1-$tcname1</option>";
}
}
echo "</select></td>";
echo "</tr>";
//2
echo "<tr>";
echo "<td width=105><input name='fangshi2' type='text' id='fangshi2' size=12/></td>";
echo "<td width=105>";
//select框的显示
echo "<select name='duoxuan2' onchange='
fangshi2.value= this.options[this.selectedIndex].test;'>";
echo "<option value=''></option>";
while(list($tcrew2,$tcname2)=$st[$i]->fetch())
{
{
list($tt2)=fields("select count(distinct mfg_no) from pda_mugn_mobile where mmcd='$tcrew2'");
echo "<option value='".$tcrew2."' name=qname2 test='$tt2'>$tcrew2-$tcname2</option>";
}
}
echo "</select></td>";
echo "</tr>";
//3
echo "<tr>";
echo "<td width=105><input name='fangshi3' type='text' id='fangshi3' size=12/></td>";
echo "<td width=105>";
//select框的显示
echo "<select name='duoxuan3' onchange='
fangshi3.value= this.options[this.selectedIndex].test;'>";
echo "<option value=''></option>";
while(list($tcrew3,$tcname3)=$st[$i]->fetch())
{
{
list($tt3)=fields("select count(distinct mfg_no) from pda_mugn_mobile where mmcd='$tcrew3'");

echo "<option value='".$tcrew3."' name=qname3 test='$tt3'>$tcrew3-$tcname3</option>";
//echo "<option value=''></option>";
}
}
echo "</select></td>";
echo "</tr>";
//4
echo "<tr>";
echo "<td width=105><input name='fangshi4' type='text' id='fangshi4' size=12/></td>";
echo "<td width=105>";
//select框的显示
echo "<select name='duoxuan4' onchange='
fangshi4.value= this.options[this.selectedIndex].test;'>";
echo "<option value=''></option>";
while(list($tcrew4,$tcname4)=$st[$i]->fetch())
{
{
list($tt4)=fields("select count(distinct mfg_no) from pda_mugn_mobile where mmcd='$tcrew4'");
echo "<option value='".$tcrew4."' name=qname4 test='$tt4'>$tcrew4-$tcname4</option>";
}
}
echo "</select></td>";
echo "</tr>";

//5
echo "<tr>";
echo "<td width=105><input name='fangshi5' type='text' id='fangshi5' size=12/></td>";
echo "<td width=105>";
//select框的显示
echo "<select name='duoxuan5' onchange='
fangshi5.value= this.options[this.selectedIndex].test;'>";
echo "<option value=''></option>";
while(list($tcrew5,$tcname5)=$st[$i]->fetch())
{
{
list($tt5)=fields("select count(distinct mfg_no) from pda_mugn_mobile where mmcd='$tcrew5'");
echo "<option value='".$tcrew5."' name=qname5 test='$tt5'>$tcrew5-$tcname5</option>";
}
}
echo "</select></td>";
echo "</tr>";

//6
echo "<tr>";
echo "<td width=105><input name='fangshi6' type='text' id='fangshi6' size=12/></td>";
echo "<td width=105>";
//select框的显示
echo "<select name='duoxuan6' onchange='
fangshi6.value= this.options[this.selectedIndex].test;'>";
echo "<option value=''></option>";
while(list($tcrew6,$tcname6)=$st[$i]->fetch())
{
{
list($tt6)=fields("select count(distinct mfg_no) from pda_mugn_mobile where mmcd='$tcrew6'");
echo "<option value='".$tcrew6."' name=qname6 test='$tt6'>$tcrew6-$tcname6</option>";
}
}
echo "</select></td>";
echo "</tr>";

//7
echo "<tr>";
echo "<td width=105><input name='fangshi7' type='text' id='fangshi7' size=12/></td>";
echo "<td width=105>";
//select框的显示
echo "<select name='duoxuan7' onchange='
fangshi7.value= this.options[this.selectedIndex].test;'>";
echo "<option value=''></option>";
while(list($tcrew7,$tcname7)=$st[$i]->fetch())
{
{
list($tt7)=fields("select count(distinct mfg_no) from pda_mugn_mobile where mmcd='$tcrew7'");
echo "<option value='".$tcrew7."' name=qname7 test='$tt7'>$tcrew7-$tcname7</option>";
}
}
echo "</select></td>";
echo "</tr>";

//8
echo "<tr>";
echo "<td width=105><input name='fangshi8' type='text' id='fangshi8' size=12/></td>";
echo "<td width=105>";
//select框的显示
echo "<select name='duoxuan8' onchange='
fangshi8.value= this.options[this.selectedIndex].test;'>";
echo "<option value=''></option>";
while(list($tcrew8,$tcname8)=$st[$i]->fetch())
{
{
list($tt8)=fields("select count(distinct mfg_no) from pda_mugn_mobile where mmcd='$tcrew8'");
echo "<option value='".$tcrew8."' name=qname8 test='$tt8'>$tcrew8-$tcname8</option>";
}
}
echo "</select></td>";
echo "</tr>";

//9
echo "<tr>";
echo "<td width=105><input name='fangshi9' type='text' id='fangshi9' size=12/></td>";
echo "<td width=105>";
//select框的显示
echo "<select name='duoxuan9' onchange='
fangshi9.value= this.options[this.selectedIndex].test;'>";
echo "<option value=''></option>";
while(list($tcrew9,$tcname9)=$st[$i]->fetch())
{
{
list($tt9)=fields("select count(distinct mfg_no) from pda_mugn_mobile where mmcd='$tcrew9'");
echo "<option value='".$tcrew9."' name=qname9 test='$tt9'>$tcrew9-$tcname9</option>";
}
}
echo "</select></td>";
echo "</tr>";

//10
echo "<tr>";
echo "<td width=105><input name='fangshi10' type='text' id='fangshi10' size=12/></td>";
echo "<td width=105>";
//select框的显示
echo "<select name='duoxuan10' onchange='
fangshi10.value= this.options[this.selectedIndex].test;'>";
echo "<option value=''></option>";
while(list($tcrew10,$tcname10)=$st[$i]->fetch())
{
{
list($tt10)=fields("select count(distinct mfg_no) from pda_mugn_mobile where mmcd='$tcrew10'");
echo "<option value='".$tcrew10."' name=qname10 test='$tt10'>$tcrew10-$tcname10</option>";
}
}
echo "</select></td>";
echo "</tr>";

//11
echo "<tr>";
echo "<td width=105><input name='fangshi11' type='text' id='fangshi11' size=12/></td>";
echo "<td width=105>";
//select框的显示
echo "<select name='duoxuan11' onchange='
fangshi11.value= this.options[this.selectedIndex].test;'>";
echo "<option value=''></option>";
while(list($tcrew11,$tcname11)=$st[$i]->fetch())
{
{
list($tt11)=fields("select count(distinct mfg_no) from pda_mugn_mobile where mmcd='$tcrew11'");
echo "<option value='".$tcrew11."' name=qname11 test='$tt11'>$tcrew11-$tcname11</option>";
}
}
echo "</select></td>";
echo "</tr>";

echo "</table>";
}
echo "1[$sqll[1]]";
echo "2[$sqll[2]]";
xuzuning 2011-08-10
  • 打赏
  • 举报
回复
select distinct crew_code,t_name, tgc
from tro_crew t1,
( select substr(sel_area,1,3) as tgc from tro_crew where employee_no='39221' ) t2
where t1.sel_area=t2.tqc
limit 12;

我认为这样写就可以了

daiweilovezj 2011-08-10
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 rczjp 的回复:]
for(12 次){存入到一个数组里面}
[/Quote]
我做了。但是达不到效果。
rczjp 2011-08-10
  • 打赏
  • 举报
回复
for(12 次){存入到一个数组里面}
daiweilovezj 2011-08-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 xuzuning 的回复:]
那么你的 SQL 写作
select distinct crew_code,t_name from tro_crew where crew_code=substr(sel_area,1,3) and employee_no='39221'

不就一下子都出来了吗?
[/Quote]

HI.
我的用意是。需要执行12次那个语句,然后再将12次执行的结果赋出来,是否有优化的操作,否则每次执行这样12遍要好久。
xuzuning 2011-08-10
  • 打赏
  • 举报
回复
那么你的 SQL 写作
select distinct crew_code,t_name from tro_crew where crew_code=substr(sel_area,1,3) and employee_no='39221'

不就一下子都出来了吗?
daiweilovezj 2011-08-10
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xuzuning 的回复:]
fields 函数是如何定义的?
[/Quote]

我是这样定义的
function fields($sql) {
global $dbh;
$stmt = $dbh->prepare("$sql");
if ($stmt->execute()) {
return $stmt->fetch();
}
kyo7469 2011-08-09
  • 打赏
  • 举报
回复
哦 应该是for(i=0;i<12;i++)
kyo7469 2011-08-09
  • 打赏
  • 举报
回复
分别定义三组变量数组$tqc,$sqll,$tqc
用for循环就可以
for(i=0;i<13;i++)
{
list($tqc[i])=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
$sqll[i]="select distinct crew_code,t_name from tro_crew where sel_area='$tqc[i]'";
$st[i+1]=$dbh->prepare("$sqll[i]");
$st[i+1]->execute();
}

xuzuning 2011-08-09
  • 打赏
  • 举报
回复
fields 函数是如何定义的?
daiweilovezj 2011-08-09
  • 打赏
  • 举报
回复
谁可以帮忙看下吗??我实在是没头绪了。。

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧