Jump to content

Foreach help please


spires

Recommended Posts

Hi Guys.

 

I'm trying to join two arrays together. I'm inputing one row of words into one textarea and another row of words into another textarea. Then trying to join the two lots of text tpgether.

 

Example:

textarea 1:

blackberry

black berry

 

textarea 2:

pearl

curve

 

Results:

blackberry pearl

blackberry curve

black berry pearl

black berry curve

 

Here the site so far:

http://www.spirestest.com/Adwords%20tools/extend-keywords.php

 

Here is my code so far:

<form name="form1" method="post" action="<?php echo $PHP_SELF; ?>">
<table width="1100" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><textarea name="keywords" cols="60" rows="8" class="body_text" id="keywords"><?PHP echo stripslashes($_POST['keywords']); ?></textarea></td>
    <td><textarea name="ext_keywords" cols="60" rows="8" class="body_text" id="ext_keywords"><?PHP echo stripslashes($_POST['ext_keywords']); ?></textarea></td>
  </tr>
  <tr>
    <td><input name="submit12" type="submit" value="submit" /></td>
    <td> </td>
  </tr>
</table>
</form>
<BR>
<?php 

		$keywords = stripslashes($_POST['keywords']);
		$keyword_array = explode("\n", $keywords);

		$ext_keywords = stripslashes($_POST['ext_keywords']);
		$ext_keyword_array = explode("\n", $ext_keywords);
		// $keyword = $keyword_array[1];

		foreach ($keyword_array as $keyword) {
    				$keyword = trim($keyword);
		}

		foreach ($ext_keyword_array as $ext_keywords) {
    				$ext_keywords = trim($ext_keywords);
		}

		$key = $keyword.' '.$ext_keywords;	



?>
<form name="form2" method="post" action="<?php echo $PHP_SELF; ?>">
<table width="1100" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><textarea name="key" cols="60" rows="8" class="body_text" id="keywords"><?PHP echo $key; ?></textarea></td>
    <td></td>
  </tr>
  <tr>
    <td><input name="submit12" type="submit" value="submit" /></td>
    <td> </td>
  </tr>
</table>
</form>

 

Thanks for your help

:)

Link to comment
https://forums.phpfreaks.com/topic/96172-foreach-help-please/
Share on other sites

Hi

 

I just try it, but it didn't help.

 

		$keywords = stripslashes($_POST['keywords']);
		$keyword_array = explode("\n", $keywords);

		$ext_keywords = stripslashes($_POST['ext_keywords']);
		$ext_keyword_array = explode("\n", $ext_keywords);

		foreach ($keyword_array as $keywords) {
    			foreach ($ext_keyword_array as $ext_keywords) {
			$keywords = trim($keywords);
    				$ext_keywords = trim($ext_keywords);

			$key = $keywords.' '.$ext_keywords;	
		}
		}

 

Any other ideas please

 

Thanks for your help

Link to comment
https://forums.phpfreaks.com/topic/96172-foreach-help-please/#findComment-492296
Share on other sites

Looking at your code I see quite a few html errors and logic errors

 

Im not 100% how this should work but try

 

<?

$keywords = stripslashes($_POST['keywords']);

$keyword_array = explode("\n", $keywords);

 

$ext_keywords = stripslashes($_POST['ext_keywords']);

$ext_keyword_array = explode("\n", $ext_keywords);

// $keyword = $keyword_array[1];

 

foreach ($keyword_array as $keyword) {

    $keyword = trim($keyword);

}

 

foreach ($ext_keyword_array as $ext_keywords) {

    $ext_keywords = trim($ext_keywords);

}

 

                  foreach ($keyword_array as $keywords) {

    foreach ($ext_keyword_array as $ext_keywords) {

$keywords = trim($keywords);

    $ext_keywords = trim($ext_keywords);

 

$key .= $keywords.' '.$ext_keywords."\n";

}

}

 

 

 

 

 

?>    <form name="form1" method="post" action="<?php echo $PHP_SELF; ?>">

<table width="1100" border="0" cellspacing="0" cellpadding="0">

  <tr>

    <td><textarea name="keywords" cols="60" rows="8" class="body_text" id="keywords"><?PHP echo stripslashes($_POST['keywords']); ?></textarea></td>

    <td><textarea name="ext_keywords" cols="60" rows="8" class="body_text" id="ext_keywords"><?PHP echo stripslashes($_POST['ext_keywords']); ?></textarea></td>

  </tr>

  <tr>

    <td><input name="submit12" type="submit" value="submit" /></td>

    <td> </td>

  </tr>

</table>

 

<table width="1100" border="0" cellspacing="0" cellpadding="0">

  <tr>

    <td><textarea name="key" cols="60" rows="8" class="body_text" id="keywords"><?PHP echo $key; ?></textarea></td>

    <td></td>

  </tr>

  <tr>

    <td><input name="submit12" type="submit" value="submit" /></td>

    <td> </td>

  </tr>

</table>

</form>

<BR>

 

 

Link to comment
https://forums.phpfreaks.com/topic/96172-foreach-help-please/#findComment-492309
Share on other sites

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.