Omzy Posted January 25, 2009 Share Posted January 25, 2009 Let's say I have code as follows: foreach($cat as $index => $value) { if($cat1 == $index || $cat2 == $index || $cat3 == $index || $cat4 == $index || $cat5 == $index) { echo ' <a href="/directory/'.$index.'/">'.$value[2].'</a> '; } } This code will output UP TO 5 hyperlinks in one line, depending on which, if any values exist in the array. For example: Item1 Item2 Item3 Item4 Item5 I now need to present this in a better form, as follows: - Item1 | Item2 | Item3 | Item4 | Item5 The '|' separator should not appear after the last item, and it should not appear if there is only one item. The '-' seperator should only appear before the first item, if one exists. How do I do this? Quote Link to comment Share on other sites More sharing options...
elgoog Posted January 25, 2009 Share Posted January 25, 2009 You could maybe try something like: foreach($cat as $index => $value) { if($cat1 == $index || $cat2 == $index || $cat3 == $index || $cat4 == $index || $cat5 == $index) { $output .= '<a href="/directory/'.$index.'/">'.$value[2].'</a> | '; } } substr($output,-3,3) == ' | '; echo $output; Quote Link to comment Share on other sites More sharing options...
Prismatic Posted January 25, 2009 Share Posted January 25, 2009 Try this, haven't tested it <?php $i = 0; $str = ""; foreach($cat as $index => $value) { if($cat1 == $index || $cat2 == $index || $cat3 == $index || $cat4 == $index || $cat5 == $index) { if($i % 2) { $str .= " | "; } $str .= "<a href="/directory/'.$index.'/">'.$value[2].'</a>"; } $i++; } echo ($var != "" ? "- ".$str : ""); ?> Quote Link to comment Share on other sites More sharing options...
.josh Posted January 25, 2009 Share Posted January 25, 2009 Based on your variable names, I have a sneaking suspicion you could be writing your code a whole lot better in the first place, but working with what you have... foreach($cat as $index => $value) { if($cat1 == $index || $cat2 == $index || $cat3 == $index || $cat4 == $index || $cat5 == $index) { $output[] = '<a href="/directory/'.$index.'/">'.$value[2].'</a>'; } } $output = '- ' . implode(' | ',$output); echo $output; Quote Link to comment Share on other sites More sharing options...
Omzy Posted January 25, 2009 Author Share Posted January 25, 2009 Thanks, Crayon Violent. That works perfect. How do you think my code could be improved though? Quote Link to comment Share on other sites More sharing options...
.josh Posted January 25, 2009 Share Posted January 25, 2009 well i can't tell you for sure, since I don't know what your code looks like overall. But I do see a bunch of variables that are named similar things, which means you probably could have used an array instead, and just done $catx = array(array of those vars); if (in_array($index, $catx)) { ... } Quote Link to comment Share on other sites More sharing options...
Omzy Posted January 25, 2009 Author Share Posted January 25, 2009 Cheers for that. Do you know how I can implement your solution in the below code: foreach($_POST['selection'] as $value) { echo ''.$value.''; foreach($_POST['tags'] as $index1 => $value1) { if($value1 == $value) { echo ''.$subcats[$index1][2].''; } } } So basically I need to replace the line: echo ''.$subcats[$index1][2].''; Quote Link to comment Share on other sites More sharing options...
Omzy Posted January 26, 2009 Author Share Posted January 26, 2009 BUMP for the above query. Quote Link to comment Share on other sites More sharing options...
Omzy Posted January 26, 2009 Author Share Posted January 26, 2009 BUMP for the above query. Quote Link to comment Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 Reason you are not getting the answers you want is because you are not giving us much to work with. What do you expect to be outputted from the above code? Why are you referencing [2] of subcats, what is suppose to be stored in there? Where is subcats defined and what does that array look like? Does the current script not print correctly ...or what is the issue? Quote Link to comment Share on other sites More sharing options...
Omzy Posted January 26, 2009 Author Share Posted January 26, 2009 Well I think all the info that is needed is in that one post. I thought I could implement Crayon Violet's solution into this piece of code - the only functional thing that differentiates this piece of code from the first one is that this has a foreach loop within a foreach loop. Essentially what this code is doing is outputting a list of 'child' values that match it's 'parent' value. Quote Link to comment Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 I guess what I am getting at, is where were you setting $cat1 etc at? I do not know how those are being defined etc, which would make trying to answer your question like having a blind man drive you to the supermarket...post more code and your question will be answered. CV touched on this: well i can't tell you for sure, since I don't know what your code looks like overall. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.