<?php
$con = mysqli_connect('localhost','dotrazie_new','catalonia9f532','dotrazie_new');


if(!$con){
    die("couldnt connect".mysqli_error);
}


//$result = mysqli_query($con,"SELECT c.ID, c.Name, c.Customer, c.Technologies, p.ImageType, p.Path FROM customers c LEFT JOIN pictures p ON p.IDCustomer = c.ID");
$result1 = mysqli_query($con,"SELECT * FROM customers c");
$result2 = mysqli_query($con,"SELECT p.Path FROM pictures p");


//$r = array();
//$r2 = array();
//
//$picturesArray = array();
//
//$r["Deskop"] = array();




//if( $result->num_rows>0){
//    while($row = $result->fetch_assoc()){
//        $r[] = $row;
//        if ($row["ID"] == 1) {
//            echo "TRIGGER";
//        }
//    }
//}

$usersList_array =array();
$user_array = array();
$note_array = array();

$desktop_array = array();

$pictures_array = array();

while ($row_users = mysqli_fetch_assoc($result1)) {
    $user_array['ID'] = $row_users['ID'];
    $user_array['Name'] = $row_users['Name'];
    $user_array['Customer'] = $row_users['Customer'];
    $user_array['Technologies'] = $row_users['Technologies'];
    $user_array['Description'] = $row_users['Description'];
    $user_array['MainImage'] = $row_users['MainImage'];

    $user_array['Pictures'] = array();

    $pictures_array['Desktop'] = array();
    $pictures_array['Laptop'] = array();

    $fetch_notes = mysqli_query($con, "SELECT p.Path, p.ImageType FROM pictures p WHERE p.IDCustomer = ".$row_users["ID"]."") or die(mysqli_error($mysqli));

    $fetch_picsDesktop = mysqli_query($con,
        "SELECT p.Path, p.ImageType FROM pictures p 
WHERE p.IDCustomer = ".$row_users["ID"]." 
AND p.ImageType = 'Desktop'") or die(mysqli_error($mysqli));

    $fetch_picsLaptop = mysqli_query($con,
        "SELECT p.Path, p.ImageType FROM pictures p 
WHERE p.IDCustomer = ".$row_users["ID"]." 
AND p.ImageType = 'Laptop'") or die(mysqli_error($mysqli));



//    while ($row_notes = mysqli_fetch_assoc($fetch_notes)) {
//        $note_array['Path']=$row_notes['Path'];
//        $note_array['ImageType']=$row_notes['ImageType'];
//
//        if ($row_notes['ImageType'] == 'Desktop') {
//            $desktop_array['Path']=$row_notes['Path'];
//            //$desktop_array['ImageType']=$row_notes['ImageType'];
//        }
//
//        array_push($pictures_array['Desktop'], $desktop_array);
//
//
//    }


    while ($row_notes = mysqli_fetch_assoc($fetch_picsDesktop)) {
        $note_array['Path']=$row_notes['Path'];
        //$note_array['ImageType']=$row_notes['ImageType'];
        array_push($pictures_array['Desktop'], $note_array);
    }

    while ($row_notes = mysqli_fetch_assoc($fetch_picsLaptop)) {
        $note_array['Path']=$row_notes['Path'];
        //$note_array['ImageType']=$row_notes['ImageType'];
        array_push($pictures_array['Laptop'], $note_array);
    }



    array_push($user_array['Pictures'],$pictures_array);


    array_push($usersList_array,$user_array);
}





$jsonData = json_encode($usersList_array, JSON_PRETTY_PRINT);


echo $jsonData;

echo "<pre>";
echo print_r($jsonData);

echo "<pre>"



//if( $result1->num_rows>0){
//    while($row1 = $result1->fetch_assoc()){
//
//
////if( $result2->num_rows>0) {
////    while ($row2 = $result2->fetch_assoc()) {
////            $r2[] = $row2;
////    }
////}
//
//
//        $result2 = mysqli_query($con,"SELECT p.Path FROM pictures p WHERE p.IDCustomer = ".$row1["ID"]."");
//        //$row =  $result2->fetch_assoc();
//
//        while ($row_notes = mysqli_fetch_assoc($result2)) {
//            $picturesArray['Deskop'] = $row_notes['Path'];
//        }
//
//        $r[] = $row1;
//        //$r["ID"] = $row1["ID"];
//
//            echo "TRIGGER";
//            array_push($r["Desktop"],$picturesArray);
//
//
//        //array_push($arr,$r);
//
//        //array_splice( $original, 3, 0, $inserted ); // splice in at position 3
//        //$r["Name"] = "aaaaa";
//        //$r = array_merge($r,)
//
//        //$r = array_splice( $original, 3, 0, $inserted ); // splice in at position 3
//
//
//    }
//}




?>

