This question is not focused on how to fix or what is undefined index/variable, but on how to write the logic to join both results. Therefore, it is not the same as "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP. If you say otherwise you might as well take the time to read the question again.
I am using MySQL queries to get the list of product of different types (1-8 or all types).
Now, there is a special case when the type = 8. In this case, data should come from a different table. I can't get my logic around on how to implement this. I am trying using array_merge to join both arrays. I need both results to be encoded together for future use.
Update table1 has 23 columns & table2 has 18 columns:
UNIONs (UNION and UNION ALL) require that all the queries being UNION'd have:
The same number of columns in the SELECT clause
The column data type has to match at each position
The challenge here are the types coming at a different time as shown below:
GetProductList.php
if ($type == 8) {
$sql2 = "";
}
if ($type != "8") { // originally $type != "%"
$typeCondition = "...$type";
$sql1 = "".typeCondition;
} else {
$typeCondition = "";
}
if ($sql1) {
$result1 = mysqli_query($con, $sql1);
$list1 = array();
while ($row = mysqli_fetch_assoc($result1)) {
$list1[] = $row;
}
}
if ($sql2) {
$result2 = mysqli_query($con, $sql2);
$list2 = array();
while ($row = mysqli_fetch_assoc($result2)) {
$list2[] = $row;
}
}
if ($sql1 && $sql2) {
$merged_results = array_merge($list1, $list2);
}
echo json_encode($merged_results);
Current output
GetProductList.php?project=1&type=1:
variable: sql2
GetProductList.php?project=1&type=8
variable: sql1