PHP [RESOLVED] foreach loop
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: [RESOLVED] foreach loop

  1. #1
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,098

    [RESOLVED] foreach loop

    Hello. I have this loop :

    Code:
    	foreach ($_POST['ordered'] as $key=>$id) {
    		echo '<p/>
    		ProdName: ' , $prod[$id]['name'] , '<br/>
    		ProdPrice: ' , $prod[$id]['price'] , '<br/>
    		ProdQty: ' , $prod[$id]['qty'] , '
    		<hr/>';
      }
    Now I am trying to create a string variable to hold all this results, so, the variable must get appended the whole time, and then stored inside a Hidden field. Simple. NO!

    I have tried this :
    Code:
    	$strOrder = "";
    	
    	foreach ($_POST['ordered'] as $key=>$id) {
    		echo '<p/>
    		ProdName: ' , $prod[$id]['name'] , '<br/>
    		ProdPrice: ' , $prod[$id]['price'] , '<br/>
    		ProdQty: ' , $prod[$id]['qty'] , '
    		<hr/>';
    		
    		$strOrder .= $key . " " . $id;
    Nothing gets stored inside strOrder.

    I created the hidden fields like this :

    Code:
    		$TOT2 = $TOT + 55;
    		
    		echo	'<input type="hidden" Name="TOT" value ="'. $TOT .'">';
    		echo	'<input type="hidden" name="TOT2" value =". $TOT2 .'">';
    		
    		echo 	'<input type="hidden" name="Order" value=". $strOrder . '">';
    But NOTHING gets stored in any of them either.....

    What am I doing wrong now??

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,098

    Re: foreach loop

    OK, progress :

    Code:
    	$strOrder = "";
    	
    	foreach ($_POST['ordered'] as $key=>$id) {
    		echo '<p/>
    		ProdName: ' , $prod[$id]['name'] , '<br/>
    		ProdPrice: ' , $prod[$id]['price'] , '<br/>
    		ProdQty: ' , $prod[$id]['qty'] , '
    		<hr/>';
    		
    		$strOrder .= $key . "," . $id.";";
    		
    	
    		$TOT += $prod[$id]['price'] * $prod[$id]['qty'];
    		
    	}
    	
    		$TOT2 = $TOT + 55;
    		
    		echo	'<input type="hidden" Name="TOTAL" value ="'. $TOT .'">';
    		echo	'<input type="hidden" name="TOTAL2" value ="'. $TOT2 .'">';
    		
    		echo 	'<input type="hidden" name="Order" value="'. $strOrder . '">';
    }
    	echo "strOrder String == $strOrder";
    Gives me :

    Code:
    strOrder String == 0,2;1,11;2,20;
    It seems as if the array indexes are stored into strOrder in stead of those indexes' values. I need the values inside the Order hidden field

  3. #3
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,881

    Re: foreach loop

    You're making this way too difficult on yourself. Use PHP's built-in implode() method for making strings out of arrays.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,098

    Re: foreach loop

    Thanx Peej, but I got it working.

    I AM SO STUPID!! It was so simple!

    Code:
    	foreach ($_POST['ordered'] as $key=>$id) {
    		echo '<p/>
    		ProdName: ' , $prod[$id]['name'] , '<br/>
    		ProdPrice: ' , $prod[$id]['price'] , '<br/>
    		ProdQty: ' , $prod[$id]['qty'] , '
    		<hr/>';
    		
    		$strOrder2 .= "<p/>ProdName: " .  $prod[$id]['name'] . "<br/>ProdPrice: " . $prod[$id]['price'] . "ProdQty: " . $prod[$id]['qty'];
    		
    		
    		$TOT += $prod[$id]['price'] * $prod[$id]['qty'];
    		
    	}
    
    
    		$TOT2 = $TOT + 55;
    		
    		echo	'<input type="hidden" Name="TOTALH" value ="'. $TOT .'">';
    		echo	'<input type="hidden" name="TOTAL2H" value ="'. $TOT2 .'">';		
    		echo 	'<input type="hidden" name="Order" value="'. $strOrder2 . '">';
    Thanx anyways!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center