Arrays, text areas and concatenation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Flashart
Forum Commoner
Posts: 71
Joined: Tue Oct 06, 2009 12:12 pm

Arrays, text areas and concatenation

Post by Flashart »

Hi everyone

I was hoping someone could help me. I am trying to build a simple form that concatenates. The issue I have is trying to get the words entered into an array that then spits out the result into the same page. I have managed to do some simple concatenation with just one or 2 words, but now I am entering the realms of an array and I'm not sure whats next.

Here is my html code

Code: Select all

<!DOCTYPE html>
<head>
<title>Concat 4</title>
<style ="text/css">

form: {width:200px;}
.match_button {width:239px; clear:left; float:left;}
textarea {float:left; clear: right; width:500px; height:540px; margin: 0 0 0 15px;}
</style>
</head>

<div id="left_content">
<form action="concat4.php" method="post">
<input type="submit" name="b_button" class="match_button" value="Broad match"/>
<input type="submit" class="match_button" name="bpe_button" value="Broad, "Phrase" & [Exact] match"/>
<input type="submit" class="match_button" name="bp_button" value="Broad, "Phrase match"/>
<input type="submit" name="p_button" class="match_button" value="Phrase match" /></form>


</div>
<div id="middle_content">
	<textarea name="textarea">
</div>




</html>
and here is my php code

Code: Select all

<?php
$total=$_POST["textarea"]
 
$keyarr=explode("\n",$total);
// In order to  process this  array  values  here is the code 
 
foreach($keyarr  as  $key=>$value)
{
	echo "$all_keywords<br />";
	echo "\"$all_keywords\"<br/>";
	echo "[$all_keywords]<br />";
	}

 



?>
Ideally I want the results to appear in the same text area concatenated with the users choice. At the moment it just goes to a blank page. I am a newb so I am prob missing something important!

Thanks
curlybracket
Forum Commoner
Posts: 59
Joined: Mon Nov 29, 2010 2:40 pm

Re: Arrays, text areas and concatenation

Post by curlybracket »

1. You have an error in PHP code in line 1. You forgot about ; at the end of the line.
2. If you don't get an error message, you should examine your error reporting level. I suggest reading this: http://www.phperrorfix.com/5/i-can-t-se ... r-messages
3. You forgot to close textarea tag.
4. You are closing form tag before texatera, so texatera don't exists in your form.
5. At last, I'm not sure what do you want to do, I fixed your code and simplify it a bit. Read it and maybe this will help you:

Code: Select all

<?php
$total=$_POST["textarea"];
 
$keyarr=explode("\n",$total);
 
foreach($keyarr  as  $key => $value)
{
	echo $value.'<br/>';
}

?>
 <form action="" method="post">
<input type="submit" name="b_button" class="match_button" value="Broad match"/>
<textarea name="textarea"></textarea>
</form>


Flashart
Forum Commoner
Posts: 71
Joined: Tue Oct 06, 2009 12:12 pm

Re: Arrays, text areas and concatenation

Post by Flashart »

Hi Thanks for that.

That's great thanks ever so much for your help. I want to create something like this http://www.jumbokeyword.com

when you enter the keywords, and select one of the buttons to the left it spits out the result into the same window. I need to now somehow send the results to the same place. I can see the jumbokeyword uses Ajax. Presumably this is how they are achieving that? Is it simply a case of putting the foreach statement into the right part of the page?

I really appreciate the help!
curlybracket
Forum Commoner
Posts: 59
Joined: Mon Nov 29, 2010 2:40 pm

Re: Arrays, text areas and concatenation

Post by curlybracket »

I did two sample buttons for you, but inserting the result to textarea may be bit more complicated. Your sample site is using AJAX for that but it is also achiveable only with PHP.

Code: Select all

<?php

if(isset($_POST))
{
	$total=$_POST["textarea"];
	$keyarr=explode("\n",$total);

	foreach($keyarr  as  $key => $value)
	{
		if(isset($_POST['exact_match']))
		{
			echo '['.$value.']<br/>';
		}
		elseif(isset($_POST['phrase_match']))
		{
			echo '"'.$value.'"<br/>';
		}
		else
		{
			echo $value.'<br/>';
		}
			
	}
}

?>
<form action="" method="post">
	<input type="submit" name="phrase_match" class="match_button" value="Phrase match"/>
	<input type="submit" name="exact_match" class="match_button" value="[Exact] match"/>
<textarea name="textarea"></textarea>
</form>

Flashart
Forum Commoner
Posts: 71
Joined: Tue Oct 06, 2009 12:12 pm

Re: Arrays, text areas and concatenation

Post by Flashart »

I have learnt so much just from that! thanks! I have been trying to do that using the define statement!
Flashart
Forum Commoner
Posts: 71
Joined: Tue Oct 06, 2009 12:12 pm

Re: Arrays, text areas and concatenation

Post by Flashart »

Ok I have tried moving on from this towards including another text area for multipliers and concatenating them. However I'm not sure how to construct the php elements.

Code: Select all

<?php
if(isset($_POST))
{
        $total=$_POST["textarea"];
        $keyarr=explode("\n",$total);

       
}

{
	$multiplier_keyword=$_POST["multiplier_textarea"];
	$keyarr=explode("\n",$multiplier_keyword);
	}
?>
<!DOCTYPE html>
<head>

</head>
<body>

<form action="" method="post">
 	<input type="submit" name="broad" class="match_button"
        value="Broad"/>
        <input type="submit" name="phrase" class="match_button" value=""Phrase""/>
        <input type="submit" name="exact" class="match_button" value="[Exact]"/>
        <input type="submit" name="broad_phrase" class="match_button" value="Broad & "Phrase"" />
        <input type="submit" name="broad_exact" class="match_button" value="Broad & [Exact]" />
        <input type="submit" name="broad_phrase_exact" class="match_button" value="Broad & "Phrase&quot & [Exact]" />
         <input type="submit" name="phrase_exact" class="match_button" value=""Phrase&quot & [Exact]" />
   	<!--	<input type="reset" value="Reset" class="match_button" />    -->
   		
   <div id="content"></div></td>
     

<textarea name="textarea"></textarea>

<textarea name="multiplier_textarea"></textarea>
</form>

<div id="results">
 <?php foreach($keyarr  as  $key => $value) and ($keyarr as $key => $multiplier)
        {
                if(isset($_POST['exact']))
                {
                        echo '['.$value.']<br/>';
                }
                elseif(isset($_POST['phrase']))
                {
                        echo '"'.$value.'"<br/>';
                }
                elseif(isset($_POST['broad_phrase']))
                {
                		echo $value.'<br/>';
                		echo '"'.$value.'"<br/>';
                }
                  elseif(isset($_POST['broad_exact']))
                {
                		echo $value.'<br/>';
                		echo '['.$value.']<br/>';
                }
                elseif(isset($_POST['broad_phrase_exact']))
                {
                		echo $value.'<br/>';
                		echo '"'.$value.'"<br/>';
                		echo '['.$value.']<br/>';
                		}
                elseif(isset($_POST['phrase_exact']))
                {
                		echo '"'.$value.'"<br/>';
                		echo '['.$value.']<br/>';
                }
                elseif(isset($_POST['exact'] and ['$multiplier']))
                {
                		echo '['.$value . $multiplier']<br/>';
                		}
             
                else
                {
                        echo $value.'<br/>';
                }
                
                        
        }
        ?>
        </div>
        	
        </body>
        </html>
So for example you were to write in the first box the word new, and in the second box the word clothes, and pressed the exact match button, you would end up with [new clothes]

The issue I have (i think)is sending two lots of isset POST . I'm sure this is quite simple but I can't seem to fathom it out! Any help would be greatly appreciated!

Many thanks.
curlybracket
Forum Commoner
Posts: 59
Joined: Mon Nov 29, 2010 2:40 pm

Re: Arrays, text areas and concatenation

Post by curlybracket »

You can't do it in this way. You have to give different names for your different variables, otherwise the second declaration will overwrite the first. Second thing is that your foreach(...) and (...) will cause Parse error, there is no such thing in PHP. Try this code, it should give you some ideas:

Code: Select all

<form action="" method="post">
	<input type="submit" name="exact" class="match_button" value="[Exact]"/>
	<textarea name="textarea"></textarea>
	<textarea name="multiplier_textarea"></textarea>
</form>

<?php 
if(isset($_POST))
{
	$firstTextarea = $_POST['textarea'];
	$secondTextarea = $_POST['multiplier_textarea'];
	$firstArray = explode("\n", $firstTextarea);
	$secondArray = explode("\n", $secondTextarea);
   
	if(isset($_POST['exact']))
	{
		$counter = 0;
		foreach($firstArray as $key => $value)
		{
			echo '['.$value.' '.$secondArray[$counter].']<br/>';
			$counter++;
		}
	}
}

?>

Flashart
Forum Commoner
Posts: 71
Joined: Tue Oct 06, 2009 12:12 pm

Re: Arrays, text areas and concatenation

Post by Flashart »

Ok I would never have got that!

I will have a stab!
Last edited by Flashart on Tue Nov 30, 2010 9:09 am, edited 1 time in total.
curlybracket
Forum Commoner
Posts: 59
Joined: Mon Nov 29, 2010 2:40 pm

Re: Arrays, text areas and concatenation

Post by curlybracket »

SO in essence, the if statement would read: "if the first box has something and the second box has something, concatenate them depending on the users choice (phrase, exact etc), otherwise just return the first box contents as per the users choice.
This is ok, but handling data may be more complicated problem: how to validate data in textareas: if empty second textarea, if empty first textarea, if different number of lines in both textareas and so on...
Flashart
Forum Commoner
Posts: 71
Joined: Tue Oct 06, 2009 12:12 pm

Re: Arrays, text areas and concatenation

Post by Flashart »

Ok I see what you mean. This is almost there though, I just need to get it so that if i add more than one keyword in the first textarea, the multiplier concatenates against each word. At the moment it adds it to the first one only.

I'm assuming this is to do with the counter variable? Also if no words are added to the second text area the result is [clothes ]. I need to strip that extra space before the closing bracket? I believe this is the trim function, and I have tried this to no avail?

Code: Select all

$firstTextarea = trim($_POST['textarea']);
        $secondTextarea = trim($_POST['multiplier_textarea']);
Flashart
Forum Commoner
Posts: 71
Joined: Tue Oct 06, 2009 12:12 pm

Re: Arrays, text areas and concatenation

Post by Flashart »

Just realised that it probably is stripping it, just the end of the string is after the closing ] whereas I need to strip whitespace before the closing ].

Hmm...would i need to count the characters and the strip the one before the end?
curlybracket
Forum Commoner
Posts: 59
Joined: Mon Nov 29, 2010 2:40 pm

Re: Arrays, text areas and concatenation

Post by curlybracket »

I'm not sure if you mean this:

Code: Select all

<form action="" method="post">
        <input type="submit" name="exact" class="match_button" value="[Exact]"/>
        <textarea name="textarea"></textarea>
        <textarea name="multiplier_textarea"></textarea>
</form>

<?php 
if(isset($_POST['exact']))
{
	$firstTextarea = $_POST['textarea'];
    $secondTextarea = $_POST['multiplier_textarea'];
    $firstArray = explode("\n", $firstTextarea);
    $secondArray = explode("\n", $secondTextarea);
   
    foreach($firstArray as $key => $value)
    {
		foreach($secondArray as $key2 => $value2)
		{
			echo '['.$value.' '.$value2.']<br/>';
		}
	}
}
And to get rid this additional space you may do:

Code: Select all


if(!empty($value2))
			{
				echo '['.$value.' '.$value2.']<br/>';
			}
			else
			{
				echo '['.$value.']<br/>';
			}
Flashart
Forum Commoner
Posts: 71
Joined: Tue Oct 06, 2009 12:12 pm

Re: Arrays, text areas and concatenation

Post by Flashart »

That's brilliant, I can see what this is doing, however now if I try to expand the idea by including phrase match as well it doesn't work.

Code: Select all

if(isset($_POST['exact']))
if(isset($_POST['phrase']))
Also when i put the foreach loop into my results div (so the results don't spit out at the top of the page I get an error and it breaks!

Warning: Invalid argument supplied for foreach() in /Applications/MAMP/htdocs/PPC_Account_Builder/concat7.php on line 49

I really do appreciate your help!
Flashart
Forum Commoner
Posts: 71
Joined: Tue Oct 06, 2009 12:12 pm

Re: Arrays, text areas and concatenation

Post by Flashart »

I think I have it..

I changed the

Code: Select all

if(isset($_POST['exact']))
to read just

Code: Select all

if(isset($_POST)) 


and lower down the page I have

Code: Select all

 foreach($firstArray as $key => $value)
    {
    	  foreach($secondArray as $key2 => $value2)
    	  {
    			if(isset($_POST['exact']))
    				
    				if(!empty($value2))
					            
            	        {
                                echo '['.$value.' '.$value2.']<br/>';
                        }
                        else
                        {
                                echo '['.$value.']<br/>';
                        }
            
                       }
        }
 
I'm guessing I just then repeat this code for the phrase, broad,e tc etc.
curlybracket
Forum Commoner
Posts: 59
Joined: Mon Nov 29, 2010 2:40 pm

Re: Arrays, text areas and concatenation

Post by curlybracket »

1. Yes, you should expand this code for each type of match.
2. I can't tell why isset($_POST['exact']) does not work because I don't see the whole code. Maybe something is wrong in HTML name=""...
3. I also can't tell why your code breaks after moving PHP lower in the code, you may paste whole broken code here.
Post Reply