spires Posted April 22, 2008 Share Posted April 22, 2008 Hi Guys can someone help with out putting an array. I have two textarea fields, 'Keywords' & 'code'. Both fields can have multiple line of text. I the need the contents of both fields to be outputted in one text area. Example: keyword - apples pear Code - 1 2 I need it to output: apples 1 pear 1 BUT, it's outputting: apples 1 apples 2 pear 1 pear 2 Here is the URL: http://freeadwordsebook.com/test.php Here is my code <form name="from11" method="post" action"tracker.php"> <input name="type" type="hidden" value="upload"> <table width="600" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="300">Keywords:<br> <textarea name="keywords" cols="25" rows="8" class="body_text" id="keywords"><?PHP echo stripslashes($_POST['keywords']); ?></textarea> </td> <td width="300">Code:<br> <textarea name="code" cols="25" rows="8" class="body_text" id="code"><?PHP echo stripslashes($_POST['code']); ?></textarea> </td> </tr> <tr> <td> <input name="submit11" value="submit" type="submit"> </td> </tr> </table> </form> <br><br> <textarea name="results" cols="50" rows="8"> <?PHP if (isset($_POST['submit11'])){ $keywords = stripslashes($_POST['keywords']); $key_array = explode("\n", $keywords); $code = stripslashes($_POST['code']); $code_array = explode("\n", $code); // --------------------------------------------- foreach ($key_array as $keyword) { foreach ($code_array as $cd) { $keyword = trim($keyword); $cd = trim($cd); echo $keyword.$cd."\r\n"; } } } ?> </textarea> Thanks for any help. Link to comment https://forums.phpfreaks.com/topic/102300-how-to-display-an-array/ Share on other sites More sharing options...
zenag Posted April 22, 2008 Share Posted April 22, 2008 just change ... foreach ($key_array as $keyword) { foreach ($code_array as $cd) { as............... foreach ($code_array as $cd) { foreach ($key_array as $keyword) { Link to comment https://forums.phpfreaks.com/topic/102300-how-to-display-an-array/#findComment-523809 Share on other sites More sharing options...
spires Posted April 22, 2008 Author Share Posted April 22, 2008 Sorry, It needs to output: apples 1 pear 2 Not It needs to output: apples 1 pear 1 Hope this makes it clearer Link to comment https://forums.phpfreaks.com/topic/102300-how-to-display-an-array/#findComment-523811 Share on other sites More sharing options...
spires Posted April 22, 2008 Author Share Posted April 22, 2008 Hi zenag I reversed it just like you said. But i'm still getting the extra values that I don't want. I only want: apples 1 plum 2 Not the full: apples1 plum1 apples2 plum2 Any more help would be great. Link to comment https://forums.phpfreaks.com/topic/102300-how-to-display-an-array/#findComment-523813 Share on other sites More sharing options...
rajivgonsalves Posted April 22, 2008 Share Posted April 22, 2008 change your foreach to a for loop for ($i=0;$i<count($code_array);$i++) { echo $key_array[$i]." ".$code_array[$i]; } hopes its helpful Link to comment https://forums.phpfreaks.com/topic/102300-how-to-display-an-array/#findComment-523821 Share on other sites More sharing options...
spires Posted April 22, 2008 Author Share Posted April 22, 2008 Hi rajivgonsalves I see what you trying to do, and that is exactly on the right lines. But now i'm just getting 'A A' as the output results. As you can see here: http://freeadwordsebook.com/test.php New Code <form name="from11" method="post" action"tracker.php"> <input name="type" type="hidden" value="upload"> <table width="600" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="300">Keywords:<br> <textarea name="keywords" cols="25" rows="8" class="body_text" id="keywords"><?PHP echo stripslashes($_POST['keywords']); ?></textarea> </td> <td width="300">Code:<br> <textarea name="code" cols="25" rows="8" class="body_text" id="code"><?PHP echo stripslashes($_POST['code']); ?></textarea> </td> </tr> <tr> <td> <input name="submit11" value="submit" type="submit"> </td> </tr> </table> </form> <br><br> <textarea name="results" cols="50" rows="8"> <?PHP if (isset($_POST['submit11'])){ $keywords = stripslashes($_POST['keywords']); $key_array = explode("\n", $keywords); $code = stripslashes($_POST['code']); $code_array = explode("\n", $code); // --------------------------------------------- for ($i=0;$i<count($code_array);$i++){ $key_array = trim($key_array); $code_array = trim($code_array); echo $key_array[$i]." ".$code_array[$i]; } } ?> </textarea> Link to comment https://forums.phpfreaks.com/topic/102300-how-to-display-an-array/#findComment-523825 Share on other sites More sharing options...
spires Posted April 22, 2008 Author Share Posted April 22, 2008 OK, First, thanks for all your help. I have now got rid of the A A, but now there is one last problem. The text is outputting like this: apples 1 plum 2 banana 3 It shouls be like this: apples 1 plum 2 banana 3 Any more help please. :-) <?PHP if (isset($_POST['submit11'])){ $keywords = stripslashes($_POST['keywords']); $key_array = explode("\n", $keywords); $code = stripslashes($_POST['code']); $code_array = explode("\n", $code); // --------------------------------------------- for ($i=0;$i<count($code_array);$i++){ echo $key_array[$i]." ".$code_array[$i]; } } ?> Link to comment https://forums.phpfreaks.com/topic/102300-how-to-display-an-array/#findComment-523830 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.