-2

I am creating a PHP menu system (for an education program) that has a whole lot of module numbers, if statements, etc.

What I would love to do is go:

'module_number' = 'NUMBER'
'module_name'   = 'NAME'
'module_url'    = 'URL',

'module_number' = 'NUMBER'
'module_name'   = 'NAME'
'module_url'    = 'URL',

'module_number' = 'NUMBER'
'module_name'   = 'NAME'
'module_url'    = 'URL', etc, etc

and that then creates a li element for each module above in the format:

<li>
    NUMBER
    <?php if (( NUMBER * 3 - 1 ) < $GlobalVariable ) { ?>
        <a href="URL">NAME</a>
    <?php } else { ?>
        NAME    
</li>

Is there some way I can do that?

I assume it would be with arrays, etc but I am still learning.


EDIT:

Ok, I've got a multidimensional array happening (thanks for telling me what they are called so I could learn).

$daysregistered = 2;
$modules = array (
    array(0,'Thought Engineering Introduction','thought-engineering/'),
    array(1,'Leadership','thought-engineering/leadership'),
    array(2,'The Key to Velocity','thought-engineering/the-key-to-velocity'),
    array(3,'Master Level Communication','thought-engineering/master-level-communication'),
    array(4,'Priming for Opportunity','thought-engineering/'),
    array(5,'Quality','thought-engineering/'),
    array(6,'Your Most Critical Role as a Leader','thought-engineering/'),
    array(7,'Comprehensive Single System Perspective','thought-engineering/'),
    array(8,'Nexus Points & Income Time/Busy Time','thought-engineering/'),
    array(9,'Goals, Precession & Motion','thought-engineering/'),
    array(10,'Risk','thought-engineering/'),
    array(11,'Measurement','thought-engineering/'),
    array(12,'Bringing it All Together','thought-engineering/')
);

for ($row = 0; $row < 12; $row++) {
    $unlocked = ( $modules[$row][0] * 3 - 1 ) < $daysregistered;
    echo "<li>";
    if ( $unlocked ) { echo "<a href='".$modules[$row][2]."'>"; };
    echo "<span class='module_no'>".$modules[$row][0]."</span>";
    echo "<span class='module_descript'>".$modules[$row][1];
    echo "<span class='module_access";
    if ( $unlocked ) { echo " lock"; };
    echo"'><i class='fa fa-2x fa-";
    if ( ! $unlocked ) { echo "un"; };
    echo "lock";
    if ( ! $unlocked ) { echo "-alt"; };
    echo "' aria-hidden='true'></i></span></span>";
    if ( $unlocked ) { echo "</a>"; };
    echo "</li>";
}
8
  • You just asked a question a few minutes ago: stackoverflow.com/q/37716211/3933332 and you got some links to read up on. Now I would highly recommend you to really give you some time to read them and play around with it. Because here you will need to use a multidimensional array and to understand them you first need to understand a basic array. (Maybe even google for some basic tutorial exercises with arrays) Commented Jun 9, 2016 at 3:39
  • For your question: The title doesn't need tags, such as PHP, because you already got tags to tag your question; The title should be like a "keyword summary" of your problem; In the question body itself you can try to be more specific and show some effort: show attempts and research which you have done, where you are, where you want to get and where you are stuck; At the end "Thank you!" is just noise and you can remove it from the question; Commented Jun 9, 2016 at 3:44
  • (Might be the best for right now to delete the question here; Give yourself some time to read and learn the new stuff from the old question; Doing some research for the new question; Read How to Ask again; Because downvoted questions count against you) Commented Jun 9, 2016 at 3:48
  • Fair call. I'm trying it at the moment. Almost there. I am just experiencing an error will post as an edit. Commented Jun 9, 2016 at 4:05
  • That is what you should normally do before you ask a question. You code, you try stuff and you get stuck. You isolate the problem, debug, search and trial & error. Then if you don't make any progress and haven't found anything on the internet you read minimal reproducible example and How to Ask and write up your question. Commented Jun 9, 2016 at 4:09

1 Answer 1

2

To apply certain program logic to several elements, it is best to use a loop of some sort - it prevents you from repeating yourself (DRY-Principle - "Don't Repeat Yourself").

PHP knows several loop constructs, while and for being the most common ones. However for arrays, especially if you do only plan to read, not write them, there is an easy looping instruction provided by PHP as well: foreach.

foreach($array as $element) executes everything within the statement's body for every element in $array, the current element is accessible with $element.

So the easiest way for you to solve your problem is, to store every element inside an array, and use said foreach to loop over it (looping over an array is also called iterating over an array).


As your data is more complex than just one value, you cannot store it in an array directly. You first need to structure it in a way that allows you to store all 3 properties of each entry (module_number,module_name,module_url) in one variable.

The easiest option is to use associative arrays. Those are arrays with named keys (instead of numeric indices). YOu could structure each of your entries like this:

$entry = array(); // Since PHP5: $entry = []
$entry['module_number'] = 'NUMBER';
$entry['module_name']   = 'NAME';
$entry['module_url']    = 'URL';

Using array literals this can also be written like this:

$entry = array (
    'module_number' => 'NUMBER',
    'module_name'   => 'NAME',
    'module_url'    => 'URL'
)  // in PHP5+ you can also use [..] instead of array(...) again

Now you can store multiple entries with this exact structure in one array, then use foreach to loop over it (remark: $array[]=$entry prepends a new entry to an existing array, it's equivalent to array_push($array, $entry)):

// create an empty array
$entries  = [];

// add data
$entries[]= [
    'module_number' => 'NUMBER',
    'module_name'   => 'NAME',
    'module_url'    => 'URL'
];
$entries[]= [
    'module_number' => 'NUMBER',
    'module_name'   => 'NAME',
    'module_url'    => 'URL'
];
$entries[]= [
    'module_number' => 'NUMBER',
    'module_name'   => 'NAME',
    'module_url'    => 'URL'
];

// loop over all entries
foreach ($entries as $entry) {
    echo "<li>\n" . $entry['module_number'] . "\n";
    if (($entry['module_number'] * 3 - 1) < $GlobalVariable) {
        echo '<a href="' . $entry['module_url'] . '">' . $entry['module_name'] . "</a>\n";
    } else {
        echo $entry['module_url'] . "\n"
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

OP just asked a question a few minutes ago and he didn't even know about arrays. So just providing him some code probably won't help him and will just damage the site. Since he can just copy&paste the code without even understanding it. Then he will come back and ask the next question to write him code.
@Rizier123 I didn't read your titles before I made the effort writing the code. Seeing how tihngsd are now, I ahve to agree with you. However now I already put some effort into answering the question, so I don't really feel like deleting it...
*comments Damnit, can't english today
Yes. If you want you now can try to edit your answer and make it even more detailed trying to explain OP what is going on and maybe add some manual links for him to read up on.
@Rizier123 on it as we speak
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.