I’m not a that good person in PHP but this code I created for API, How can I make this code cleaner and more readable?
<?php
$connection = mysqli_connect("...", "...", "...", "...");
$countries_update_time = $_POST["countries_update_time"];
$cities_update_time = $_POST["cities_update_time"];
$neighborhoods_update_time = $_POST["neighborhoods_update_time"];
$categories_update_time = $_POST["categories_update_time"];
$subcategories_update_time = $_POST["subcategories_update_time"];
$frequently_asked_question_update_time = $_POST["frequently_asked_question_update_time"];
$tips_update_time = $_POST["tips_update_time"];
$currencies_update_time = $_POST["currencies_update_time"];
$countries = $cities = $neighborhoods = $categories = $subcategories = $frequently_asked_question = $tips = $currencies = [];
if (isset($countries_update_time, $cities_update_time, $neighborhoods_update_time, $categories_update_time, $subcategories_update_time, $frequently_asked_question_update_time, $tips_update_time, $currencies_update_time))
{
$update_time_of_tables_result = mysqli_query($connection, "SELECT * FROM update_time_of_tables");
if ($update_time_of_tables_result)
{
while ($i = mysqli_fetch_assoc($update_time_of_tables_result))
{
switch ($i["table_name"])
{
case "countries":
if ($countries_update_time != $i["update_time"])
{
$countries_result = mysqli_query($connection, "SELECT * FROM countries");
if ($countries_result)
{
while ($i = mysqli_fetch_assoc($countries_result))
{
$countries[] = $i;
}
}
}
else
{
$countries = null;
}
break;
case "cities":
if ($cities_update_time != $i["update_time"])
{
$cities_result = mysqli_query($connection, "SELECT * FROM cities");
if ($cities_result)
{
while ($i = mysqli_fetch_assoc($cities_result))
{
$cities[] = $i;
}
}
}
else
{
$cities = null;
}
break;
case "neighborhoods":
if ($neighborhoods_update_time != $i["update_time"])
{
$neighborhoods_result = mysqli_query($connection, "SELECT * FROM neighborhoods");
if ($neighborhoods_result)
{
while ($i = mysqli_fetch_assoc($neighborhoods_result))
{
$neighborhoods[] = $i;
}
}
}
else
{
$neighborhoods = null;
}
break;
case "categories":
if ($categories_update_time != $i["update_time"])
{
$categories_result = mysqli_query($connection, "SELECT * FROM categories");
if ($categories_result)
{
while ($i = mysqli_fetch_assoc($categories_result))
{
$categories[] = $i;
}
}
}
else
{
$categories = null;
}
break;
case "subcategories":
if ($subcategories_update_time != $i["update_time"])
{
$subcategories_result = mysqli_query($connection, "SELECT * FROM subcategories");
if ($subcategories_result)
{
while ($i = mysqli_fetch_assoc($subcategories_result))
{
$subcategories[] = $i;
}
}
}
else
{
$subcategories = null;
}
break;
case "frequently_asked_question":
if ($frequently_asked_question_update_time != $i["update_time"])
{
$frequently_asked_question_result = mysqli_query($connection, "SELECT * FROM frequently_asked_question");
if ($frequently_asked_question_result)
{
while ($i = mysqli_fetch_assoc($frequently_asked_question_result))
{
$frequently_asked_question[] = $i;
}
}
}
else
{
$frequently_asked_question = null;
}
break;
case "tips":
if ($tips_update_time != $i["update_time"])
{
$tips_result = mysqli_query($connection, "SELECT * FROM tips");
if ($tips_result)
{
while ($i = mysqli_fetch_assoc($tips_result))
{
$tips[] = $i;
}
}
}
else
{
$tips = null;
}
break;
case "currencies":
if ($currencies_update_time != $i["update_time"])
{
$currencies_result = mysqli_query($connection, "SELECT * FROM currencies");
if ($currencies_result)
{
while ($i = mysqli_fetch_assoc($currencies_result))
{
$currencies[] = $i;
}
}
}
else
{
$currencies = null;
}
break;
}
}
echo json_encode(["countries" => $countries, "cities" => $cities, "neighborhoods" => $neighborhoods, "categories" => $categories, "subcategories" => $subcategories, "frequently_asked_question" => $frequently_asked_question, "tips" => $tips, "currencies" => $currencies]);
}
}
?>
Simply I want to compare the time I send it via Java and Swift with the time in the server, If was the same I’ll return a null value because I store data locally in Android and IOS………………………
>Solution :
Whenever you are doing "pretty much the same thing" multiple times, see what parts are "static", and in which parts values/variable names need to be "dynamic". Variable variables are an ugly construct, so use arrays instead.
$countries_update_time, $cities_update_time, … – ugly to access. Use an array instead, using the table_name
you got in your database as key:
$update_times = [
'countries' => ...,
'cities' => ...,
...
];
Same goes for the arrays, that will hold your data – instead of multiple arrays with different names, use one array, and use the table name as key in there. PHP does not require you to create sub-arrays before the first value assignment, so $result = [];
will do.
Now all your switch cases can boil down to one single block of code. (That is assuming, there are no table_name
values in your database, for which you need to do nothing – in that case, you will have to specifically exclude those.)
if ($update_times[$i["table_name"]] != $i["update_time"])
{
$query_result = mysqli_query($connection, "SELECT * FROM " . $i["table_name"]);
if ($query_result)
{
while ($row = mysqli_fetch_assoc($query_result))
{
$result[$i["table_name"]][] = $row;
}
}
}
else
{
$result[$i["table_name"]] = null;
}
You need to use a different variable name than $i
for the fetch result in your inner while loop btw., otherwise you will be overwriting that of the outer loop at this point.
(And inserting stuff into database queries directly, like "SELECT * FROM " . $i["table_name"]
, is of course ugly – apply proper escaping at least.)
And then to build your JSON at the end, you simply access the array elements, instead of single variables:
echo json_encode(["countries" => $result['countries'], "cities" => $result['cities'], ...]);
Or echo json_encode($result);
directly, because $result already has that same structure, the data is collected in sub-arrays under the respective table name as key.