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

    Definire funzione per evitare di ripetere codice

    Possiedo un codice di questo tipo:

    codice:
    <tr>
    <td class="Menu_1" background="layout/bg_menu_scritte.gif" height="21">
    Homepage</td>
    <td class="Menu_1" background="layout/bg_menu_scritte.gif" height="21">
    [img]layout/freccia_menu.gif[/img]</td>
    </tr>
    <tr>
    <td class="Menu_1" background="layout/bg_menu_scritte.gif" height="21">
    Archivio News</td>
    <td class="Menu_1" background="layout/bg_menu_scritte.gif" height="21">
    [img]layout/freccia_menu.gif[/img]</td>
    </tr>
    <tr>
    <td class="Menu_1" background="layout/bg_menu_scritte.gif" height="21">
    Collabora</td>
    <td class="Menu_1" background="layout/bg_menu_scritte.gif" height="21">
    [img]layout/freccia_menu.gif[/img]</td>
    </tr>
    <tr>
    <td class="Menu_1" background="layout/bg_menu_scritte.gif" height="21">
    Links</td>
    <td class="Menu_1" background="layout/bg_menu_scritte.gif" height="21">
    [img]layout/freccia_menu.gif[/img]</td>
    </tr>
    Come posso definire una funzione f(), in maniera tale da poter sostituire quanto sopra con:

    codice:
    f(Homepage)
    f(Archivio News)
    f(Collabora)
    f(Links)
    ovvero che sia tale per cui:

    codice:
    f(testo)
    =
    codice:
    <tr>
    <td class="Menu_1" background="layout/bg_menu_scritte.gif" height="21">
    testo</td>
    <td class="Menu_1" background="layout/bg_menu_scritte.gif" height="21">
    [img]layout/freccia_menu.gif[/img]</td>
    </tr>

  2. #2
    Utente di HTML.it L'avatar di Laxus
    Registrato dal
    Oct 2010
    Messaggi
    251
    Codice PHP:

    function f($testo)
    {

    echo 
    '<tr>
    <td class="Menu_1" background="layout/bg_menu_scritte.gif" height="21">
    [url="links.php"]'
    $testo .'[/url]</td>
    <td class="Menu_1" background="layout/bg_menu_scritte.gif" height="21">
    [img]layout/freccia_menu.gif[/img]</td>
    </tr>'
    ;

    }


    // usare così:
    f('il mio testo'); 

  3. #3
    Originariamente inviato da Laxus
    Codice PHP:

    function f($testo)
    {

    echo 
    '<tr>
    <td class="Menu_1" background="layout/bg_menu_scritte.gif" height="21">
    [url="links.php"]'
    $testo .'[/url]</td>
    <td class="Menu_1" background="layout/bg_menu_scritte.gif" height="21">
    [img]layout/freccia_menu.gif[/img]</td>
    </tr>'
    ;

    }


    // usare così:
    f('il mio testo'); 
    Ok, grazie, anche se ho notato che così tutti i link finirebbero per puntare alla stessa pagina. La soluzione proposta sotto è giusta?

    Codice PHP:
    function f($testo$link)
    {

    echo 
    '<tr>
    <td class="Menu_1" background="layout/bg_menu_scritte.gif" height="21">
    [url='
    $link .']'$testo .'[/url]</td>
    <td class="Menu_1" background="layout/bg_menu_scritte.gif" height="21">
    [img]layout/freccia_menu.gif[/img]</td>
    </tr>'
    ;

    }


    // usare così:
    f('il mio testo''il mio link'); 

  4. #4
    Utente di HTML.it L'avatar di Laxus
    Registrato dal
    Oct 2010
    Messaggi
    251

  5. #5
    Funziona, grazie. Una domanda stilistica, i commenti mi conviene metterli come HTML appena fuori dallo script, o come PHP dentro?

    Codice PHP:

    <?php function voce($testo$link) {
    echo 
    '<tr>
    <td class="Menu_1" background="layout/bg_menu_scritte.gif" height="21">
    [url='
    $link .']'$testo .'[/url]</td>
    <td class="Menu_1" background="layout/bg_menu_scritte.gif" height="21">
    [img]layout/freccia_menu.gif[/img]</td>
    </tr>'
    ; } ?>
    Codice PHP:
    <?php function voce($testo$link) { //inizio funzione PHP che permette di rendere le voci nei menu
    echo '<tr>
    <td class="Menu_1" background="layout/bg_menu_scritte.gif" height="21">
    [url='
    $link .']'$testo .'[/url]</td>
    <td class="Menu_1" background="layout/bg_menu_scritte.gif" height="21">
    [img]layout/freccia_menu.gif[/img]</td>
    </tr>'
    ; } //fine funzione PHP che permette di rendere le voci nei menu ?>
    Posso dire che nel primo caso risulta chiedendo di visualizzare il sorgente dal browser, nel secondo immagino di no visto che è allinterno di uno script di cui viene restituito soltanto l'output.

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