Scott Scott - 5 months ago 8
PHP Question

PHP Array that creates multiple <li> instances

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 sorry :(

Thank you!




EDIT

NOW WORKING!!

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>";
}

Answer

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"
    }
}