Solved

Tidier code

Posted on 2013-01-17
2
194 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 108

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now