而不是像以下代码:
if ( $Boss1 ==1)
{
$Boss1_holder = $tick;
}
else if ( $Boss1 ==0)
{
$Boss1_holder = $cross;
}
if ( $Boss2 ==1)
{
$Boss2_holder = $tick;
}
else if ( $Boss2 ==0)
{
$Boss2_holder = $cross;
}
if ( $Boss3 ==1)
{
$Boss3_holder = $tick;
}
else if ( $Boss3 ==0)
{
$Boss3_holder = $cross;
}
if ( $Boss4 ==1)
{
$Boss4_holder = $tick;
}
else if ( $Boss4 ==0)
{
$Boss4_holder = $cross;
}
我可以改为做这样的事情:
$i = 1;
while ($i <= 4)
{
if ( $Boss[$i] ==1)
{
$boss_holder[$i] = $tick;
}
else if ( $Boss[$i] ==0)
{
$boss_holder[$i] = $cross;
}
}
我不完全确定如何做到这一点。目前如果只有4组if else语句,但我的程序后来需要大约30个,我宁愿像我的第二个块那样会减少所需的行数。我是在正确的轨道上。我认为$ boss_holder [$ i]部分不正确,因为[i]会成为数组的一部分?但是我不知道如何正确地写这个。我查看了php手册页,但很多例子都是复杂的,超出了我需要的范围。
澄清我是否已经解释得很糟糕。我试图在变量$ boss_holder的末尾添加一个值,该值随着while循环计数器
递增 $Boss1_holder = '';
$Boss2_holder = '';
$Boss3_holder = '';
$Boss4_holder = '';
$Boss5_holder = '';
$Boss6_holder = '';
$Boss7_holder = '';
$Boss8_holder = '';
$Boss9_holder = '';
$Boss10_holder = '';
$Boss11_holder = '';
$Boss12_holder = '';
$Boss13_holder = '';
$Boss14_holder = '';
$Boss15_holder = '';
$completed1 = 0;
$completed2 = 0;
$completed3 = 0;
$counter = 0;
$tick =' <img src="/styles/subsilver2/theme/images/soap/tick.png">';
$cross =' <img src="/styles/subsilver2/theme/images/soap/cross.png">';
班级检查 {
private $addthemup1;
private $addthemup2;
private $addthemup3;
public function checkBosses($Boss1, $Boss2, $Boss3, $Boss4, $Boss5, $Boss6, $Boss7, $Boss8, $Boss9, $Boss10, $Boss11, $Boss12, $Boss13, $Boss14, $Boss15)
{
global $tick, $cross, $completed1, $completed2, $completed3, $Boss1_holder, $Boss2_holder, $Boss3_holder, $Boss4_holder, $Boss5_holder, $Boss6_holder, $Boss7_holder, $Boss8_holder, $Boss9_holder, $Boss10_holder, $Boss11_holder, $Boss12_holder, $Boss13_holder, $Boss14_holder, $Boss15_holder;
$addthemup1 = $Boss1 + $Boss2 + $Boss3 + $Boss4 + $Boss5;
$addthemup2 = $Boss6 + $Boss7 + $Boss8 + $Boss9 + $Boss10;
$addthemup3 = $Boss11 + $Boss12 + $Boss13 + $Boss14 + $Boss15;
switch ($addthemup1)
{
case "5":
$completed1 = $tick;
break;
case "4":
$completed1 ='4/5';
break;
case "3":
$completed1 ='3/5';
break;
case "2":
$completed1 ='2/5';
break;
case "1":
$completed1 ='1/5';
break;
case "0":
$completed1 ='0/5';
break;
}
switch ($addthemup2)
{
case "5":
$completed2 = $tick;
break;
case "4":
$completed2 ='4/5';
break;
case "3":
$completed2 ='3/5';
break;
case "2":
$completed2 ='2/5';
break;
case "1":
$completed2 ='1/5';
break;
case "0":
$completed2 ='0/5';
break;
}
switch ($addthemup3)
{
case "5":
$completed3 = $tick;
break;
case "4":
$completed3 ='4/5';
break;
case "3":
$completed3 ='3/5';
break;
case "2":
$completed3 ='2/5';
break;
case "1":
$completed3 ='1/5';
break;
case "0":
$completed3 ='0/5';
break;
}
if ( $Boss1 ==1)
{
$Boss1_holder = $tick;
}
else if ( $Boss1 ==0)
{
$Boss1_holder = $cross;
}
if ( $Boss2 ==1)
{
$Boss2_holder = $tick;
}
else if ( $Boss2 ==0)
{
$Boss2_holder = $cross;
}
if ( $Boss3 ==1)
{
$Boss3_holder = $tick;
}
else if ( $Boss3 ==0)
{
$Boss3_holder = $cross;
}
if ( $Boss4 ==1)
{
$Boss4_holder = $tick;
}
else if ( $Boss4 ==0)
{
$Boss4_holder = $cross;
}
if ( $Boss5 ==1)
{
$Boss5_holder = $tick;
}
else if ( $Boss5 ==0)
{
$Boss5_holder = $cross;
}
if ( $Boss6 ==1)
{
$Boss6_holder = $tick;
}
else if ( $Boss6 ==0)
{
$Boss6_holder = $cross;
}
if ( $Boss7 ==1)
{
$Boss7_holder = $tick;
}
else if ( $Boss7 ==0)
{
$Boss7_holder = $cross;
}
if ( $Boss8 ==1)
{
$Boss8_holder = $tick;
}
else if ( $Boss8 ==0)
{
$Boss8_holder = $cross;
}
if ( $Boss9 ==1)
{
$Boss9_holder = $tick;
}
else if ( $Boss9 ==0)
{
$Boss9_holder = $cross;
}
if ( $Boss10 ==1)
{
$Boss10_holder = $tick;
}
else if ( $Boss10 ==0)
{
$Boss10_holder = $cross;
}
if ( $Boss11 ==1)
{
$Boss11_holder = $tick;
}
else if ( $Boss11 ==0)
{
$Boss11_holder = $cross;
}
if ( $Boss12 ==1)
{
$Boss12_holder = $tick;
}
else if ( $Boss12 ==0)
{
$Boss12_holder = $cross;
}
if ( $Boss13 ==1)
{
$Boss13_holder = $tick;
}
else if ( $Boss13 ==0)
{
$Boss13_holder = $cross;
}
if ( $Boss14 ==1)
{
$Boss14_holder = $tick;
}
else if ( $Boss14 ==0)
{
$Boss14_holder = $cross;
}
if ( $Boss15 ==1)
{
$Boss15_holder = $tick;
}
else if ( $Boss15 ==0)
{
$Boss15_holder = $cross;
}
}
}
$ sql ='SELECT * FROM raid_progression
';
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$counter = $counter+1;
$checker1 = new checks();
$checkResult1 = $checker1-> checkBosses($row['kp_em_no_bonethr'], $row['kp_em_no_foreman'], $row['kp_em_no_heavyfab'], $row['kp_em_no_jargsor'], $row['kp_em_no_kragga'],$row['kp_em_ha_bonethr'], $row['kp_em_ha_foreman'], $row['kp_em_ha_heavyfab'], $row['kp_em_ha_jargsor'], $row['kp_em_ha_kragga'],$row['kp_em_kn_bonethr'], $row['kp_em_kn_foreman'], $row['kp_em_kn_heavyfab'], $row['kp_em_kn_jargsor'], $row['kp_em_kn_kragga']);
$template->assign_block_vars('raid_op', array(
'VAR0' => $counter,
'VAR1' => $row['guild_name'],
'VAR2' => $row['guild_website'],
'VAR3' => $row['server_name'],
'VAR4' => $row['server_region'],
'VAR5' => $row['faction_name'],
'VAR6' => $completed1,
'VAR7' => $Boss1_holder,
'VAR8' => $Boss2_holder,
'VAR9' => $Boss3_holder,
'VAR10' => $Boss4_holder,
'VAR11' => $Boss5_holder,
'VAR12' => $Boss6_holder,
'VAR13' => $Boss7_holder,
'VAR14' => $Boss8_holder,
'VAR15' => $Boss9_holder,
'VAR16' => $Boss10_holder,
'VAR17' => $Boss11_holder,
'VAR18' => $Boss12_holder,
'VAR19' => $Boss13_holder,
'VAR20' => $Boss14_holder,
'VAR21' => $Boss15_holder,
'VAR22' => $completed2,
'VAR23' => $completed3,
));
}
$db->sql_freeresult($result);
答案 0 :(得分:3)
有几种方法可以做到......
$boss = new array(1, 2, 3, 4);
foreach($boss as $value){
switch($value){
case 1:
//do something
break;
case 2:
//something else;
break;
}
}
可能就是我写它的方式。
编辑:如果您正在寻找干净的代码,您可以这样做:
<强> bosshandler.php:强>
function doSomethingWithBoss($value){
switch($value){
case 1:
return "boss equals 1";
break;
case 2:
return "boss equals 2";
break;
}
}
您的脚本页面:
include bosshandler.php;
$boss = new array(1, 2, 3, 4);
foreach($boss as $value){
echo doSomethingWithBoss($value);
}
好的,再试一次。我想我现在看到你在问什么:
您想要分配一个变量,以便您的数组匹配。如果您在$boss[1]
,您希望为$boss_holder['1']
分配内容,那是正确的吗?如果是这样,你不想使用foreach数组,因为它对于数组及其解析顺序有些不可预测。
相反,让我们使用for()
循环,如下所示:
$boss = new Array(1, 2, 3, 4);
$boss_holder = new Array(); //empty
$length = count($boss);
//iterate through boss
for($i = 1; $i < $length; $i++){
if($boss[$i] == 1){
$boss_holder[$i] = $tick;
}elseif($boss[$i] == 2){
$boss_holder[$i] = $cross;
}
}
再次修改以反映您的完整代码:
使用数组。重构您的代码,以便它使用数组。如果由于某种原因你完全不能或不会使用数组,你可以使用PHP的variable variables来完成你正在寻找的东西。它看起来像这样:
var $boss1_holder, $boss2_holder, $boss3_holder;
for($i=1; $i> 4; $i++){
$variableName = 'boss' . $i . '_holder';
$$variableName = 1;
}
echo $boss2_holder; //returns 2
答案 1 :(得分:1)
看看这段代码:
$bosses = new array(1, 2, 1, 1, 2);
$boss_holders = new array()
for ($i = 1; $i <= count($boss); $i++) {
// if bosses[i] equals 1, we make the current boss_holder equal a $tick
if($bosses[i] == 1)
array_push($boss_holders, $tick);
// if the current boss doesn't equal 1 then it must equal 2
// so we add a $cross instead
else
array_push($boss_holders, $cross);
}
请注意:上面的代码未经测试,可能包含错误,因为我的PHP有点(好吧,很多)生锈。
它的作用是创建一个新的老板数组(你可以将这个值设置为你老板的任何数字),以及一个名为$ boss_holders的空数组。然后我们通过一系列的老板,如果当前的Boss等于1,那么我们加上$ tick。否则,当前的Boss等于2,所以我们将添加$ cross。
当for循环运行完毕后,你将拥有一个反映老板价值的boss_holders
数组。如果boss1 = 1,boss2 = 2,boss3 = 1,则数组boss_holders将如下所示:[$tick, $cross, $tick]
,依此类推。例如,当您需要访问boss_holder_2
的值时,您可以通过访问boss_holder
数组来获得该值:boss_holders[2]
。
这有意义吗?这就是你需要的吗?
答案 2 :(得分:0)
也许你可以使用switch。
从第一个例子开始:
if ($i == 0) {
echo "i equals 0";
} elseif ($i == 1) {
echo "i equals 1";
} elseif ($i == 2) {
echo "i equals 2";
}
是一样的:
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
}
但看起来更干净。
答案 3 :(得分:0)
Have you tried using variable variables? The first script will print out the variables created, while the second demonstrates that a variable can be defined or determined by modifying the "if" statement. Maybe this will be helpful for your situation. `
`“; $ I ++; } ?&GT;
<?php
$i=1;
$boss4_holder=true;
while($i<10){
$q="boss".$i."_holder";
if ($$q==true){
echo "true";
}
$i++;
}
?>