Page 1 of 1

loops

Posted: Tue Nov 04, 2003 6:55 pm
by seeker2921
So I want to create a loop to save on code space.. Currently I have 14 elseif's I want one.. Here is part of the code I am using..

Code: Select all

<?
	if(!isset($go)){
include("home.php");
		}
elseif($go==$links[0]){
include("$url[0]");
}
?>
The rest is just the elseif with diff numbers for the arrays.. How would I go about creating a loop for that? Thanx for your help.. And sorry I'f this is simple.. I'm still learning..

Posted: Tue Nov 04, 2003 8:33 pm
by Gen-ik
Here's a quick example but you should check out this.. [php_man]for()[/php_man]

Code: Select all

<?php

$links[0] = "link one";
$links[1] = "link two";
$links[2] = "link three";

$url[0] = "one.php";
$url[1] = "two.php";
$url[2] = "three.php";

if(!isset($go))
{
    include("home.php");
}
else
{
    for($i=0; i<count($links); $i++)
    {
        if($go==$links[$i])
        {
            include($url[$i]);
            break;
        }
    }
}

?>