0

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: enter image description here

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
Mark Bottom
  • 57
  • 1
  • 1
  • 7
  • 1
    You conditionally create those variables, but then you use them in `if` conditions later. Maybe you should use `isset` in them. – El_Vanja Mar 25 '21 at 22:49
  • It seems to me what you really need is an SQL `UNION` - as in `SELECT * from tableA UNION SELECT * from TableB` with appropriate field lists and `WHERE` clauses, and then you'll get all your results at once. – Tangentially Perpendicular Mar 25 '21 at 23:14
  • @TangentiallyPerpendicular I like your suggestion, but how would that be implemented if the queries are different when types are 1 and 8. There has to be at least 2 if statements. – Mark Bottom Mar 25 '21 at 23:50
  • There's not enough information here to answer that. I suggest you ask a new question on that topic, including the table definitions and sample queries. – Tangentially Perpendicular Mar 25 '21 at 23:54

1 Answers1

0

Finally I found a solution. $sql1 is different for type = 1 & type = 8. Since they are executed at different times, they always get the correct result:

if ($type != "%" && $type != "8") {
$typeCondition =  " … = $type ";

$sql1 =  "".
$typeCondition.
"";
} elseif ($type == "8"){
$typeCondition =  " … = $type ";

$sql1 =
$typeCondition.
"";
} else {
$typeCondition = "";
}

$sql = $sql1;

$result = mysqli_query($con, $sql);
$list = array();
while ($row = mysqli_fetch_assoc($result)) {
$list[] = $row;
}

echo json_encode($list);
Mark Bottom
  • 57
  • 1
  • 1
  • 7
  • 1
    I recommend that you use a prepared statement for security/stability. Also, you can more directly use `fetch_all()` https://stackoverflow.com/a/24542282/2943403 – mickmackusa Mar 26 '21 at 02:58