Jump to content

I need way to replace following code

- - - - -

  • Please log in to reply
4 replies to this topic

#1
AdrianWierciochPHP

AdrianWierciochPHP

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
Hi. I have some problem with my code. I have script called add.php which add lines in some file but this script take action by get method and my code is not professional and readable so I must replace it. It look like following:

require_once('configure.php');

require_once( TEMPLATE_CLASS );

//require_once( MAILER_CLASS );

$tpl = new Template();

$tpl->assign('action', MAIN_PAGE_ADDRESS);


if (!isset($_GET['action'])) {


	

	$tpl->assign(array(

			'add_message' => ADD_MESSAGE,

			

			'add_label_choose' => ADD_LABEL_CHOOSE,

			'add_multi' => ADD_MULTI,

			'add_single' => ADD_SINGLE,

			'add_submit_choose' => ADD_SUBMIT_CHOOSE,

			'title' => ADD_TITLE

			));

	$tpl->display( DISPLAY_ADD . EXT_T );


} elseif($_GET['action'] == 'add') {

	$tpl->assign('add_submit', ADD_SUBMIT);

	$choose = $_GET['choose'];

	$tpl->assign('hidden', $choose);

	if ($choose == 'single') {

		$tpl->assign(array(

				'add_label_text' => ADD_LABEL_TEXT,

				));

		$tpl->display( DISPLAY_ADD . '_action-single' . EXT_T );

	} elseif($choose == 'multi') {

		$tpl->assign('add_label_textarea', ADD_LABEL_TEXTAREA);

		$tpl->display( DISPLAY_ADD . '_action-multi' . EXT_T );

	}

} elseif($_GET['action'] == 'try') {

	//code to add some lines in file

} //end elseif


And I must replace conditional instruction but I don`t know how. I want replace conditional because I think that is not readable and any PHP Development deprecate it.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Maybe a little more structured like this?
<?php
require_once('configure.php');
require_once( TEMPLATE_CLASS );

$tpl = new Template();
$tpl->assign('action', MAIN_PAGE_ADDRESS);

switch($_GET['action']) {
    case 'add':
    $choose = $_GET['choose'];
    $tpl->assign('add_submit', ADD_SUBMIT);
    $tpl->assign('hidden', $choose);
    switch($choose) {
        case 'single':
            $tpl->assign(array(
                'add_label_text' => ADD_LABEL_TEXT,
            ));
            $tpl->display( 
                 DISPLAY_ADD . '_action-single' . EXT_T 
            );
        break;
        case 'multi':
            $tpl->assign(
                'add_label_textarea', ADD_LABEL_TEXTAREA
            );
            $tpl->display(
                DISPLAY_ADD . '_action-multi' . EXT_T 
            );
        break;
    }
    break;
    case 'try':
        //code to add some lines in file
    break;
    default:
        $tpl->assign(array(
            'add_message' => ADD_MESSAGE
            , 'add_label_choose' => ADD_LABEL_CHOOSE
            , 'add_multi' => ADD_MULTI
            , 'add_single' => ADD_SINGLE
            , 'add_submit_choose' => ADD_SUBMIT_CHOOSE
            , 'title' => ADD_TITLE
        ));
        $tpl->display(
            DISPLAY_ADD . EXT_T 
        );
    break;
} 

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
  • Location:New York, NY
Notice: Undefined index: action

:)

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200

John said:

Notice: Undefined index: action

:)
I had rewritten it to be clearer, an undefined index is not an error and is handled by "default:", I was unsure if wrapping the whole thing in an if and preventing default from being run was a choice that was wanted, so I left it as it is.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
AdrianWierciochPHP

AdrianWierciochPHP

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
I think at the beginning of code I have to just define index by simple script:

if (!isset($_GET['index'])) {

$index = 'other'; // something other action handled by 'default' in switch loop  

} else {

$index = $_GET['index'];

}






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users