spires Posted July 30, 2008 Share Posted July 30, 2008 Hi guys Can anyone see why this code is not working correctly? What should happen: on the website (see below) you will see a text area box. If you enter in more than two lines, the the results box should display both line. At present, it is only displaying the last line of test. I am using a foreach to loop through the amount of values in the array. So it should be 2 lines = 2 loops. But its only doing 2 line = 1 loop. Any ideas please? The website is: http://www.adwordstool.co.uk/MT-SK2.php Select 'standard' radio button and any of the check box's function generate_standard_text(&$vars, &$adgroup_array) { $standard_text = ""; // init results string to return if($vars['type'] == "standard"){ foreach ($adgroup_array as $adgroup){ $adgroup = trim($adgroup); $standard_text = "$adgroup\n"; if($vars['Phrase_Match']){ $standard_text1 = ""$adgroup"\n"; } if($vars['Exact_Match']){ $standard_text2 = "[$adgroup]\n"; } if($vars['Negative_Match']){ $standard_text3 = "-$adgroup\n"; } } } return $standard_text.$standard_text1.$standard_text2.$standard_text3; // return results string } Quote Link to comment https://forums.phpfreaks.com/topic/117319-solved-help-with-foreach/ Share on other sites More sharing options...
samshel Posted July 30, 2008 Share Posted July 30, 2008 post code where you are calling this function. Quote Link to comment https://forums.phpfreaks.com/topic/117319-solved-help-with-foreach/#findComment-603463 Share on other sites More sharing options...
spires Posted July 30, 2008 Author Share Posted July 30, 2008 I think the error is in the PHP code. But here is the HTML code: <form method="post" name="form3" action="myexport.php"> <table width="727" height="180" border="0" cellpadding="0" cellspacing="0"> <tr> <td background="GFX/wrapper/Result-box.jpg" align="center"> <table width="707" border="0" cellspacing="0" cellpadding="0"> <tr> <td align="center"> <textarea name="data" cols="90" rows="8" class="smallText" id="results" wrap="off" >{$standard_text}</textarea> </td> </tr> </table> </td> </tr> </table> <br> <table width="727" border="0" cellpadding="0" cellspacing="0"> <tr> <td align="right"> <select name="export_type"> <option value="xls">XLS</option> <option value="csv">CSV</option> </select> <input type="submit" name="submit" value="Export" /> </td> </tr> </table> </form> Hope this makes it more clear Cheers Quote Link to comment https://forums.phpfreaks.com/topic/117319-solved-help-with-foreach/#findComment-603465 Share on other sites More sharing options...
GingerRobot Posted July 30, 2008 Share Posted July 30, 2008 No, samshel was asking where you call the function generate_standard_text(). What we really need to see is where $adground_array is defined. How are you turning the contents of that textbox (a string) to an array? Incidently, why are you passing your variables by reference? Quote Link to comment https://forums.phpfreaks.com/topic/117319-solved-help-with-foreach/#findComment-603469 Share on other sites More sharing options...
spires Posted July 30, 2008 Author Share Posted July 30, 2008 O' Ok does this help $vars = array( // Match types "Negative_Match" => "", "Exact_Match" => "", "Phrase_Match" => "", // Adgroup "ad_group" => "", // Types "type" => "", ); $vars['ad_group'] = sanitize($_POST['ad_group']); if($vars['type'] == "standard") { // generate results string and assign to template $smarty->assign("standard_text", generate_standard_text($vars, $adgroup_array)); } I am using smarty template to separate the PHP and HTML. Quote Link to comment https://forums.phpfreaks.com/topic/117319-solved-help-with-foreach/#findComment-603473 Share on other sites More sharing options...
sasa Posted July 30, 2008 Share Posted July 30, 2008 try <?php function generate_standard_text(&$vars, &$adgroup_array) { $standard_text = ""; // init results string to return if($vars['type'] == "standard"){ foreach ($adgroup_array as $adgroup){ $adgroup = trim($adgroup); $standard_text = "$adgroup\n"; if($vars['Phrase_Match']){ $standard_text1 = ""$adgroup"\n"; } if($vars['Exact_Match']){ $standard_text2 = "[$adgroup]\n"; } if($vars['Negative_Match']){ $standard_text3 = "-$adgroup\n"; } $out .= $standard_text.$standard_text1.$standard_text2.$standard_text3; } } return $out; // return results string } ?> Quote Link to comment https://forums.phpfreaks.com/topic/117319-solved-help-with-foreach/#findComment-603482 Share on other sites More sharing options...
spires Posted July 30, 2008 Author Share Posted July 30, 2008 Also As you can see from the website, if I echo $adgroup."<br>"; You can see in the top lefthand corner That both (if two are entered) of the values are listed. However, they are not listed in textarea Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/117319-solved-help-with-foreach/#findComment-603483 Share on other sites More sharing options...
spires Posted July 30, 2008 Author Share Posted July 30, 2008 That perfect :-) Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/117319-solved-help-with-foreach/#findComment-603486 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.