Jump to content

Issue with array and foreach statement!


mikebarbaro

Recommended Posts

Hello,

 

I have the following code which is needed to display all the values of an array by themselves in a list and include it in a fax. The problem is only the first item in the array prints out.

 

So, if I have the following in an array only Chicken Salad would show up in the fax:

 

Chicken Salad

Nachos

Root Beer

 

I need all three to show up just as listed above!

 

Any help is appreciated!

 

$items = $_SESSION['items'];

foreach ($items as $itemname) {
        $finalitems = $itemname . '<br />';

// Combine data to be faxed
$params->Data      = $texttofax . $finalitems;

}

Link to comment
https://forums.phpfreaks.com/topic/190131-issue-with-array-and-foreach-statement/
Share on other sites

$items = $_SESSION['items'];

foreach ($items as $itemname) {
        $finalitems = $itemname . '<br />';

// Combine data to be faxed
$params->Data      = $texttofax . $finalitems;

}

 


$items = $_SESSION['items'];

foreach ($items as $itemname) 
      {
        $finalitems = $itemname . '<br />';
      }   // <-----MOVED / ADDED A BRACKET

// Combine data to be faxed <---- DO THIS AFTER BRACKET MAYBE?!?!  Else as pointed out you are resetting your data.
$params->Data      = $texttofax . $finalitems;

$items = $_SESSION['items'];
foreach ($items as $itemname) 
      {
        $finalitems = $itemname . '<br />';
      }   // <-----MOVED / ADDED A BRACKET
$params->Data      = $texttofax . $finalitems;

 

That is still not quite right. You need to concatenate the data inside the loop.

 

$items = $_SESSION['items'];
foreach ($items as $itemname) {
        $finalitems .= $itemname . '<br />'; //changed this line
}
// Combine data to be faxed
$params->Data = $texttofax . $finalitems;

$items = $_SESSION['items'];
foreach ($items as $itemname) 
      {
        $finalitems = $itemname . '<br />';
      }   // <-----MOVED / ADDED A BRACKET
$params->Data      = $texttofax . $finalitems;

 

That is still not quite right. You need to concatenate the data inside the loop.

 

$items = $_SESSION['items'];
foreach ($items as $itemname) {
        $finalitems .= $itemname . '<br />'; //changed this line
}
// Combine data to be faxed
$params->Data = $texttofax . $finalitems;

 

Worked perfectly! Thank you very much!

Archived

This topic is now archived and is closed to further replies.

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