Gandalf
Gandalf

Reputation: 13683

Displaying some value in a foreach

I have this code which i am using to present my data in a table format

<style>
td{
border:1px solid orange;
}
table{
border:1px solid orange;
}
</style>
<?php
$arr = array(
    'one' => array(
        'sidebarname' => 'first',
        'sidebarid' => 'first1'
    ),
    'two' => array(
        'sidebarname' => 'oneaa',
        'sidebarid' => 'onebb'
    ),
    'three' => array(
        'sidebarname' => 'onecc',
        'sidebarid' => 'onedd'
    ),
    'four' => array(
        'sidebarname' => 'oneee',
        'sidebarid' => 'oneff'
    ),
    'five' => array(
        'sidebarname' => 'onegg',
        'sidebarid' => 'onehh'
    ),
    'six' => array(
        'sidebarname' => 'oneii',
        'sidebarid' => 'onejj'
    )
);
echo '<table>';
foreach ($arr as $key => $value) {
echo '<tr>'.'<td>'."<input type='text' value=$key />".'</td>';

foreach ($value as $keyed=> $newvalue) 
echo '<td>'."<input type='text' value=$newvalue />".'</td>';   
}

echo '</tr></table>';

?>

I am displaying the main key of each array and values of the first and second arrays.For instance

'five' => array(
            'sidebarname' => 'onegg',
            'sidebarid' => 'onehh'
        ),

i am displaying five,the value of sidebarname and the value of sidebarid.This is what i am expecting in html.

http://jsfiddle.net/83hNB/

How can i use foreach to display sidebarid as a textarea instead of the input text.

Upvotes: 0

Views: 118

Answers (3)

Wearybands
Wearybands

Reputation: 2455

You should remove the the second loop and dispaly the first value foreach key as input and the sencond value as text area.

You code should looks like this I tried it and it outputs according to what you need.

 <style>
 td{
 border:1px solid orange;
 }
 table{
  border:1px solid orange;
 }
 </style>
<?php
$arr = array(
'one' => array(
    'sidebarname' => 'first',
    'sidebarid' => 'first1'
),
'two' => array(
    'sidebarname' => 'oneaa',
    'sidebarid' => 'onebb'
),
'three' => array(
    'sidebarname' => 'onecc',
    'sidebarid' => 'onedd'
),
'four' => array(
    'sidebarname' => 'oneee',
    'sidebarid' => 'oneff'
),
'five' => array(
    'sidebarname' => 'onegg',
    'sidebarid' => 'onehh'
),
'six' => array(
    'sidebarname' => 'oneii',
    'sidebarid' => 'onejj'
)
);

echo '<table>';
  foreach ($arr as $key => $value) {
  echo "<tr><td><input type='text' value='".$key."' /></td>";
  echo "<td><input type='text' value='".$value['sidebarname']."' /></td>";   
  echo "<td><textarea>".$value['sidebarid']."</textarea></td>"; 
}
echo '</tr></table>';


?> 

Hope it will help

Upvotes: 2

LeJared
LeJared

Reputation: 2052

Change your inner loop to this:

<?php
foreach ($value as $keyed=> $newvalue) {
    if ($keyed === 'sidebarid' ) {
        echo '<td><textarea>'.htmlspecialchars($newvalue).'</textarea></td>';   
    } else {
        echo '<td><input type="text" value="'.htmlspecialchars($newvalue).'" /></td>';   
    }
}
?>

Please note the htmlspecialchars() and the " on the value="" attribute. Both are really important when outputting userdata to HTML.

Upvotes: 1

Mihai Iorga
Mihai Iorga

Reputation: 39704

You should remove the second foreach as you already know the keys:

echo '<table>';
foreach ($arr as $key => $value) {
    echo "<tr><td><input type='text' value='".$key."' /></td>";
    echo "<td><input type='text' value='".$value['sidebarname']."' /></td>";   
    echo "<td><textarea>".$value['sidebarid']."</textarea></td>"; 
}
echo '</tr></table>';

Upvotes: 1

Related Questions