supposedlysupposed Posted February 15, 2009 Share Posted February 15, 2009 I have this in a part of a page I'm working on, if ($tabs[$i] == 0) { $curcat[0] = count($nav); $nav[$curcat[0]][0] = $name[$i]; } else if ($tabs[$i] == 1) { $curcat[1] = count($nav[$curcat[0]]); $nav[$curcat[0]][$curcat[1]][0] = $name[$i]; } else if ($tabs[$i] == 2) { $curcat[2] = count($nav[$curcat[0]][$curcat[1]]); $nav[$curcat[0]][$curcat[1]][$curcat[2]][0] = $name[$i]; } else if ($tabs[$i] == 3) { $curcat[3] = count($nav[$curcat[0]][$curcat[1]][$curcat[2]]); $nav[$curcat[0]][$curcat[1]][$curcat[2]][$curcat[3][0] = $name[$i]; } and it's obviously too long and repetitive, especially since I plan to have it go through itself at least five times. I've shortened it down to this... but I'm stuck on how to make it write more [$curcat[...]]'s depending on the value of $tab, where the areas of three periods in the code below are. Maybe something recursive would work, but I wouldn't know how to do that. Any ideas? for($j=0; $j<=max($tabs); $j++) { $curcat[$j] = count($nav . . . ); $nav[$curcat[0] . . . ] = $name[$i]; } Link to comment https://forums.phpfreaks.com/topic/145271-how-can-i-simplify-a-bunch-of-if-statements/ Share on other sites More sharing options...
genericnumber1 Posted February 15, 2009 Share Posted February 15, 2009 You could replace the above code with... <?php $countIndex =& $nav; $nameIndex =& $nav; for($j = 0; $j <= $tabs[$i]; ++$j) { if($j != $tabs[$i]) { $countIndex =& $countIndex[$curcat[$j]]; } else { $curcat[$j] = count($countIndex); } $nameIndex =& $nameIndex[$curcat[$j]]; } $nameIndex[0] = $name[$i]; something like that^ (untested, and I'd be shocked if it worked without tweaking), but there is very likely a better solution that I can't help work out without seeing the rest of the surrounding code and actually knowing what the heck this code is supposed to do . And you're right, the above could be done even more cleanly with recursive functions, but the problem is that so much varied data is used in the above algorithm that you'd have a lot of hassle passing around the data between the functions. Link to comment https://forums.phpfreaks.com/topic/145271-how-can-i-simplify-a-bunch-of-if-statements/#findComment-762627 Share on other sites More sharing options...
supposedlysupposed Posted February 15, 2009 Author Share Posted February 15, 2009 Cool! That actually works perfect without even touching it. I don't really get what it does though, mainly because I'm not familiar with =&, what is that? The code is actually supposed to make the array for a list of navigation links, from an organized text file. Here's the surrounding code, if you're interested. $handle = @fopen("websiteoutline.txt", "r"); if ($handle) { $i = 1; while (!feof($handle)) { $line = fgets($handle, 4096); $tabs[$i] = substr_count($line,"\t"); $line = preg_replace("/ \\{.*\\}/", "", $line); $line = str_replace("\t", "", $line); $line = str_replace("\n", "", $line); $line = urlencode($line); $line = str_replace("%0D", "", $line); $line = urldecode($line); $link[$i] = strstr($line, ' '); $link[$i] = str_replace(' ', '', $link[$i]); $name[$i] = str_replace(' '.$link[$i], '', $line); if ($name[$i]) { $countIndex =& $nav; $nameIndex =& $nav; for($j = 0; $j <= $tabs[$i]; ++$j) { if($j != $tabs[$i]) { $countIndex =& $countIndex[$curcat[$j]]; } else { $curcat[$j] = count($countIndex); } $nameIndex =& $nameIndex[$curcat[$j]]; } $nameIndex[0] = $name[$i]; } $i++; } fclose($handle); } function dispNav(&$array,$where) { if (is_array($array)) { foreach ($array as $key => $val) { $where .= $key; if ($array[$key][1]) { if (is_array($val)) { $val = $val[0]; echo '<li><a class="leftnav" href="javascript:showHide(\'navcat'.$where.'\');">'.$val.'</a></li>'."\n"; echo '<ul class="leftnav" id="navcat'.$where.'">'."\n"; dispNav($array[$key], $where); echo "</ul>\n"; } else { dispNav($array[$key], $where); } } else { if (is_array($val)) { $val = $val[0]; } echo '<li><a class="leftnav" href="javascript:void('.$where.');">'.$val.'</a></li>'."\n"; } } } } foreach ($nav as $key => $val) { if (is_array($val[0])) { $val[0] = $val[0][0]; } echo '<input type="checkbox" class="leftnav" name="stayopen_navcat'.$key.'0"></input>'."\n"; echo '<li><a class="leftnav" href="javascript:showHide(\'navcat'.$key.'0\');">'.$val[0].'</a></li>'; echo '<ul class="leftnav" id="navcat'.$key.'0">'; dispNav($nav[$key],$key); echo "</ul>\n<br />\n"; } Link to comment https://forums.phpfreaks.com/topic/145271-how-can-i-simplify-a-bunch-of-if-statements/#findComment-762629 Share on other sites More sharing options...
genericnumber1 Posted February 15, 2009 Share Posted February 15, 2009 Well it's embarrassingly messy, which is why I'm surprised it works... The code in my first post works by building up references (the &) to the value you want... so for n = 3, here's a run through... countIndex and nameIndex refer to nav iteration 0: countIndex and nameIndex now refer to nav[index0] iteration 1: countIndex and nameIndex now refer to nav[index0][index1] iteration 2: countIndex and nameIndex now refer to nav[index0][index1][index2] iteration 3: index3 is set by counting the number of elements in the variable referred to by countIndex nameIndex now refers to nav[index0][index1][index2][index3] set the value of the variable referred to by nameIndex[0] (which is nav[index0][index1][index2][index3][0]) That said, you can simplify it even more now that I've seen the full code... <?php $nameIndex =& $nav; for($j = 0; $j <= $tabs[$i]; ++$j) { if(!isset($curcat[$j])) $curcat[$j] = count($nameIndex); $nameIndex =& $nameIndex[$curcat[$j]]; } $nameIndex[0] = $name[$i]; ?> (Again, not guaranteed to work since I haven't tried it yet and it's 4:30 AM so my brain is very similar to mush). Heck, there's an even simpler version I can think of if $tabs[$i] is predictable. Link to comment https://forums.phpfreaks.com/topic/145271-how-can-i-simplify-a-bunch-of-if-statements/#findComment-762638 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.