PHP funzione ricorsiva per ottenere breadcrumb
-
27-09-2019 - |
Domanda
trascorso un paio d'ore su questo e bisogno di qualche aiuto di un esperto.
Ho una tabella come questa:
[id] [name] [parent_id]
1 fruits 0
2 orange 1
3 lemon 2
4 steak 0
Quando vado al limone, voglio il breadcrumb per essere come:
Home> Frutta> Orange> Lemon
E limone non essere un collegamento, ma il resto di essere un collegamento.
Qualche suggerimento?
Il migliore che ho trovato è questo, ma fa tutto in un collegamento.
function createPath($id, $category_tbl) {
$s = "SELECT * FROM ".$category_tbl." WHERE ID = $id";
$r = mysql_query($s);
$row = mysql_fetch_array($r);
if($row['parent_id'] == 0) {
$name = $row['name'];
return "<a href='index.php'>Admin</a> > <a href='index.php?folder_id=$id'>".$name."</a> > ";
} else {
$name = $row['name'];
return createPath($row['parent_id'],$category_tbl). " <a href='index.php?folder_id=$id'>".$name."</a> >";
}
}
Risposta seguito da Erwin mi ha dato quello che mi serve per farlo funzionare.
function createPath($id, $category_tbl, $except = null) {
$s = "SELECT * FROM ".$category_tbl." WHERE ID = $id";
$r = mysql_query($s);
$row = mysql_fetch_array($r);
if($row['parent_id'] == 0) {
$name = $row['name'];
if(!empty($except) && $except == $row['id']) {
return "<a href='index.php'>Admin</a> » ".$name."";
}
return "<a href='index.php'>Admin</a> » <a href='index.php?folder_id=$id'>".$name."</a> » ";
} else {
if(!empty($except) && $except == $row['id']) {
$name = $row['name'];
return createPath($row['parent_id'],$category_tbl, false). " $name";
}
$name = $row['name'];
return createPath($row['parent_id'],$category_tbl, false). " <a href='index.php?folder_id=$id'>".$name."</a> »";
}
}
Soluzione
aggiungere un terzo parametro, che sarà il nome del link che presumibilmente non sarà reso in un un tag
function createPath($id, $category_tbl, $except = null) {
$s = "SELECT * FROM ".$category_tbl." WHERE ID = $id";
$r = mysql_query($s);
$row = mysql_fetch_array($r);
if($row['parent_id'] == 0) {
$name = $row['name'];
return "<a href='index.php'>Admin</a> > <a href='index.php?folder_id=$id'>".$name."</a> > ";
} else {
$name = $row['name'];
if(!empty($except) && $except == $name)
return createPath($row['parent_id'],$category_tbl, $except)." ".$name;
}
return createPath($row['parent_id'],$category_tbl, $except). " <a href='index.php?folder_id=$id'>".$name."</a> >";
}
}
Altri suggerimenti
Non che il codice ottenere i risultati indietro Lemon > Orange > Fruits > Home
? Come suggerito joni, avevo messo i risultati in un array e poi costruire la stringa di output.
Se si dispone di altre informazioni nel database di cui avete bisogno, come ad esempio un url oltre folder_id=$id
è possibile memorizzare come
$breadcrumb_items = array(
0 => array( 'id' => '3',
'title' => 'Lemon',
'url' => 'LemonURL'
),
1 => array( 'id' => '2',
'title' => 'Orange',
'url' => 'OrangeURL'
),
2 => array( 'id' => '1',
'title' => 'Fruit',
'url' => 'FruitURL'
),
3 => array( 'id' => '0',
'title' => 'Home',
'url' => 'HomeURL'
)
);
Quindi chiamare array_reverse
per fissare l'ordine matrice, e costruire il vostro html. Assicurarsi di impostare un flag per evitare che l'ultimo elemento da essere trasformato in un collegamento.
$targetID = 3; //Lemon
foreach( $breadcrumb_items as $breadcrumb ){
...
if( $breadcrumb['id'] != $targetID ){ //if the id does not match our target id
//add link code
}
...
}
Invece di emettere appena in tempo, crea un array e tampone risultati lì. Dopo dell'array si è riempito da createPath()
è possibile l'uscita breadcrumb da un ciclo for che sa poi che che l'ultimo elemento è (da count()
) e può evitare di fare un collegamento fuori di esso.