$time = date("G");
$location = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
$countrycode = $location['geoplugin_countryCode'];
switch($time){
case 0:
$conn->query("UPDATE link SET visits_at_00 = visits_at_00 + 1 WHERE id = '$id'");
break;
case 1:
$conn->query("UPDATE link SET visits_at_01 = visits_at_01 + 1 WHERE id = '$id'");
break;
case 2:
$conn->query("UPDATE link SET visits_at_02 = visits_at_02 + 1 WHERE id = '$id'");
break;
case 3:
$conn->query("UPDATE link SET visits_at_03 = visits_at_03 + 1 WHERE id = '$id'");
break;
case 4:
$conn->query("UPDATE link SET visits_at_04 = visits_at_04 + 1 WHERE id = '$id'");
break;
case 5:
$conn->query("UPDATE link SET visits_at_05 = visits_at_05 + 1 WHERE id = '$id'");
break;
case 6:
$conn->query("UPDATE link SET visits_at_06 = visits_at_06 + 1 WHERE id = '$id'");
break;
case 7:
$conn->query("UPDATE link SET visits_at_07 = visits_at_07 + 1 WHERE id = '$id'");
break;
case 8:
$conn->query("UPDATE link SET visits_at_08 = visits_at_08 + 1 WHERE id = '$id'");
break;
case 9:
$conn->query("UPDATE link SET visits_at_09 = visits_at_09 + 1 WHERE id = '$id'");
break;
case 10:
$conn->query("UPDATE link SET visits_at_10 = visits_at_10 + 1 WHERE id = '$id'");
break;
case 11:
$conn->query("UPDATE link SET visits_at_11 = visits_at_11 + 1 WHERE id = '$id'");
break;
case 12:
$conn->query("UPDATE link SET visits_at_12 = visits_at_12 + 1 WHERE id = '$id'");
break;
case 13:
$conn->query("UPDATE link SET visits_at_13 = visits_at_13 + 1 WHERE id = '$id'");
break;
case 14:
$conn->query("UPDATE link SET visits_at_14 = visits_at_14 + 1 WHERE id = '$id'");
break;
case 15:
$conn->query("UPDATE link SET visits_at_15 = visits_at_15 + 1 WHERE id = '$id'");
break;
case 16:
$conn->query("UPDATE link SET visits_at_16 = visits_at_16 + 1 WHERE id = '$id'");
break;
case 17:
$conn->query("UPDATE link SET visits_at_17 = visits_at_17 + 1 WHERE id = '$id'");
break;
case 18:
$conn->query("UPDATE link SET visits_at_18 = visits_at_18 + 1 WHERE id = '$id'");
break;
case 19:
$conn->query("UPDATE link SET visits_at_19 = visits_at_19 + 1 WHERE id = '$id'");
break;
case 20:
$conn->query("UPDATE link SET visits_at_20 = visits_at_20 + 1 WHERE id = '$id'");
break;
case 21:
$conn->query("UPDATE link SET visits_at_21 = visits_at_21 + 1 WHERE id = '$id'");
break;
case 22:
$conn->query("UPDATE link SET visits_at_22 = visits_at_22 + 1 WHERE id = '$id'");
break;
case 23:
$conn->query("UPDATE link SET visits_at_23 = visits_at_23 + 1 WHERE id = '$id'");
break;
}
switch($countrycode){
case "US":
$conn->query("UPDATE link SET visits_from_us = visits_from_us + 1 WHERE id = '$id'");
break;
case "DE":
$conn->query("UPDATE link SET visits_from_de = visits_from_de + 1 WHERE id = '$id'");
break;
case "FR":
$conn->query("UPDATE link SET visits_from_fr = visits_from_fr + 1 WHERE id = '$id'");
break;
case "MX":
$conn->query("UPDATE link SET visits_from_mx = visits_from_mx + 1 WHERE id = '$id'");
break;
case "TR":
$conn->query("UPDATE link SET visits_from_tr = visits_from_tr + 1 WHERE id = '$id'");
break;
case "IT":
$conn->query("UPDATE link SET visits_from_it = visits_from_it + 1 WHERE id = '$id'");
break;
case "RU":
$conn->query("UPDATE link SET visits_from_ru = visits_from_ru + 1 WHERE id = '$id'");
break;
case "ES":
$conn->query("UPDATE link SET visits_from_es = visits_from_es + 1 WHERE id = '$id'");
break;
case "CN":
$conn->query("UPDATE link SET visits_from_cn = visits_from_cn+ 1 WHERE id = '$id'");
break;
case "AU":
$conn->query("UPDATE link SET visits_from_au = visits_from_au + 1 WHERE id = '$id'");
break;
case "IN":
$conn->query("UPDATE link SET visits_from_in = visits_from_in + 1 WHERE id = '$id'");
break;
case "EN":
$conn->query("UPDATE link SET visits_from_en = visits_from_en + 1 WHERE id = '$id'");
break;
case "CA":
$conn->query("UPDATE link SET visits_from_ca = visits_from_ca + 1 WHERE id = '$id'");
break;
case "SA":
$conn->query("UPDATE link SETvisits_from_sa = visits_from_sa + 1 WHERE id = '$id'");
break;
}
首先,我知道这看起来很糟糕,我必须准备语句并验证数据,以及所有这些。无论如何,我想先建立一个基本的内容,我想让这个更短,现在我知道如何制作一个包含相同步骤的多个短脚本,但在这里我不知道从哪里开始,以及如何做。请帮助我获得一些想法和例子。
答案 0 :(得分:1)
无需在您的情况下使用开关或foreach循环,直接向前,
$time = date("G");
$location = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_SERVER['REMOTE_ADDR']));
$countrycode = $location['geoplugin_countryCode'];
$time = sprintf("%02d", $time);
$conn->query("UPDATE link SET visits_at_$time = visits_at_$time + 1 WHERE id = '$id'");
$conn->query("UPDATE link SET visits_from_".strtolower($countrycode)." = visits_from_".strtolower($countrycode)." + 1 WHERE id = '$id'");
我直接用数据库字段本身替换你的相关变量。
它会起作用。
修改强>
正如在 strtolower 的文档中所述,该预定义函数用于将字符串设为小写。
类似的功能是strtoupper
答案 1 :(得分:0)
您可以验证switch
和$time
的值,而不是$countrycode
,然后使用它来生成正确的查询。
以下是它的外观示例。当然我没有运行它,所以它可能无法通过复制和粘贴它。
$time = date("G");
$location = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
$countrycode = $location['geoplugin_countryCode'];
//validation 0<=time<=23
if(0<=$time && $time <= 23) {
//modify $time to format with leading zero for digits.
$index = str_pad($time, 2, '0', STR_PAD_LEFT);
// use it for the query.
$conn->query("UPDATE link SET visits_at_$index = visits_at_$index + 1 WHERE id = '$id'");
}
// array of all country codes.
// I've added only three, and you should add here the rest of valid codes
$availableCountryCodes = array('US', 'DE', 'FR');
//validate if the country code is in the array, which means it's valid
if(in_array($countrycode, $availableCountryCodes)) {
// make it lower case as it's the required format for the query
$lowerCountryCode = strtolower($countrycode);
// use it for the query
$conn->query("UPDATE link SET visits_from_$lowerCountryCode = visits_from_$lowerCountryCode + 1 WHERE id = '$id'");
}
编辑:
正如@okante在评论中指出的那样,可以对其进行优化,使其成为一个UPDATE
查询,因为您需要两次更新同一行。
$time = date("G");
$location = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
$countrycode = $location['geoplugin_countryCode'];
$fields = array();
//validation 0<=time<=23
if(0<=$time && $time <= 23) {
//modify $time to format with leading zero for digits.
$index = str_pad($time, 2, '0', STR_PAD_LEFT);
// add to $fields array for later use
$fields[] = "visits_at_$index = visits_at_$index + 1";
}
// array of all country codes.
// I've added only three, and you should add here the rest of valid codes
$availableCountryCodes = array('US', 'DE', 'FR');
//validate if the country code is in the array, which means it's valid
if(in_array($countrycode, $availableCountryCodes)) {
// make it lower case as it's the required format for the query
$lowerCountryCode = strtolower($countrycode);
// add to $fields array for later use
$fields[] = "visits_from_$lowerCountryCode = visits_from_$lowerCountryCode + 1";
}
// if there's somehting to update
if($fields) {
// concat the update parts with a comma
$concatedFields = implode(', ', $fields);
// and the final query
$conn->query("UPDATE link SET $concatedFields WHERE id = '$id'");
}
答案 2 :(得分:0)
假设您不打算更改代码或重新编写数据库,或使用prepared statements,或处理可能的错误 - 您可以使用变量构建查询。
注意:
使用date('H)获得前导零的小时
使用curly braces({})围绕双引号字符串中的变量
使用strtolower()将字符串从大写转换为小写
使用1 UPDATE
sql语句设置数据库中的值
$hour = date("H");
$location = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
$countrycode = strtolower($location['geoplugin_countryCode']);
$visitsAtColumn = 'visits_at_' . $hour;
$visitsFromColumn = 'visits_from_' . $countrycode;
$sql = "UPDATE link SET {$visitsAtColumn} = {$visitsAtColumn} + 1, {$visitsFromColumn} = {$visitsFromColumn} + 1 WHERE id = '{$id}'";
$conn->query($sql);