Jump to content

Can you print out and call variables from foreach?


Kane250

Recommended Posts

Hi there.  I am trying to do something fairly simple, but cannot get it.

 

I have 4 textareas that have 4 corresponding tables in MySQL.  I'm using explode to split the paragraph into sentences.  Then I want foreach to take that array and insert each sentence into the appropriate table.

 

I've been trying to write something here that would be flexible so I didn't have a ton of code, but the foreach is printing out my variables names as $variable, rather than calling them and returning each sentence in the array.

 

I know this code doesnt do the actual insert..im just trying to prepare the statements.

 

Can someone help me out? I'm completely stumped!

 

<?php



$para1content = ($_POST['firstParaText']);
$para2content = ($_POST['secondParaText']);
$para3content = ($_POST['thirdParaText']);
$para4content = ($_POST['fourthParaText']);

$allpara = array("para1", "para2", "para3", "para4");


$para1exploded = (explode('.', $para1content));
$para2exploded = (explode('.', $para2content));
$para3exploded = (explode('.', $para3content));
$para4exploded = (explode('.', $para4content));


$contentmarker = array('$para1exploded', '$para2exploded', '$para3exploded', '$para4exploded');



foreach ($allpara as $value) //This returns the name of the table to insert to
{
	foreach ($contentmarker as $content) //This iterates through the exploded array
	{
		$sample_query = "INSERT INTO" . " " . $value . " (text, updated_at) VALUES ( "  . $content . ", NOW())";
  		print $sample_query . "<br />";
};
};

?>

 

$contentmarker = array('$para1exploded', '$para2exploded', '$para3exploded', '$para4exploded');

 

Single quotes do not execute variables and such.

 

$contentmarker = array($para1exploded, $para2exploded, $para3exploded, $para4exploded);

or

$contentmarker = array("$para1exploded", "$para2exploded", "$para3exploded", "$para4exploded");

 

Thanks for the reply.  The problem is that I don't want the variables to be executed until they are placed into the statement.  If I remove the single quotes, the variable is being executed earlier and doesn't seem to be working properly.  You know what I mean?

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.