Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    Invertire l'ordine di un array

    Salve. Ho creato una funzione ricorsiva che dato un id risale l'albero delle categorie ricreando il path.
    Il problema è che dovrei invertire l'ordine di stampa. Ho provato ad usare array_reverse() ma non funziona. Vi posto il codice della funzione e quello per far funzionare il tutto. Grazie mille! Ciao!
    Codice PHP:
    function Path($id)
            {
               global 
    $path;
               
    $QueryPath "SELECT id,name,parent FROM catalogo_categories WHERE id='$id'";
               
    $ResultPath mysql_query($QueryPath);
               
    $RowPath mysql_fetch_array($ResultPath);
               
    $path[$RowPath['name']] = $RowPath['id'];
               if (
    $RowPath['parent'] != 0)
                 {
                    
                    
    Path ($RowPath['parent']);
                 }
               return 
    array_reverse($path);
             } 
    Questo è cil codice che fa funzionare il tutto
    Codice PHP:
    include('functions/Path.function.php');
         
    $path = array();
         
    Path($_GET['cat']);
         
    array_reverse($path);
         while (list(
    $name$id) = each ($path))
              {
                 echo 
    '[url="default.php?module=catalogo&cat=' $id '"]' $name '[/url] / ';
              } 

  2. #2
    Problema risolto. Bastava mettere l'inserimento dei dato nell'array dopo l'esecuzione della ricorsione. Posto la funzione corretta:

    Codice PHP:
    function Path($id)
            {
               global 
    $path;
               
    $QueryPath "SELECT id,name,parent FROM catalogo_categories WHERE id='$id'";
               
    $ResultPath mysql_query($QueryPath);
               
    $RowPath mysql_fetch_array($ResultPath);
               if (
    $RowPath['parent'] != 0)
                 {
                    
                    
    Path ($RowPath['parent']);
                 }
               
    $path[$RowPath['name']] = $RowPath['id'];
               return 
    array_reverse($path);
             } 

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.