$ array displaying next result?

1

In my code I made the end of it so that the array made a sequential sequence of my search but the same is not working, can anyone help me please?

<?php
error_reporting (E_ALL & ~ E_NOTICE & ~ E_DEPRECATED);

//chama o arquivo de conexão com o banco

include("connect_db.php");

//consulta mysqli

$query = mysql_query("SELECT numero, orig, dest, eqpt, rota FROM voos ORDER BY RAND() LIMIT 5") or die(mysql_error());

//aqui ele gera looping e cria um array com os campos da consulta

while($array = mysql_fetch_array($query))

{

echo $array['numero'];
?><br />
Origem:  <?php
echo $array['orig'];
?><br />
Destino: <?php
echo $array['dest'];
?><br />
Aeronave: 
<?php
echo $array['eqpt'];
?><br />
Rota: <?php
echo $array['rota'];
?><br /><?php

}
    ?> 
    $orig = $array->dest;
    }
    $dest = $orig;
    } 
    <?php

?>
    
asked by anonymous 29.09.2017 / 19:56

3 answers

1
//consulta mysqli

$query = mysql_query("SELECT numero, orig, dest, eqpt, rota FROM voos ORDER BY RAND() LIMIT 5", $con) or die(mysql_error());

You have missed the connection in the query ... mysql_query ("SELECT ...", $ con)

And that part is really out, useless:

$orig = $array->dest;
}
$dest = $orig;
} 

Running as you want: (may have other ways, I did so to understand step by step)

//aqui ele gera looping e cria um array com os campos da consulta
while($i = mysql_fetch_array($query)) {
    $array[] = $i;
}

//echo "<pre>";
//print_r($array);
$r = 0;

foreach ($array as $i) {

    //print_r($i);

    echo $array[$r]['numero'];
    ?><br />
    Origem:  <?php
    if ($r == 0) { echo $array[$r]['orig']; } else {
        echo $array[$r-1]['dest'];
    }
    ?><br />
    Destino: <?php
    echo $array[$r]['dest'];
    ?><br />
    Aeronave: 
    <?php
    echo $array[$r]['eqpt'];
    ?><br />
    Rota: <?php
    echo $array[$r]['rota'];
    ?><br /><?php

    ++$r;
}

To avoid the same origin and destination: foreach ($ array as $ i) {

foreach ($array as $i) {

//print_r($i);
if ($array[$r-1]['dest'] == $array[$r]['dest']) continue;

echo $array[$r]['numero'];
    
29.09.2017 / 20:17
0

This part of the code is outside of the php tag and there is also more in the code.

$orig = $array->dest;
}
$dest = $orig;
} 
    
29.09.2017 / 20:02
0

What if I listed this way? Problem that I could not adjust, everything.

//aqui ele gera looping e cria um array com os campos da consulta
$array = mysql_fetch_array($query)
foreach($array as $indice => $item)
{
    if(isset($array[$indice + 1])){
        echo $array[$indice]['numero'];
        echo $array[$indice]['orig'];
        echo $array[$indice]['dest'];
        echo $array[$indice]['eqpt'];
        echo $array[$indice]['rota'];
        echo "<br>";
        //Se for o item 2 primeiro inverta a parte de baixo com a de cima
        echo $array[$indice + 1]['numero'];
        echo $array[$indice + 1]['orig'];
        echo $array[$indice + 1]['dest'];
        echo $array[$indice + 1]['eqpt'];
        echo $array[$indice + 1]['rota'];
    
29.09.2017 / 20:30