我正在创建一个页面,您可以在其中保存对足球比赛的预测。 每周有9场比赛,用户可以通过表格发布他的预测。 此表单的结果将产生以下数组:
Array ( [week] => 12 [game_id_1] => 28 [game_1_home] => 2 [game_1_away] => 2 [game_id_2] => 29 [game_2_home] => 2 [game_2_away] => 1 [game_id_3] => 31 [game_3_home] => 4 [game_3_away] => 0 [game_id_4] => 30 [game_4_home] => 2 [game_4_away] => 0 [game_id_5] => 32 [game_5_home] => 0 [game_5_away] => 2 [game_id_6] => 33 [game_6_home] => 1 [game_6_away] => 0 [game_id_7] => 35 [game_7_home] => 1 [game_7_away] => 1 [game_id_8] => 34 [game_8_home] => 2 [game_8_away] => 4 [game_id_9] => 36 [game_9_home] => 3 [game_9_away] => 0 )
我已经为第一场比赛写了一个验证部分,但是我认为这可以更轻松地复制8次。 任何人都知道如何做到这一点,如果我能改进这段代码,请给我建议。
$blnOK = true;
$strResult = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (isset($_POST['game_id_1']) && !empty($_POST['game_1_home']) && !empty($_POST['game_1_away']))
//check if the posts are not empty, then put in variables
{
$strGame_id_1 = $_POST['game_id_1'];
$strGame_1_home = $_POST['game_1_home'];
$strGame_1_away = $_POST['game_1_away'];
//Check if strings are numeric
if ((ctype_digit($strGame_1_home)) && (ctype_digit($strGame_1_away)))
{
//Check if prediction already exists in database for user
$strQueryPrediction = "SELECT count(id) amount FROM prediction where player_id = 1 and game_id = $strGame_id_1";
$resultQueryPrediction = mysql_query ($strQueryPrediction);
$intPredictionKnown = mysql_result($resultQueryPrediction, 0);
if ($intPredictionKnown == 0)
{
//NOT KNOWN so INSERT
$blnOK = executeQuery("INSERT INTO prediction (player_id, game_id, predict_home, predict_away) VALUES (1, $strGame_id_1, $strGame_1_home, $strGame_1_away)", $strErrorText, $db);
echo "Your prediction is saved";
}
else
{
//KNOWN so UPDATE
$blnOK = executeQuery("UPDATE prediction SET predict_home = $strGame_1_home, predict_away = $strGame_1_away WHERE player_id = 1 AND game_id = $strGame_id_1", $strErrorText, $db);
echo "Your prediction is updated";
}
}
else
{
$strResult = "Too bad, your prediction could not saved.";
$blnOK = false;
}
}
else
{
echo "Input is empty";
}
}
答案 0 :(得分:0)
您可以并且可以说应该使用循环来保存您多次复制代码。你说有9场比赛,所以你可以做到以下几点:
$blnOK = true;
$strResult = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
for ($i = 1; $i <= 9; $i++)
{
if (isset($_POST['game_id_' . $i]) && !empty($_POST['game_' . $i . '_home']) && !empty($_POST['game_' . $i . '_away']))
//check if the posts are not empty, then put in variables
{
$strGame_id = $_POST['game_id_' . $i];
$strGame_home = $_POST['game_' . $i . '_home'];
$strGame_away = $_POST['game_' . $i . '_away'];
//Check if strings are numeric
if ((ctype_digit($strGame_' . $i . '_home)) && (ctype_digit($strGame_' . $i . '_away)))
{
//Check if prediction already exists in database for user
$strQueryPrediction = "SELECT count(id) amount FROM prediction where player_id = 1 and game_id = $strGame_id";
$resultQueryPrediction = mysql_query ($strQueryPrediction);
$intPredictionKnown = mysql_result($resultQueryPrediction, 0);
if ($intPredictionKnown == 0)
{
//NOT KNOWN so INSERT
$blnOK = executeQuery("INSERT INTO prediction (player_id, game_id, predict_home, predict_away) VALUES (1, $strGame_id, $strGame_home, $strGame_away)", $strErrorText, $db);
echo "Your prediction is saved";
}
else
{
//KNOWN so UPDATE
$blnOK = executeQuery("UPDATE prediction SET predict_home = $strGame_home, predict_away = $strGame_away WHERE player_id = 1 AND game_id = $strGame_id", $strErrorText, $db);
echo "Your prediction is updated";
}
}
else
{
$strResult = "Too bad, your prediction could not saved.";
$blnOK = false;
}
}
else
{
echo "Input is empty";
}
}
}
每当你必须多次做某事时,可以考虑使用循环,或者创建一个可以多次调用的函数。