We help IT Professionals succeed at work.

PHP Code assistance needed with code logic

error77
error77 asked
on
Hi all,

I need assistance modifying the following code.

<?php $li_count = 0; 
	echo '<ul>';
    
	echo '<li><a href="#">One</a></li>';
    $li_count++;
	
	echo '<li><a href="#">Two</a></li>';
    $li_count++;
	
	echo '<li><a href="#">Three</a></li>';
	$li_count++;
	
	//Dynamic pages.
	
	if ($var1 == '1') {
		echo '<li><a href="#">Dynamic1</a></li>';
		$li_count++;
	}	
	
	
	//Area 2
	if ($li_count > 4) {
		echo '<ul>';
		echo '<li><a href="#">Sub One</a></li>';
		echo '<li><a href="#">Sub Two</a></li>';
		echo '<li><a href="#">Sub Three</a></li>';
		echo '</ul>';
	}
	
	echo '</ul>';
		 ?>

Open in new window

           

What I need it to do is the following:

If at any time $li_count is bigger than 4 then I just it to do Area 2. In other words the first 4 list items will show up as normal..all other items if more than 4 will do Area 2.

So, I need to code to check the total at all times and if more than 4 then you get the sub list for all lists after 4th.

Hope it makes sense, if not please let me know.

Thanks
Comment
Watch Question

CERTIFIED EXPERT

Commented:
I would probably make a function called li that would have a local variable and insert the item. Something like this:

$li_count=0;

li('<li><a href="#">One</a></li>');
li('<li><a href="#">Two</a></li>');
li('<li><a href="#">Three</a></li>');
li('<li><a href="#">Four</a></li>');
li('<li><a href="#">Five</a></li>');

function li($str) {
global $li_count;
$li_count++;
if ($li_count==4) { echo "<ul>\n"; }
echo $str;
}

Author

Commented:
Interesting ... and how would I use it?
Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Hi, error77. I prepared the code below which partially solves your problem. I say partially because my function uses an array. Since this array can or cannot hold some values depending on the value of several variables, to build these arrays could be boring or difficult, depending on the complexity of your code.
To build array inserting elements within it you can use array_splice() function: http://it2.php.net/manual/en/function.array-splice.php

Hope this helps

Cheers
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
function WriteLi($values){
    if (!is_array($values)){
        exit ("Only array can be used here.");
    }
    global $li_count;
    global $area2created;
    foreach($values as $v){
        if($li_count <= 3){
            echo '<li><a href="#">' . $v . '</a></li>';
            array_shift($values);
            $li_count++;
        }
    }
    if ($li_count > 3){
        if (!$area2created){
            echo "<ul>";
        }
        foreach ($values as $v){
            echo '<li><a href="#">' . $v . '</a></li>';
            array_shift($values);
            $li_count++;
        }
        echo "</ul>";
    }
}

echo "SHOW LIST WITH DYNAMIC VAR";
$li_count = 0;
$area2created = false;
echo '<ul>';
$var1 = "1";
if ($var1 == '1') {
    $litags = array("one", "two", "three", "Dynamic1", "SubOne", "SubTwo", "SubThree");
}else{
    $litags = array("one", "two", "three", "SubOne", "SubTwo", "SubThree");
}
WriteLi($litags);
echo '</ul>';
print "END WITH DYNAMIC VAR<br />";

echo "SHOW LIST WITHOUT DYNAMIC VAR";
$li_count = 0;
$area2created = false;
echo '<ul>';
$var1 = "0";
if ($var1 == '1') {
    $litags = array("one", "two", "three", "Dynamic1", "SubOne", "SubTwo", "SubThree");
}else{
    $litags = array("one", "two", "three", "SubOne", "SubTwo", "SubThree");
}
WriteLi($litags);
echo '</ul>';
echo "END WITH STATIC VAR";

?>

Open in new window

Author

Commented:
Hi marqusG,

I really appreciate your time and effort. I really wanted to have the lists

echo '<li><a href="#">One</a></li>';

instead of tags representing them.

Otherwise it makes things visually really complex for me.

I think I need something simpler than this so I can understand it.
Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Here you have the list printed as you wish. I can't help you to understand code, if you want. I'll prepare a cemmented version, if you need it. Unfortunately, this is the simplest mode I thought to solve your problem. Pewrhaps I'm wrong, but sometimes to get certain results the code complexity grows up independently by our wishes :-)
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
function WriteLi($values){
    if (!is_array($values)){
        exit ("Only array can be used here.");
    }
    global $li_count;
    global $area2created;
    foreach($values as $v){
        if($li_count <= 3){
            echo '<li>' . htmlentities('<li><a href="#">' . $v . '</a></li>') . '</li>';
            array_shift($values);
            $li_count++;
        }
    }
    if ($li_count > 3){
        if (!$area2created){
            echo "<ul>";
        }
        foreach ($values as $v){
            echo '<li>' . htmlentities('<li><a href="#">' . $v . '</a></li>') . '</li>';
            array_shift($values);
            $li_count++;
        }
        echo "</ul>";
    }
}

echo "SHOW LIST WITH DYNAMIC VAR";
$li_count = 0;
$area2created = false;
echo '<ul>';
$var1 = "1";
if ($var1 == '1') {
    $litags = array("one", "two", "three", "Dynamic1", "SubOne", "SubTwo", "SubThree");
}else{
    $litags = array("one", "two", "three", "SubOne", "SubTwo", "SubThree");
}
WriteLi($litags);
echo '</ul>';
print "END WITH DYNAMIC VAR<br />";

echo "SHOW LIST WITHOUT DYNAMIC VAR";
$li_count = 0;
$area2created = false;
echo '<ul>';
$var1 = "0";
if ($var1 == '1') {
    $litags = array("one", "two", "three", "Dynamic1", "SubOne", "SubTwo", "SubThree");
}else{
    $litags = array("one", "two", "three", "SubOne", "SubTwo", "SubThree");
}
WriteLi($litags);
echo '</ul>';
echo "END WITH STATIC VAR";

?>

Open in new window

Author

Commented:
Wow! I never thought it would be so complex to get what I needed working :o/

I'll have a further look and try to digest it :o)

Author

Commented:
What about if the code doesn't have to count to static lists?

I could hardcode a variable say there's 3 static lists ... and I type:

$no_of_static_lists = 3;

Then, the code would need to make sure that if more than 4 in total ...do Area2..

That easier?
Freelancer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Sorry, I was away. Could explain me better what you mean? I really don't understand... :-(

If you don't have dynamic list items, it would simpler to build array, but I would use my function anyway: I post the code of my function fully commented. Feel free to ask if something is obscure.

function WriteLi($values){
//    if value passed is not an array exit with a message
    if (!is_array($values)){
        exit ("Only array can be used here.");
    }
//    we refer to variable defined out of the function so we use reserved word global
    global $li_count;
    global $area2created;
//    for each value in the array treated as $v
    foreach($values as $v){
//        if li tag number is less or equal to 3
        if($li_count <= 3){
//            we print the list item
            echo '<li><a href="#">' . $v . '</a></li>';
//            we delete the element from the array***
            array_shift($values);
//            we have printed one li tag so we increment $li_count
            $li_count++;
        }
    }
//    if we have printed four li tags
    if ($li_count > 3){
//        we create another unordered list
        if (!$area2created){
            echo "<ul>";
        }
//        then, using the same array (the first three elements have been delted
        foreach ($values as $v){
//            we print others li tags in the new list
            echo '<li><a href="#">' . $v . '</a></li>';
//             we delete the element just printewd
            array_shift($values);
//             and increment $li_count
            $li_count++;
        }
//        finally we close the second list
        echo "</ul>";
    }
}

//*** this way we can use the same array in t5he second foreach loop

Open in new window


If you are able to build an array with all list item you wish to print, you can pass it to this function and i will print your list creating a new sublist when the first fopur items wiil have been printed yet.

In the second snippet you finad the same function improved: using this version you can decide how many items have to be the limit for the creation of the new sublist passinf the $n parameters this way:

$litags = array("one", "two", "three", "SubOne", "SubTwo", "SubThree");
WriteLi($litags, $n);  

function WriteLi($values, $n){
//    if value passed is not an array exit with a message
    if (!is_array($values)){
        exit ("Only array can be used here.");
    }
//    we refer to variable defined out of the function so we use reserved word global
    global $li_count;
    global $area2created;
//    for each value in the array treated as $v
    foreach($values as $v){
//        if li tag number is less or equal to the avlue you passed
        if($li_count <= $n){
//            we print the list item
            echo '<li><a href="#">' . $v . '</a></li>';
//            we delete the element from the array***
            array_shift($values);
//            we have printed one li tag so we increment $li_count
            $li_count++;
        }
    }
//    if we have printed the first group of li tags
    if ($li_count > $n){
//        we create another unordered list
        if (!$area2created){
            echo "<ul>";
        }
//        then, using the same array (the first three elements have been delted
        foreach ($values as $v){
//            we print others li tags in the new list
            echo '<li><a href="#">' . $v . '</a></li>';
//             we delete the element just printewd
            array_shift($values);
//             and increment $li_count
            $li_count++;
        }
//        finally we close the second list
        echo "</ul>";
    }
}

//*** this way we can use the same array in t5he second foreach loop

Open in new window


Cheers

Author

Commented:
THanks
Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Thanks for points.
If you need more assitance, feel free to ask. I'll keep turned on the monitoring for this question, so you'll can always contact me for problems related to this code. Don't have problems at all requiring assistance: maybe I'm not able to explain clearly my code so I can retry, if requested :-)

Cheers

Explore More ContentExplore courses, solutions, and other research materials related to this topic.