Jump to content

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

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

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

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. 

Link to post
Share on other sites
This thread is more than a year old.

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.