Solved

Tidier code

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
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 …

705 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