Pagina 1 di 3 1 2 3 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 26
  1. #1

    Aiuto con array.

    Salve. Mi sono creato questa funzione per ricavare il path di una categoria passato il suo id:

    codice:
    function path($id)
            {
    	   $query = "SELECT id,nome,padre FROM catalogo_categorie WHERE id = '$id'";
    	   $result = mysql_query($query);
    	   $row = mysql_fetch_array($result);
    	   if ($row['padre'] != '0')
    	     {
    		echo $row['nome'].'
    ';
    		path($row['padre']);
    	     }
            }
    Il problema è che non riesco a far apparire i risultati nella forma classica di un path! (home/cat1/subcat1/subsubcat1/ecc.)
    Come faccio?
    Da tenere presente che la funzione estrae per prima la cat attuale e poi le cat del livello superiore. Per chiarire uso l'esempio di prima:

    ecc.
    subsubcat1
    subcat1
    cat1

    Questo è l'output se stampo i risultati.
    Qualcuno mi può aiutare? Grazie!
    Io credo si debba utilizzare un array ma non so come fare!
    Quello che vorrei fare è memorizzare i risultati in un array e poi stamparli.
    eCommerceRS.NET - Commerciante, vendi on-line!
    Il mio nick è mircov e non mirco!!!

  2. #2
    Utente di HTML.it L'avatar di Pasco
    Registrato dal
    Apr 2002
    Messaggi
    1,559
    aggiungi i valori in un array in questo modo:

    codice:
    $GLOBALS['path'][] = $row['nome'];
    path($row['padre']);
    poi all'esterno della funzioni cicli l'array con foreach()

    codice:
    settype($string,'string');
    
    foreach($path as $dir):
    
    	$string .= $dir . '/' ;
    
    endforeach;
    
    echo $string;
    anche se non è la miglior soluzione per fare quello che volevi.
    PyFanatics

  3. #3
    E quale sarebbe la migliore? Io in un esempio in rete ho visto che veniva utilizzato questo codice:

    codice:
    // $node is the name of the node we want the path of
    function get_path($node) {
       // look up the parent of this node
       $result = mysql_query('SELECT parent FROM tree '.
                              'WHERE title="'.$node.'";');
       $row = mysql_fetch_array($result);
    
       // save the path in this array
       $path = array();
    
       // only continue if this $node isn't the root node
       // (that's the node with no parent)
       if ($row['parent']!='') {
           // the last part of the path to $node, is the name
           // of the parent of $node
           $path[] = $row['parent'];
    
           // we should add the path to the parent of this node
           // to the path
           $path = array_merge(get_path($row['parent']), $path);
       }
    
       // return the path
       return $path;
    }
    La ricorsione l'ho imparata leggendo proprio questo articolo. Il problema è che utilizzando la funzione proposta l'output è "Array" e non ho capito perchè. Ho provato anche ricreando la tabella dell'esempio ma niente lo stesso. Ora vi posto pure il codice dell'esempio della tabella così se qualcuno vuole provare a trovare l'errore...

    codice:
    #
    # Struttura della tabella `prova`
    #
    
    CREATE TABLE `prova` (
      `parent` varchar(255) default NULL,
      `title` varchar(255) default NULL
    ) TYPE=MyISAM;
    
    #
    # Dump dei dati per la tabella `prova`
    #
    
    INSERT INTO `prova` VALUES (NULL, 'food');
    INSERT INTO `prova` VALUES ('food', 'fruit');
    INSERT INTO `prova` VALUES ('fruit', 'green');
    INSERT INTO `prova` VALUES ('green', 'pear');
    INSERT INTO `prova` VALUES ('fruit', 'red');
    INSERT INTO `prova` VALUES ('red', 'cherry');
    INSERT INTO `prova` VALUES ('fruit', 'yellow');
    INSERT INTO `prova` VALUES ('yellow', 'banana');
    INSERT INTO `prova` VALUES ('food', 'meat');
    INSERT INTO `prova` VALUES ('meat', 'beef');
    INSERT INTO `prova` VALUES ('meat', 'pork');
    Io onestamente non sono riuscito a trovare l'errore. Spero che qualcuno mi possa aiutare! Grazie ancora!
    eCommerceRS.NET - Commerciante, vendi on-line!
    Il mio nick è mircov e non mirco!!!

  4. #4

    L'ho provata ma non funziona!

    Pasco, ho provato la soluzione che mi hai proposto ma non funziona!

    La funzione l'ho modificata in questo modo:

    codice:
    function path($id)
            {
    	   $query = "SELECT id,nome,padre FROM catalogo_categorie WHERE id = '$id'";
    	   $result = mysql_query($query) or die(mysql_error());
    	   $row = mysql_fetch_array($result) or die('errore su mysql_fetch_array');
    	   if ($row['padre'] != '0')
    	     {
    		$GLOBALS['path'][] = $row['nome'];
    		path($row['padre']);
    	     }
    	  }
    Credo che si questo che avrei dovuto fare.
    Nella pagina dove devo mostrare il path ho poi inserito il seguente codice:

    codice:
    settype($string,'string');
    foreach($path as $dir):
    $string .= $dir . '/' ;
    endforeach;
    echo $string;
    L'errore che mi mostra è questo:

    Warning: Invalid argument supplied for foreach() in f:\web\www.crespigioielli.it2\default.php on line 156

    Io onestamente non so come risolvere.
    eCommerceRS.NET - Commerciante, vendi on-line!
    Il mio nick è mircov e non mirco!!!

  5. #5
    Utente di HTML.it L'avatar di Pasco
    Registrato dal
    Apr 2002
    Messaggi
    1,559
    quell'errore è dovuto al fatto che la variabile array $path non esiste.

    la funzione path() è stata invocata nello script?
    PyFanatics

  6. #6
    Cioè? Nella pagina dove deve comparire il path io devo invocare la funzione path passando l'id e poi avviene tutto il resto? Mi potresti fare un esempio per favore?
    eCommerceRS.NET - Commerciante, vendi on-line!
    Il mio nick è mircov e non mirco!!!

  7. #7
    Utente di HTML.it L'avatar di badaze
    Registrato dal
    Jun 2002
    residenza
    Lyon
    Messaggi
    5,372
    A partire dai dati forniti propongo questo.

    codice:
    <? 
    function retrieveTree($start,&$array,$str="") {
     if ($start == null) {
      $clause = "parent is null";
     } else {
      $clause = "parent = '$start'";
     } //  if ($start == null)
     $query = "select title from prova where $clause order by parent, title";
     $res   = mysql_query($query);
     while (list($title) = mysql_fetch_array($res)) {
      retrieveTree($title,$array,$str."/".$title);
    	$inter = $str."/".$title;
      if (! array_begins_with($inter,$array)) { 
    	 $array[]  = $inter;
      } // if (! array_begins_with($inter,$array))
     } // while (list($title) = mysql_fetch_array($res))
    } // function retrieveTree($start,$level)
    
    function array_begins_with($string,$array) {
     foreach ($array as $value) {
      $equals = strstr($value, $string);
    	if (!empty($equals)) {
       return true;
    	} // if (!empty($equals))
     } // foreach ($array as $value)
     return false;
    } // array_begins_with($string,$array)
    
    $arr = array();
    retrieveTree(null,$arr);
    print_r($arr);
    ?>
    riempie un array ($arr) che contiene tutti i livelli più bassi ovvero pear, cherry, banana, beef e pork.

  8. #8
    OK Badaze, ma come la uso? Scusate ragazzi ma mi sto incasinando!!!
    eCommerceRS.NET - Commerciante, vendi on-line!
    Il mio nick è mircov e non mirco!!!

  9. #9
    Utente di HTML.it L'avatar di badaze
    Registrato dal
    Jun 2002
    residenza
    Lyon
    Messaggi
    5,372
    Qual'è la struttura della tua tabella ?

  10. #10
    Come quella dell'esempio solo che uso gli id al posto dei nomi.
    eCommerceRS.NET - Commerciante, vendi on-line!
    Il mio nick è mircov e non mirco!!!

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 © 2026 vBulletin Solutions, Inc. All rights reserved.