views:

32

answers:

2

Hi everyone, I am having a re-declare issue and I just want to understand why can someone enlighten me.

Here's my code.

 function createAdminPage($dialarg,$topsnamearg,$shortnamearg) {
    global $dial;
    $dial = $dialarg;
    global $topsname;
    $topsname = $topsnamearg;
    global $topname;
    $topname = "Top ".$dial;
    global $shortname;
    $shortname = $shortnamearg;

    function createArrays($i){
    global $shortname;
        $arr =    array (
                 "name" => "Canción ".$i,
                "desc" => "Escriba el nombre de la canción ".$i,
                "autor" => "Autor ".$i,
                "desc_autor" => "Escriba el Nombre del Autor ".$i,
                "id" => $shortname."cancicon_".$i,
                "va_autor" => $shortname."autor_".$i,
                "type" => "text",
                "std" => "");
        return $arr;
    }

    global $options;
    $options = array (
        array( "name" => "Top ".$dial,
               "type" => "title"),
        array( "type" => "open"),
        createArrays(1),
        createArrays(2),
        createArrays(3),
        createArrays(4),
        createArrays(5),
        createArrays(6),
        createArrays(7),
        createArrays(8),
        createArrays(9),
        createArrays(10),
        array( "type" => "close")
    );
    function cl_add_admin() {

        global $topname, $shortname, $options, $dial;

        if ( $_GET['page'] == basename(__FILE__) ) {

            if ( 'save' == $_REQUEST['action'] ) {

                foreach ($options as $value) {
                    update_option( $value['id'], $_REQUEST[ $value['id'] ] );
                    update_option( $value['va_autor'], $_REQUEST[ $value['va_autor'] ] );

                    foreach ($options as $value) {
                        if( isset( $_REQUEST[ $value['id'] ] ) ) { update_option( $value['id'], $_REQUEST[ $value['id'] ]  ); } else { delete_option( $value['id'] ); }
                        if( isset( $_REQUEST[ $value['va_autor'] ] ) ) { update_option( $value['va_autor'], $_REQUEST[ $value['va_autor'] ]  ); } else { delete_option( $value['va_autor'] ); }

                    }

                    header("Location: themes.php?page=functions.php&saved=true");
                    die;

                }
                } else if( 'reset' == $_REQUEST['action'] ) {

                    foreach ($options as $value) {
                        delete_option( $value['id'] ); 
                        delete_option( $value['va_autor'] ); 

                    }

                    header("Location: themes.php?page=functions.php&reset=true");
                    die;

                }
        }

        add_menu_page($topname, 'Tops Circuito Lider', 'edit_themes', $dial.'-handle', 'cl_admin');
        add_submenu_page($dial.'-handle', $topname, $topname, 'edit_themes', $dial.'sub-handle', 'cl_admin');

    }
    function cl_add_init() {  
        $file_dir=get_bloginfo('template_directory');  
        wp_enqueue_style("functions", $file_dir."/functions/options_panel.css", false, "1.0", "all");  
    } 

    function cl_admin() {

    global $topname, $shortname, $options;

         //echo $topname;
         //print_r($options);

        if ( $_REQUEST['saved'] ) echo '<div id="message" class="updated fade"><p><strong>'.$topname.' settings saved.</strong></p></div>';
        if ( $_REQUEST['reset'] ) echo '<div id="message" class="updated fade"><p><strong>'.$topname.' settings reset.</strong></p></div>';

        ?>
            <div class="wrap">
            <h2>Configurar <?php echo $topname; ?></h2>

            <form method="post">

            <?php     

        foreach ($options as $value) {
            switch ( $value['type'] ) {

            case "open":
            ?>
                <table width="100%" border="0" style="background-color:#cdcdcd; padding:10px;">

            <?php break;

            case "close":
            ?>

                </table><br />

            <?php break;

            case "title":
            ?>
                <table width="100%" border="0" style="background-color:#868686; padding:5px 10px;"><tr>
                <td colspan="2"><h3 style="font-family:Georgia,'Times New Roman',Times,serif;"><?php echo $value['name']; ?></h3></td>
                </tr>

            <?php break;

            case 'text':
            //echo "begins";
            ?>
                <tr>
                <td width="20%" rowspan="2" valign="middle">
                    <strong><?php echo $value['name']; ?></strong>
                    <br />
                    <strong><?php echo $value['autor']; ?></strong>
                </td>
                <td width="80%">
                    <input style="width:400px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" value="<?php     if ( get_settings( $value['id'] ) != "") { echo get_settings( $value['id'] ); } else { echo $value['std']; } ?>" /><br />
                    <small><?php echo $value['desc']; ?></small>
                    <br />
                    <input style="width:400px;" name="<?php echo $value['va_autor']?>" id="<?php echo $value['va_autor']?>" type="<?php echo $value['type']; ?>" value="<?php     if ( get_settings( $value['va_autor'] ) != "") { echo get_settings( $value['va_autor'] ); } else { echo $value['std']; } ?>" /><br />
                    <small><?php echo $value['desc_autor']; ?></small>
                </td>
                </tr>

                <tr><td colspan="2" style="margin-bottom:5px;border-bottom:1px dotted #000000;">&nbsp;</td></tr><tr><td colspan="2">&nbsp;</td></tr>

                <?php

                break;
            }
        //echo "after break";
        }
    ?>
         <style type="text/css" />
         .submit {
        display:inline;
        width:200px;
        }
        </style>
        <p class="submit">
        <input name="save" type="submit" value="Guardar" />
        <input type="hidden" name="action" value="save" />
        </p>
        <p class="submit">
        <input name="reset" type="submit" value="Borrar todos" />
        <input type="hidden" name="action" value="reset" />
        </p>
        </form>
        </div>
    <?php
    }
    add_action('admin_menu', 'cl_add_admin');
    add_action('admin_init', 'cl_add_init');
}

And this is how I am calling it from another file

include("includes/_createTop.php");
createAdminPage("923","Lider 92.3 FM","cl_923");
createAdminPage("931","Romance 93.1 FM","cl_931");
+1  A: 

You're calling createAdminPage() twice:

createAdminPage("923","Lider 92.3 FM","cl_923");
createAdminPage("931","Romance 93.1 FM","cl_931");

That causes the inner functions to be declared again, because this is what actually runs each time you call the function:

function createAdminPage($dialarg,$topsnamearg,$shortnamearg) {
    function createArrays($i){
    ...
    function cl_add_admin() {
    ...
    function cl_add_init() {  
    ...
    function cl_admin() {
}

Hence the redeclare error.

You should move the files out of the createAdminPage() function (but keep them within your include file) so you can call it without causing the redeclarations.

BoltClock
The thing is that I do want to use them again :S but with different value that why the arguments are different, if I take it out of the function createAdminPage ho I won't be able to reuse the code with different values :(
Dario Novoa
Just to clarify since I need to use the code 3 times but with different variable values, if I take it out of the function and include it 3 times I will get the same error, that's what I mean with I want to reuse the code. :) Sorry
Dario Novoa
Is there any way to `include_once()` instead of `include()`?
BoltClock
A: 

You've got function declarations within other functions, so each time the outer function is called, those inner declared functions are re-declared, and you get the error.

Simply move those functions out of the createAdminPage() function.

Paul Dixon
The thing is that I do want to use them again :S but with different value that why the arguments are different, if I take it out of the function createAdminPage ho I won't be able to reuse the code with different values :(
Dario Novoa
Just to clarify since I need to use the code 3 times but with different variable values, if I take it out of the function and include it 3 times I will get the same error, that's what I mean with I want to reuse the code. :) Sorry
Dario Novoa