Result of 2 queries fetch_assoc in an array

1

The idea is to obtain the values that contain the result of two queries and store them in a single array, since this is treated later and another query is made with it. I put in situation:

$comprobar_reservas_inicio = $sql->ejecutar_consulta(" SELECT id_producto,fecha_inicio,fecha_fin FROM reservas_prueba 
                  WHERE fecha_inicio >= '2016-10-13'" );

$comprobar_reservas_fin = $sql->ejecutar_consulta(" SELECT id_producto,fecha_inicio,fecha_fin FROM reservas_prueba 
                  WHERE fecha_fin <= '2016-11-11'" );

$barcos_no_disponibles = array();

$i = 0;

while($row = mysqli_fetch_assoc($comprobar_reservas_inicio))
                            {
                               $barcos_no_disponibles = $row['id_producto'];

                            }



                            while($row = mysqli_fetch_assoc($comprobar_reservas_fin))
                            {
                               $barcos_no_disponibles = $row['id_producto'];
                            }

            $comprobar_flota = $sql->ejecutar_consulta("SELECT * FROM flota
         WHERE FLOTAID NOT IN ('$barcos_no_disponibles') ");

The variable $i , I want to use it to access the correct position of the new array, which will increase, but, I can not get it to work properly and be stored in a single array.

Thanks in advance.

Edit.Do more details ..

//Las dos variables se pasan por POST,desde un formulario con campos DATE..

$fecha_inicio = $_POST['fecha_inicio'];
$fecha_fin = $_POST['fecha_fin'];

//Mediante una función obtengo los dias existentes entre esos dos

function createDateRangeArray($strDateFrom,$strDateTo = null)
{
    // takes two dates formatted as YYYY-MM-DD and creates an
    // inclusive array of the dates between the from and to dates.
    $aryRange=array();

    $iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2),     substr($strDateFrom,8,2),substr($strDateFrom,0,4));
    $iDateTo=mktime(1,0,0,substr($strDateTo,5,2),     substr($strDateTo,8,2),substr($strDateTo,0,4));

    if ($iDateTo>=$iDateFrom)
    {
        array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry
        while ($iDateFrom<$iDateTo)
        {
            $iDateFrom+=86400; // add 24 hours
            array_push($aryRange,date('Y-m-d',$iDateFrom));
        }
    }
    return $aryRange;
}

//Guardo los valores de las fechas en una cadena

$array_rango_fechas = createDateRangeArray($fecha_inicio,$fecha_fin);

$fechas_a_reservar = implode(',' ,$array_rango_fechas );

//Despues con estas fechas,compruebo los datos de los barcos que esten //reservados en dias presentes en el rango

$comprobar_reservas_inicio = $sql->ejecutar_consulta(" SELECT id_producto FROM reservas_prueba WHERE fecha_inicio IN  ('$fechas_a_reservar') " );

$comprobar_reservas_fin    = $sql->ejecutar_consulta(" SELECT id_producto FROM reservas_prueba WHERE fecha_fin IN  ('$fechas_a_reservar') " );

//y después,gurado los IDs de los barcos YA RESERVADOS,para excluirlos de la //consulta de barcos disponibles

$barcos_no_disponibles = array();

        $i = 0;

            while($row = mysqli_fetch_assoc($comprobar_reservas_inicio))
            {
               $barcos_no_disponibles = $row['id_producto'].",";

            }


            while($row = mysqli_fetch_assoc($comprobar_reservas_fin))
            {
               $barcos_no_disponibles .= $row['id_producto'];
            }


/---COMPROBAR flota con ID (?)
$comprobar_flota = $sql->ejecutar_consulta("SELECT * FROM flota WHERE FLOTAID NOT 
                IN ('$barcos_no_disponibles') ");

            $array_flota = array();
            $i = 0;


            while($row = mysqli_fetch_array($comprobar_flota )) {

                $array_flota[$i]['FLOTAID']         = $row['FLOTAID'];
                $array_flota[$i]['NOMBRE']          = $row['NOMBRE'];
                $array_flota[$i]['CARACTERISTICAS'] = $row['CARACTERISTICAS'];
                $array_flota[$i]['EQUIPAMIENTO']    = $row['EQUIPAMIENTO'];

                $i++;
            }


//por ultimo devuelvo los valores al script php que llama a este
//de esta forma debido a ser una petición AJAX
header("Content-Type: application/json");
echo json_encode($array_flota);
exit;

The script does not just return what I want .. It shows a list of ships available according to the dates specified, but as I said before, it is not stored correctly in a single array, so only the last changes are applied and a part of the data is returned.

    
asked by Víc M.R. 10.10.2016 в 17:32
source

1 answer

1

Use union
SELECT product_id, start_date, end_date FROM test_reserves           WHERE start_date & = = '2016-10-13' union SELECT product_id, start_date, end_date FROM test_reserves           WHERE date_final < = '2016-11-11' "
or just do a query, I understand that you select a range of dates WHERE start_date > = '2016-10-13' OR end_date

answered by 10.10.2016 в 21:24