用php避免重复的行

时间:2015-12-20 01:33:16

标签: php

我正在尝试进行dico检查。因此,当用户写一个单词时,代码将找到第一个字母并从对应表中检查。我把字典分成26个表。 代码运行但代码很糟糕,重复太多。 我知道有一些方法可以减少所有代码。 那么......有什么建议吗?

<?php
$lng=strlen($word);$cr=0;
$f1='<div class="fOne">Votre mot : '.$word.' ( <b>'.$lng.'</b> Lettres )';
$f2='<div class="fTwo">Votre mot : '.$word.' ( <b>'.$lng.'</b> Lettres )';
$f3='<div class="fThree">********** ( '.$lng.' Lettres )';
     if ($lng>0) 
        {
         include "../config.php";
if($lng==10) {
if (mysql_num_rows(mysql_query("SELECT mot FROM dix WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>';
else {echo $f2.' est incorrect !<br>';$cr=1;}
} else {
         switch ($word[0])
            {
             case 'A':if (mysql_num_rows(mysql_query("SELECT mot FROM adiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'B':if (mysql_num_rows(mysql_query("SELECT mot FROM bdiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'C':if (mysql_num_rows(mysql_query("SELECT mot FROM cdiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'D':if (mysql_num_rows(mysql_query("SELECT mot FROM ddiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'E':if (mysql_num_rows(mysql_query("SELECT mot FROM ediki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'F':if (mysql_num_rows(mysql_query("SELECT mot FROM fdiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'G':if (mysql_num_rows(mysql_query("SELECT mot FROM gdiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'H':if (mysql_num_rows(mysql_query("SELECT mot FROM hdiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'I':if (mysql_num_rows(mysql_query("SELECT mot FROM idiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'J':if (mysql_num_rows(mysql_query("SELECT mot FROM jdiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'K':if (mysql_num_rows(mysql_query("SELECT mot FROM kdiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'L':if (mysql_num_rows(mysql_query("SELECT mot FROM ldiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'M':if (mysql_num_rows(mysql_query("SELECT mot FROM mdiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'N':if (mysql_num_rows(mysql_query("SELECT mot FROM ndiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'O':if (mysql_num_rows(mysql_query("SELECT mot FROM odiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'P':if (mysql_num_rows(mysql_query("SELECT mot FROM pdiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'Q':if (mysql_num_rows(mysql_query("SELECT mot FROM qdiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'R':if (mysql_num_rows(mysql_query("SELECT mot FROM rdiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'S':if (mysql_num_rows(mysql_query("SELECT mot FROM sdiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'T':if (mysql_num_rows(mysql_query("SELECT mot FROM tdiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'U':if (mysql_num_rows(mysql_query("SELECT mot FROM udiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'V':if (mysql_num_rows(mysql_query("SELECT mot FROM vdiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'W':if (mysql_num_rows(mysql_query("SELECT mot FROM wdiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'X':if (mysql_num_rows(mysql_query("SELECT mot FROM xdiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'Y':if (mysql_num_rows(mysql_query("SELECT mot FROM ydiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
             case 'Z':if (mysql_num_rows(mysql_query("SELECT mot FROM zdiki WHERE mot='$word'",$conn))>0) echo $f1.' est correct ! <br>'; else {echo $f2.' est incorrect !<br>';$cr=1;} break;
            }
                        }
        }
    else {$cr=1;echo $f3.' - Pas de mot !';}
 ?>

0 个答案:

没有答案