FooKelvin Posted December 19, 2016 Share Posted December 19, 2016 Hi, i have my foreach loop codes here. What i trying to do is loop them out and list them with their own href link, but now the problem is, all of them are sharing href link. My Code: foreach ($data as $eid => $cdata) { $trows .= "<tr><td class='eid'>{$eid}</td>"; foreach ($cdata as $fc => $content) { $trows .= "<td><a href='#'>" . join(';<br>', $content) ."</a></td>"; } $trows .= "</tr>\n"; } Here is my output: After Inspect Element of HTML code: <tr><td class="eid">E1234</td><td><a href="#">A.pdf;<br>B.pdf;<br>C.xlsx</a></td></tr> Expected Output: <tr><td class="eid">E1234</td><td><a href="#">A.pdf</a>;<br><a href="#">B.pdf</a>;<br><a href="#">C.xlsx</a></td></tr> Quote Link to comment https://forums.phpfreaks.com/topic/302768-for-each-loop-with-their-own-a-href/ Share on other sites More sharing options...
requinix Posted December 19, 2016 Share Posted December 19, 2016 If you can turn the string A.pdf;B.pdf;C.xlsx into a prefix A.pdf separator B.pdf separator C.xslx suffixformat, all with the same "separator", then it fits into the code you already have. Do you see how you can do that? Quote Link to comment https://forums.phpfreaks.com/topic/302768-for-each-loop-with-their-own-a-href/#findComment-1540468 Share on other sites More sharing options...
Jacques1 Posted December 19, 2016 Share Posted December 19, 2016 First off, you need to escape all HTML input and use proper semantic markup instead of this <br> stuff. To avoid the kind of PHPHTML spaghetti code you currently have, it's also a good idea to use a template engine like Twig. The Twig version: {% for eid, cdata in data %} <tr> <td class="eid">{{ eid }}</td> {% for content in cdata %} <td> <ul> {% for file in content %} <li><a href="#">{{ file }}</a></li> {% endfor %} </ul> </td> {% endfor %} </tr> {% endfor %} Output: <tr> <td class="eid">E1234</td> <td> <ul> <li><a href="#">A.pdf</a></li> <li><a href="#">B.pdf</a></li> <li><a href="#">C.xlsx</a></li> </ul> </td> </tr> The PHP version: foreach ($data as $eid => $cdata) { $trows .= ' <tr> <td class="eid">'.html_escape($eid).'</td> '; foreach ($cdata as $content) { $trows .= ' <td> <ul> '; foreach ($content as $file) { $trows .= '<li><a href="#">'.html_escape($file).'</a></li>'; } $trows .= ' </ul> <td> '; } $trows .= ' </tr> '; } Pretty, eh? Quote Link to comment https://forums.phpfreaks.com/topic/302768-for-each-loop-with-their-own-a-href/#findComment-1540469 Share on other sites More sharing options...
FooKelvin Posted December 19, 2016 Author Share Posted December 19, 2016 If you can turn the string <td><a href="#">A.pdf</a>;<br><a href="#">B.pdf</a>;<br><a href="#">C.xlsx</a></td>into a prefix A.pdf separator B.pdf separator C.xslx suffixformat, all with the same "separator", then it fits into the code you already have. Do you see how you can do that? Hi requinix, sorry, I dont get you.. Quote Link to comment https://forums.phpfreaks.com/topic/302768-for-each-loop-with-their-own-a-href/#findComment-1540470 Share on other sites More sharing options...
requinix Posted December 19, 2016 Share Posted December 19, 2016 It's easy to make PHP look bad when you don't try to use it well. <?php foreach ($data as $eid => $cdata) { ?> <tr> <td class="eid"><?=html_escape($eid)?></td> <?php foreach ($cdata as $content) { ?> <td> <ul> <?php foreach ($content as $file) { ?> <li><a href="#"><?=html_escape($file)?></a></li> <?php } ?> </ul> </td> <?php } ?> </tr> <?php } ?> sorry, I dont get you..First of all, do you understand what the code you have already is doing? How it works? Quote Link to comment https://forums.phpfreaks.com/topic/302768-for-each-loop-with-their-own-a-href/#findComment-1540471 Share on other sites More sharing options...
Jacques1 Posted December 19, 2016 Share Posted December 19, 2016 It's easy to make PHP look bad when you don't try to use it well. <?php foreach ($data as $eid => $cdata) { ?> <tr> <td class="eid"><?=html_escape($eid)?></td> <?php foreach ($cdata as $content) { ?> <td> <ul> <?php foreach ($content as $file) { ?> <li><a href="#"><?=html_escape($file)?></a></li> <?php } ?> </ul> </td> <?php } ?> </tr> <?php } ?> What a beauty! And figuring out which <?php } ?> belongs to which statement makes this so much more fun. It's “Where's Waldo?” for programmers! Who needs technological advancements when they can have PHP? Quote Link to comment https://forums.phpfreaks.com/topic/302768-for-each-loop-with-their-own-a-href/#findComment-1540481 Share on other sites More sharing options...
FooKelvin Posted December 20, 2016 Author Share Posted December 20, 2016 It's easy to make PHP look bad when you don't try to use it well. <?php foreach ($data as $eid => $cdata) { ?> <tr> <td class="eid"><?=html_escape($eid)?></td> <?php foreach ($cdata as $content) { ?> <td> <ul> <?php foreach ($content as $file) { ?> <li><a href="#"><?=html_escape($file)?></a></li> <?php } ?> </ul> </td> <?php } ?> </tr> <?php } ?> First of all, do you understand what the code you have already is doing? How it works? Yup, my code currently able to join the array value in the same "td", but then i need them to be separate, cause i need to store their own href link. Quote Link to comment https://forums.phpfreaks.com/topic/302768-for-each-loop-with-their-own-a-href/#findComment-1540561 Share on other sites More sharing options...
Jacques1 Posted December 20, 2016 Share Posted December 20, 2016 So are you going to ignore the solution and keep working on this non-problem just for fun? Seems a bit stupid, but good luck. Quote Link to comment https://forums.phpfreaks.com/topic/302768-for-each-loop-with-their-own-a-href/#findComment-1540564 Share on other sites More sharing options...
FooKelvin Posted December 20, 2016 Author Share Posted December 20, 2016 So are you going to ignore the solution and keep working on this non-problem just for fun? Seems a bit stupid, but good luck. Hi Jacquest1, Im testing your code as well..is working actually.. Quote Link to comment https://forums.phpfreaks.com/topic/302768-for-each-loop-with-their-own-a-href/#findComment-1540565 Share on other sites More sharing options...
FooKelvin Posted December 20, 2016 Author Share Posted December 20, 2016 This is my array: Array ( [name] => [events] => Array ( [0] =>A.doc [1] =>B.pdf [2] =>C.pdf ) ) when echo it out, there is one extra row in between. like this.. Quote Link to comment https://forums.phpfreaks.com/topic/302768-for-each-loop-with-their-own-a-href/#findComment-1540566 Share on other sites More sharing options...
FooKelvin Posted December 20, 2016 Author Share Posted December 20, 2016 This is my array: Array ( [name] => [events] => Array ( [0] =>A.doc [1] =>B.pdf [2] =>C.pdf ) ) when echo it out, there is one extra row in between. like this.. CaptureX.PNG Please Ignore it. i manage to make it correct.. Quote Link to comment https://forums.phpfreaks.com/topic/302768-for-each-loop-with-their-own-a-href/#findComment-1540567 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.