Solved

Tidier code

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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.

920 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

16 Experts available now in Live!

Get 1:1 Help Now