J.K.A.
J.K.A.

Reputation: 7404

Array listing not showing properly in PHP

I have two array I merge both with following code:

$information = array_merge($this->resInfo, $this->resName);

Here first array contain id and email while second contain name only. After merging both array name part and info part showing like this:

Array
(
[0] => Array
    (           
        [id] => 91985         
        [email] => [email protected]         
    )

[1] => Array
    (
        [id] => 71262         
        [email] => [email protected]         
    )

[2] => Array
    (
        [name] => New york
    )

[3] => Array
    (
        [name] => Alaska
    )

[4] => Array
    (
        [name] => Sanfransico
    )
)

The array containing id, email and name. Here my email field value is always showing same email id while id field and name field value changing every time. I want to list email id only once while id and name as multiple depend on size. I created the following code:

<?php foreach ($information as $info) { ?>
    <ul>                                                     
        <li style="list-style: none;">
            <a href="/profile/id/<?php echo $info['id']; ?>/email/<?php echo $info['email']; ?>" style="color: #185D9B; text-decoration: underline;">
                <?php echo $info['name'] ?>
            </a>
        </li>                        
    </ul>
<?php } ?>

Here it is showing both of the $info['name'] properly while it showing blank $info['id']; and $info['email']; in href tag. Whats wrong with following code.

Upvotes: 1

Views: 135

Answers (2)

Czar Pino
Czar Pino

Reputation: 6324

The first 2 elements of the array has an id & email but no name hence no name is displayed. The last 3 however have names but no id & email hence names are displayed but href are broken. Here is how that page will render:

<ul>                                                     
    <li style="list-style: none;">
        <a href="/profile/id/91985/email/[email protected]" style="color: #185D9B; text-decoration: underline;">
        </a>
    </li>                        
</ul>
<ul>                                                     
    <li style="list-style: none;">
        <a href="/profile/id/71262/email/[email protected]" style="color: #185D9B; text-decoration: underline;">
        </a>
    </li>                        
</ul>
<ul>                                                     
    <li style="list-style: none;">
        <a href="/profile/id//email/" style="color: #185D9B; text-decoration: underline;">
            New york
        </a>
    </li>                        
</ul>
<ul>                                                     
    <li style="list-style: none;">
        <a href="/profile/id//email/" style="color: #185D9B; text-decoration: underline;">
            Alaska
        </a>
    </li>                        
</ul>
<ul>                                                     
    <li style="list-style: none;">
        <a href="/profile/id//email/" style="color: #185D9B; text-decoration: underline;">
            Sanfransico
        </a>
    </li>                        
</ul>

Upvotes: 1

Jalpesh Patel
Jalpesh Patel

Reputation: 3188

First time info index have a value like

$info[id]="91985"
$info[email][email protected] 
$info['name'] =""(NULL)

so the first record is not display it's name and link is not appear to you there is href value is correct but not display name

same for the index number 1 and 2 value

and when the index is 3 and 4 at that time value of the

$info[id]="" (null)
$info[email]="" ()
$info['name'] ="Alaska"

so it is te display link but not href value are proper href value is="/email/" only so it will not work properly

I hope you understood what i mean to say if you have any problem in my answer then let me know...

Upvotes: 1

Related Questions