Jump to content

Separating list of outputted values


Omzy

Recommended Posts

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?

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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 : ""); 

?>


Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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].'';

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.