Solved

Tidier code

Posted on 2013-01-17
2
208 Views
Last Modified: 2013-02-02
I'm trying to get a class to accept an some arguments, to be able to accept some menu value. I would like to have an array with 2 arguments (Name and onClick) but unlimited number of entries.

So for example, for 2 I would have the array:-

array(2) {
  [0]=>
  array(2) {
    ["text"]=>
    string(6) "Hello1"
    ["onClick"]=>
    string(16) "alert('hello1');"
  }
  [1]=>
  array(2) {
    ["text"]=>
    string(6) "Hello2"
    ["onClick"]=>
    string(16) "alert('hello2');"
  }
}

Which works great, so to code it I've just done:-
$Menu = new proMenu(array(array("text" => "Hello1", "onClick" => "alert('hello1');"), array("text" => "Hello2", "onClick" => "alert('hello2');")));

Open in new window


But it just looks a mess, and wondering if anyone can advise a better way of doing this.
0
Comment
Question by:tonelm54
2 Comments
 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 250 total points
ID: 38789180
1. Either use line breaks and formatting:

$Menu = new proMenu(
  array(
    array("text" => "Hello1", "onClick" => "alert('hello1');"),
    array("text" => "Hello2", "onClick" => "alert('hello2');")
  )
);

2. Or write a wrapper that sets it up:

function quickProMenu()
{
  $texts = func_get_args();
  $parameter = array();
  foreach($texts as $text)
  {
    $parameter[] = array("text" => $text, "onClick" => "alert('".strtolower($text)."');");
  }
  return new proMenu($parameter);
}

$Menu = quickProMenu("Hello1","Hello2");
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 38789853
Here's my take on it.  I've found this to be easy to read and modify, and if the code is lined up neatly, it makes it much easier to get a good visual scan when you're debugging.

$Menu = new proMenu
( array
  ( array
    ( "text"    => "Hello1"
    , "onClick" => "alert('hello1');"
    )
  , array
    ( "text"    => "Hello2"
    , "onClick" => "alert('hello2');"
    )
  )
)
;

Open in new window

HTH, ~Ray
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question