• 0

Pretty basic PHP syntax problem - foreach inside variable?


Question

Hello, sorry for the vague title. I dont know what to call my problem, but im pretty sure its rather basic.

Basically, im trying to email off the results of a stock search and im not sure of the syntax to include a foreach loop inside the message body. For example:

			// Message
			$body = '
			<html>
			<head>
				<title>Stock Enquiry</title>	
				<style type="text/css">
					body {
					font-family:"Trebuchet MS", Tahoma, Verdana, Arial, Helvetica, sans-serif;
					color:#333;
					}
				</style>
			</head>

			<body>


				<span>You have received a stock enquiry.</span><br />
				<span>The message was sent: <i>'.$date.'</i></span>

				<p><b>Contact Details:</b></p>
				<span>Title: <i>'.$title.'</i></span><br />
				<span>First Name: <i>'.$first_name.'</i></span><br />
				<span>Last Name: <i>'.$last_name.'</i></span><br />
				<span>Email: <i>'.$email.'</i></span><br />			

				<br />

				<p><b>Company Details:</b></p>
				<span>Company Name: <i>'.$company_name.'</i></span><br />
				<span>Country: <i><a href="http://www.iso.org/iso/country_codes/iso_3166_code_lists/english_country_names_and_code_elements.htm">'.$country.'<a></i></span><br />
				<span>Telephone Number: <i>'.$telephone.'</i></span><br />
				<span>Comments: <i>'.$comments.'</i></span><br />

				<br />

				<p><b>Request for Quotation:</b></p>
				<span>RFQs chosen: <i>'.$number_parts.'</i></span><br />

				<br />

				<table border="1" align="center">
					<tr>
						<th>Part Number</th>
						<th>Manufacturer</th>
						<th>Stock</th>
						<th>Quantity</th>
						<th>Delivery</th>
					</tr>


		 ' .	
				// Foreach of the parts selected (the id array), select the value corresponding to each key
				foreach($_POST['id'] as $key=>$value){

					// Using the same key (id value), select the corresponding values from each of the arrays
					$part_number_value = $_POST['part-number'][$key];
					$manufacturer_value = $_POST['manufacturer'][$key];
					$stock_value = $_POST['stock'][$key];
					$quantity_value = $_POST['quantity'][$key];
					$delivery_value = $_POST['delivery'][$key];
 	. '

					<tr>
						<td>' . $part_number_value . '</td>
						<td>' . $manufacturer_value . '</td>
						<td>' . $stock_value . '</td>
						<td>' . $quantity_value . '</td>
						<td>' . $delivery_value . '</td>
					</tr>
				}

			</table>
			<br /><br />


			</body>

			</html>
			';

Obviously isnt right and im banging my head against the wall becuase i dont know how to fix it even though i know this is a basic problem.

Cheers for the help

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Hello, sorry for the vague title. I dont know what to call my problem, but im pretty sure its rather basic.

Basically, im trying to email off the results of a stock search and im not sure of the syntax to include a foreach loop inside the message body. For example:

			// Message
			$body = '
			<html>
			<head>
				<title>Stock Enquiry</title>	
				<style type="text/css">
					body {
					font-family:"Trebuchet MS", Tahoma, Verdana, Arial, Helvetica, sans-serif;
					color:#333;
					}
				</style>
			</head>

			<body>


				<span>You have received a stock enquiry.</span><br />
				<span>The message was sent: <i>'.$date.'</i></span>

				<p><b>Contact Details:</b></p>
				<span>Title: <i>'.$title.'</i></span><br />
				<span>First Name: <i>'.$first_name.'</i></span><br />
				<span>Last Name: <i>'.$last_name.'</i></span><br />
				<span>Email: <i>'.$email.'</i></span><br />			

				<br />

				<p><b>Company Details:</b></p>
				<span>Company Name: <i>'.$company_name.'</i></span><br />
				<span>Country: <i><a href="http://www.iso.org/iso/country_codes/iso_3166_code_lists/english_country_names_and_code_elements.htm">'.$country.'<a></i></span><br />
				<span>Telephone Number: <i>'.$telephone.'</i></span><br />
				<span>Comments: <i>'.$comments.'</i></span><br />

				<br />

				<p><b>Request for Quotation:</b></p>
				<span>RFQs chosen: <i>'.$number_parts.'</i></span><br />

				<br />

				<table border="1" align="center">
					<tr>
						<th>Part Number</th>
						<th>Manufacturer</th>
						<th>Stock</th>
						<th>Quantity</th>
						<th>Delivery</th>
					</tr>


		 ' .	
				// Foreach of the parts selected (the id array), select the value corresponding to each key
				foreach($_POST['id'] as $key=>$value){

					// Using the same key (id value), select the corresponding values from each of the arrays
					$part_number_value = $_POST['part-number'][$key];
					$manufacturer_value = $_POST['manufacturer'][$key];
					$stock_value = $_POST['stock'][$key];
					$quantity_value = $_POST['quantity'][$key];
					$delivery_value = $_POST['delivery'][$key];
 	. '

					<tr>
						<td>' . $part_number_value . '</td>
						<td>' . $manufacturer_value . '</td>
						<td>' . $stock_value . '</td>
						<td>' . $quantity_value . '</td>
						<td>' . $delivery_value . '</td>
					</tr>
				}

			</table>
			<br /><br />


			</body>

			</html>
			';

Obviously isnt right and im banging my head against the wall becuase i dont know how to fix it even though i know this is a basic problem.

Cheers for the help

			// Message
			$body = '
			<html>
			<head>
				<title>Stock Enquiry</title>	
				<style type="text/css">
					body {
					font-family:"Trebuchet MS", Tahoma, Verdana, Arial, Helvetica, sans-serif;
					color:#333;
					}
				</style>
			</head>

			<body>


				<span>You have received a stock enquiry.</span><br />
				<span>The message was sent: <i>'.$date.'</i></span>

				<p><b>Contact Details:</b></p>
				<span>Title: <i>'.$title.'</i></span><br />
				<span>First Name: <i>'.$first_name.'</i></span><br />
				<span>Last Name: <i>'.$last_name.'</i></span><br />
				<span>Email: <i>'.$email.'</i></span><br />			

				<br />

				<p><b>Company Details:</b></p>
				<span>Company Name: <i>'.$company_name.'</i></span><br />
				<span>Country: <i><a href="http://www.iso.org/iso/country_codes/iso_3166_code_lists/english_country_names_and_code_elements.htm">'.$country.'<a></i></span><br />
				<span>Telephone Number: <i>'.$telephone.'</i></span><br />
				<span>Comments: <i>'.$comments.'</i></span><br />

				<br />

				<p><b>Request for Quotation:</b></p>
				<span>RFQs chosen: <i>'.$number_parts.'</i></span><br />

				<br />

				<table border="1" align="center">
					<tr>
						<th>Part Number</th>
						<th>Manufacturer</th>
						<th>Stock</th>
						<th>Quantity</th>
						<th>Delivery</th>
					</tr> ';



				// Foreach of the parts selected (the id array), select the value corresponding to each key
				foreach($_POST['id'] as $key=>$value){

					// Using the same key (id value), select the corresponding values from each of the arrays
					$part_number_value = $_POST['part-number'][$key];
					$manufacturer_value = $_POST['manufacturer'][$key];
					$stock_value = $_POST['stock'][$key];
					$quantity_value = $_POST['quantity'][$key];
					$delivery_value = $_POST['delivery'][$key];

					$body .='<tr>
						<td>' . $part_number_value . '</td>
						<td>' . $manufacturer_value . '</td>
						<td>' . $stock_value . '</td>
						<td>' . $quantity_value . '</td>
						<td>' . $delivery_value . '</td>
					</tr>';
				}

			$body .= '</table>
			<br /><br />


			</body>

			</html>
			';

that should work, dont have time atm to look at it full tho.

Link to comment
Share on other sites

  • 0

[...]

that should work, dont have time atm to look at it full tho.

If you're not sure what just happened there, he split your variable into sections and separated the function from it.

By adding a period before the equals sign (.=) it adds to the variable instead of re-writing it completely.

E.g.

$body = "Hello";

$body .= " World!";

//returns "Hello World!"

echo $body;

Link to comment
Share on other sites

  • 0

If you're not sure what just happened there, he split your variable into sections and separated the function from it.

By adding a period before the equals sign (.=) it adds to the variable instead of re-writing it completely.

E.g.

$body = "Hello";

$body .= " World!";

//returns "Hello World!"

echo $body;

cheers ;p

np

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.