0

Ive been trying make this display as html list items it just a string that i explode then loop over each item i cant get it to out put correctly. Could some one please show me where im going wrong or suggest an new approch. this is what ive tried

$path = "1/2/3/4";

$expath =  explode("/",$path);

$ret = '';

echo '<ul>';
foreach ($expath as $pitem) {
echo '<li><a href='.$ret .= $pitem. "/".'>'.$pitem.'</a></li>';
}
echo '</ul>';

.

Desired out put on hrefs

1
1/2
1/2/3
1/2/3/4

Desired visual out LIs

  • 1
  • 2
  • 3
  • 4

Output i get be warned

  • 1
  • 1
  • 2/>2
  • 1
  • 2/>23/>3
  • 1
  • 2/>23/>34/>4
1
  • I guess you have an extra "=" sign in the '.$ret .= $pitem. "/".'. Wrap it with quotes, like '.$ret .'='. $pitem. "/".' . Commented Nov 18, 2015 at 2:26

4 Answers 4

2
$path = "1/2/3/4";
$expath = explode("/", $path);

echo '<ul>';
foreach ($expath as $i => $pitem) {
    $slice = array_slice($expath, 0, $i + 1);
    $path = implode('/', $slice);
    echo '<li><a href="' . $path . '">' . $pitem . '</a></li>';
}
echo '</ul>';
Sign up to request clarification or add additional context in comments.

1 Comment

@user2914191 is see now that what i tried just wasn't enough. Thank you.
0
$list = explode("/", "1/2/3/4");

This will create an array $list as:

echo $list[0]; // 1
echo $list[1]; // 2
echo $list[2]; // 3
echo $list[3]; // 4

Comments

0

This line is the problem: echo '<li><a href='.$ret .= $pitem. "/".'>'.$pitem.'</a></li>';

Should be formatted like:

echo "<li><a href='{$ret}={$pitem}/'>{$pitem}</a></li>";

or echo '<li><a href="'.$ret.'='.$pitem.'/">'.$pitem.'</a></li>';

Comments

0

Its because your $ret. Place that inside the loop. In your code you concatenate $pitem with $ret all older $ret values also get concat.

Try

<?php

$path = "1/2/3/4";

$expath =  explode("/",$path);


echo '<ul>';
foreach ($expath as $pitem) {
    $ret = '';

    echo '<li><a href='.$ret .= $pitem. "/".'>'.$pitem.'</a></li>';
}
echo '</ul>';

If you want = sign tobe there in the url then just change echo by following

 echo "<li><a href='$ret=$pitem/'>$pitem</a></li>";

PHP echo with double quotes will print variable value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.