Jump to content
Sign in to follow this  
FooKelvin

For Each Loop with their own a href

Recommended Posts

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:

 

post-179514-0-85916000-1482126157_thumb.png

 

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>

Share this post


Link to post
Share on other sites

If you can turn the string


A.pdf;
B.pdf;
C.xlsx
into a

prefix A.pdf separator B.pdf separator C.xslx suffix
format, all with the same "separator", then it fits into the code you already have.

 

Do you see how you can do that?

Share this post


Link to post
Share on other sites

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? ::)

Share this post


Link to post
Share on other sites

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 suffix
format, 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..

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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. 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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..

Share this post


Link to post
Share on other sites

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..

post-179514-0-74678600-1482200724_thumb.png

 

Share this post


Link to post
Share on other sites

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..

attachicon.gifCaptureX.PNG

Please Ignore it. i manage to make it correct..

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

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.