$array exposing next result?

Asked

Viewed 62 times

1

In my code I did at the end of it so that the array did a logical and sequential sequence of my search but it is not working, someone can 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

?>

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());

Failed to put the connection in the query... mysql_query("SELECT...", $con)

And that part really is out, useless:

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

Working as you wish: (they may have other forms, I did so to understand the 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'];
  • $orig = $array->dest; } $dest = $orig; } .

  • But that doesn’t make any sense... What you want to see as a result on your screen ?

  • I’m listing some flights, at least it’s my idea, I’d like it to be like this: Flight Number: Origin: XXXX Destination: XXXX And the second listing would be .

  • Jeez ! That’s not how you do so guy... You’re looking from the bank, with RAND still, it’s different from that !

  • I answered from below.

  • Sorry, I can’t see all this now... But basically it would be like this: 1 - Generates an entire array in a variable, instead of printing it while query 2 - With foreach, make the impression, but at source you pull the previous array of the 2nd result forward Obs: you will have to do an if because the 1st result will not pull the origin of the previous array

  • Thank you very much Raoni but I do not know it yet, I continue studying ms is complicated.

  • I posted working as you want... but there may be other ways... The way I did following a simpler example to understand each other...

  • Raoni I put it to test but still it is not coming out in a logical sequence, it would be something in my query ? or SELECT ?

  • You can see here the result: http://www.latamvirtual.net/core/cria_voo.php

  • That’s right! ?

  • If so, and you’re ok, don’t forget to mark as solved in the reply, so help the next !

  • Raoni I’m only trying one thing, the second result is bringing the origin and destiny equal, how can I fix it please ?

  • You copied the code and pasted ? If yes, there is no problem ! Only if the previous destination matches the next destination ! Then you would have to have another if, to skip the registration, since you would not have the flight.

  • Even, you have empty record in your bank... delete it...

  • I’ll take a look here and enter more records to see what happens.

  • Raoni apologies again come here, was looking at the code and comes a few moments he copies the results for example: TAM3002 Origin: SBSP Destination: SBSP Aircraft: A320 Route: DCT SANBO UZ64 NEGUS DCT TAM3000 Origin: SBSP Destination: SBSP Aircraft: A319/ Route: DCT SANBO UZ64 NEGUS DCT Since in the Database these flights are different records.

  • By the code I gave you there is no way ! I tested ! It pulls what is in each regosto, but replaces the origin from the 2nd display !

Show 13 more comments

0

What if I listed it this way ? Problem that this way 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'];

0

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.