Link to home
Start Free TrialLog in
Avatar of error77
error77

asked on

PHP Code assistance needed with code logic

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
Avatar of mankowitz
mankowitz
Flag of United States of America image

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;
}
Avatar of error77
error77

ASKER

Interesting ... and how would I use it?
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

Avatar of error77

ASKER

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.
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

Avatar of error77

ASKER

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)
Avatar of error77

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of error77

ASKER

THanks
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