Jump to content

How Insert div inside foreach loop with return


demohat2017

Recommended Posts

<?php

function test(){
$x = '';

$arr = array('co'=>'color','ct'=>'design','cv'=>'weight','cm'=>'hight','cp'=>'price');
foreach($arr as $k=> $v){
$x .= '<tr><td> Td '.$k.' </td><td> Td '.$v.'</td></tr>'

/* AFTER ACCESS THE ROW THAT DISPLAY "<tr><td> Td ct </td><td>Td design </td></tr>" */
/* insert HTML CODE (not by echo ) then complete the loop  */

}

return $x;
}

?>


How can I do it?

Link to comment
Share on other sites

3 hours ago, demohat2017 said:

How can I do it?

Simpler than you might think. 

The foreach loops through the array, giving you the key and value of each item in turn. 

For every item, you want to insert the Table Row construct. 

For the item where the key is "ct", you want to insert some extra "stuff".

 

Try this: 

foreach( $arr as $k => $v ){
   $x .= sprintf( '<tr><td> Td %s </td><td> Td %s</td></tr>', $k, $v ); 
   if ( 'ct' === $k )
   {
      $x .= $Insert_HTML_Here ;
   }
}

Regards, 
   Phill  W.

  • Like 1
Link to comment
Share on other sites

On 6/2/2020 at 12:55 PM, Phi11W said:

Simpler than you might think. 

The foreach loops through the array, giving you the key and value of each item in turn. 

For every item, you want to insert the Table Row construct. 

For the item where the key is "ct", you want to insert some extra "stuff".

 

Try this: 


foreach( $arr as $k => $v ){
   $x .= sprintf( '<tr><td> Td %s </td><td> Td %s</td></tr>', $k, $v ); 
   if ( 'ct' === $k )
   {
      $x .= $Insert_HTML_Here ;
   }
}

Regards, 
   Phill  W.

My like that 

 $details .= trValue(__mu('equivalent') .' <span class="priceValue">',$k,'</span> '.__mu($Currency) , $v, 'NotallFetures','');

Is this the right way to print ?
    

$pricesRow = sprintf( '%s <span class="priceValue">', __mu('equivalent'));                                                                
$details .= trValue($pricesRow,$k,'</span> '.__mu($Currency) , $v, 'NotallFetures','');


                                                                        

Link to comment
Share on other sites

7 hours ago, demohat2017 said:

My like that:
    $details .= ... 
Is this the right way to print ?

From what you've shown?  No idea.
What does the trValue() function do?

Please remember - you are looking at this problem all the time.  We only get to take the most cursory of glances at it during a quiet[er] half hour or over the lunch-hour. 

We are not psychic.  (Some people around here are really Good, but none of us are that Good!) 🙂
We don't know what code you've written since your last posting - for example, this "$details" thing has appeared.

Regards, 
   Phill  W.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.