Solved

Tidier code

Posted on 2013-01-17
2
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

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 110

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

733 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