I have multiple tables in my database and those are updated daily
I'm doing a web app, which compares data from multiple tables.
Data is updated daily, so I really don't know the greatest values and I can't hard code those to a database.
Instead, I should compare data with the corresponding key.
To find the head for my table, I already do a comparison to find different Engine - Transmission -combinations and print one of each:
## GET cars with different motors by type
function findEngines($link, $type, $queryNumber, $data)
{
$query = "
SELECT Engine
, Transmission
, ID
FROM `model_" . $type . "`
GROUP
BY Engine
, Transmission
LIMIT " . $queryNumber . ", 1";
if ($result = mysqli_query($conn, $query)) {
while ($row = mysqli_fetch_array($result)) {
//currently, carData is all, which is needed
if ($data == "engine") {
return $row["Engine"] . " " . $row["Transmission"];
} else if ($data == "carData") {
return $row["ID"];
}
}
}
}
Then, I echo categories for those Engine - Transmission -Combinations:
##Categories
function printCategories($conn, $carID)
{
$query = "
SELECT TechSpecCategoryName
, Spec_Name
, SorterI
FROM `categories_" . $carID . "`
ORDER
BY FIELD(`categories_" . $carID . "`.TechSpecCategoryName, 'header1', 'header2', 'header3', 'header4', 'header5', 'header6', 'header7'), `categories_" . $carID . "`.SorterI";
if ($result = mysqli_query($conn, $query)) {
$techSpecCategory = "";
while ($row = mysqli_fetch_array($result)) {
if ($row['Spec_Name'] == "") {
$spec_name = "-";
} else {
if ($row["TechSpecCategoryName"] != $techSpecCategory) {
echo "<td class='ad-td w-40'><h5>" . $row["TechSpecCategoryName"] . "</h5></td>";
$techSpecCategory = $row["TechSpecCategoryName"];
}
$spec_name = $row["Spec_Name"];
$spec_name = preg_replace("/(\/generaattori)/", " ", $spec_name);
}
echo "<td class='ad-td w-40'>" . $spec_name . "</td>";
}
} else {
echo mysqli_errno($conn) . ": " . mysqli_error($conn) . "<br>";
}
}
Also, I print the data for every column in another function:
Print data
function printData($conn, $itemID, $type, $countResults)
{
if($countResults == 1){
$tableColWidth = "w-60";
}
elseif($countResults == 2){
$tableColWidth = "w-30";
}
elseif ($countResults == 3) {
$tableColWidth = "w-20";
}
else {
$tableColWidth = "w-15";
}
$table = str_replace("-", "_", $itemID);
$queryCarData = "SELECT `categories_" . $type . "`.Item_Name, `categories_" . $type . "`.SorterI, `" . $table . "`.*, COALESCE(SUBSTRING(`" . $table . "`.Item_value, 1, 15), '-') FROM `categories_" . $type . "`
LEFT JOIN `" . $table . "`
ON `" . $table . "`.Item_Name = `categories_" . $type . "`.Item_Name
ORDER BY FIELD(`categories_" . $type . "`.TechSpecCategoryName, 'Kulutus ja päästöt', 'Moottori', 'Vaihteisto', 'Suorituskyky', 'Jarrut', 'Jousitus', 'Ohjaus', 'Mitat ja massat', 'Kuormauskapasiteetti', 'Offroad', 'Renkaat ja vanteet'), `categories_" . $type . "`.SorterI";
if ($result = mysqli_query($conn, $queryCarData)) {
$rowDataNumber = 0;
$techspecCategory = "";
while ($row = mysqli_fetch_array($result)) {
if ($row['Item_Name'] == "") {
$Item_value = "-";
} else {
if ($row["TechSpecCategoryName"] != $techspecCategory) {
echo "<td class='ad-td ".$tableColWidth."'>" . "<br>" . "</td>";
$Item_value = " ";
$techspecCategory = $row["TechSpecCategoryName"];
$rowDataNumber++;
}
$Item_value = $row["Item_value"];
}
echo "<td class='ad-td ".$tableColWidth."'>" . $Item_value . "</td>";
$rowDataNumber++;
}
} else {
echo mysqli_errno($conn) . ": " . mysqli_error($conn) . "<br>";
}
}
What I'd like to archieve: while i'm echoing values, I'd like to echo them with greatest corresponding value, like this:
echo "<td class='ad-td ".$tableColWidth."'>" . $Item_value ." / ". $greatest_value ."</td>";
I'm aware of MySQL's GREATEST()
function, which looks like right for my problem, but I couldn't figure out, how to use it in a situation as mine is.