所以我有一个名为bestmatchrule的变量,它在1-25之间返回一个数字,现在我试图使用以下if语句为该数字赋一个名称:
htmlcontent[idgrab] = $('#'+idgrab).html();
问题是它不起作用。它只返回前一个。这是返回值或if语句的问题吗?
另外,如果有缩短它的方法使它看起来更好吗?我更专注于功能,但必须有比这更好的方法吗?
提前致谢! (p.s.对于noob问题抱歉!)
答案 0 :(得分:4)
$a == "1" || "2"
不是有效的声明。它需要$a == "1" || "$a == 2"
。那是你的错误。
就缩短这一点而言,你无能为力。你可以使用in_array()
来缩短某些陈述,但你真的没有任何真正方便的地方。
if ($a == "1" || $a == "2") {
$b="Chesil House";
} elseif ($a == "3" || $a == "4") {
$b="Corfe House";
} elseif ($a == "5" || $a == "6") {
$b="Cranborne House";
} elseif ($a == "7" || $a == "8") {
$b="Dorchester House";
} elseif ($a == "9" || $a == "10") {
$b="Lyme Regis House";
} elseif ($a == "11" || $a == "12") {
$b="Okeford House";
} elseif ($a == "13" || $a == "14") {
$b="Purbeck House";
} elseif ($a == "15" || $a == "16") {
$b="Student Village";
} elseif (in_array($a, array("17", "18","19")) {
$b="Unilet";
} elseif ($a == "20") {
$b="Conel Court";
} elseif ($a == "21" || $a == "22") {
$b="St John's Road";
} elseif (in_array($a, array("23", "24", "25")) {
$b="Private Let";
} else {
echo "Please Try Again";
}
您还可以使用数组来保存您的值,$a
值是关键。然后检查它是否在数组中,如果是,则分配值:
$array = [
1 => "Chesil House",
2 => "Chesil House",
3 => "Corfe House",
4 => "Corfe House",
5 => "Cranborne House",
6 => "Cranborne House",
7 => "Dorchester House",
8 => "Dorchester House",
9 => "Lyme Regis House",
10 => "Lyme Regis House",
11 => "Okeford House",
12 => "Okeford House",
13 => "Purbeck House",
14 => "Purbeck House",
15 => "Student Village",
16 => "Student Village",
17 => "Unilet",
18 => "Unilet",
19 => "Unilet",
20 => "Conel Court",
21 => "St John's Road",
22 => "St John's Road",
23 => "Private Let",
24 => "Private Let",
25 => "Private Let"
];
if (isset($array[$a])) {
$b = $array[$a];
}
else {
echo "Please Try Again";
}
答案 1 :(得分:1)
John Conde的回答可能就是你想要的。另一种可能性是渐进式小于(<)陈述。
$a= (int) $bestmatchrule;
$b='';
if ($a < 3) {
$b="Chesil House";
} elseif ($a < 5) {
$b="Corfe House";
} elseif ($a < 7) {
$b="Cranborne House";
} elseif ($a < 9) {
$b="Dorchester House";
} elseif ($a < 11) {
$b="Lyme Regis House";
} elseif ($a < 13) {
$b="Okeford House";
} elseif ($a < 15) {
$b="Purbeck House";
} elseif ($a < 17) {
$b="Student Village";
} elseif ($a < 20) {
$b="Unilet";
} elseif ($a < 21) {
$b="Conel Court";
} elseif ($a < 23) {
$b="St John's Road";
} elseif ($a < 26) {
$b="Private Let";
} else {
"Please Try Again";
}
switch语句也是一种选择,但我认为它不会更好。