demohat2017 Posted June 2, 2020 Share Posted June 2, 2020 <?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? Quote Link to comment https://forums.phpfreaks.com/topic/310882-how-insert-div-inside-foreach-loop-with-return/ Share on other sites More sharing options...
requinix Posted June 2, 2020 Share Posted June 2, 2020 Try explaining what your problem is using paragraphs and complete sentences. Quote Link to comment https://forums.phpfreaks.com/topic/310882-how-insert-div-inside-foreach-loop-with-return/#findComment-1578595 Share on other sites More sharing options...
Phi11W Posted June 2, 2020 Share Posted June 2, 2020 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/310882-how-insert-div-inside-foreach-loop-with-return/#findComment-1578596 Share on other sites More sharing options...
demohat2017 Posted June 4, 2020 Author Share Posted June 4, 2020 Thanks, although I had tried this solution and didn't worked but your code send magic to me because it wasn't working because html tags change the order of table Quote Link to comment https://forums.phpfreaks.com/topic/310882-how-insert-div-inside-foreach-loop-with-return/#findComment-1578618 Share on other sites More sharing options...
demohat2017 Posted June 4, 2020 Author Share Posted June 4, 2020 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',''); Quote Link to comment https://forums.phpfreaks.com/topic/310882-how-insert-div-inside-foreach-loop-with-return/#findComment-1578622 Share on other sites More sharing options...
Phi11W Posted June 4, 2020 Share Posted June 4, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310882-how-insert-div-inside-foreach-loop-with-return/#findComment-1578627 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.