mraza Posted November 29, 2009 Share Posted November 29, 2009 Hi , i have this code and i am trying to change the variable untill the loop run but can't figure out how, means in function ($var1, $vart2) when the next loop come it use the value $var3 and $var4 and in next use again $var1 and $var2 till the for loop finish it will continue this way, here the code: $var1 = $_POST['note1']; $var2 = $_POST['com1']; $var3 = $_POST['note2']; $var4 = $_POST['com2']; for($i= 0;$i < sizeof($somevar);$i++) { //my other statements function ($var1, $vart2); } i tried this way but it will give me the result only of $var3 and $var4 : $var1 = $_POST['note1']; $var2 = $_POST['com1']; $var3 = $_POST['note2']; $var4 = $_POST['com2']; $note = array(1=>"$var1", "$var3 " ); $com = array(1=>"$var2", "$var4 " ); $random = rand(1,2); $var1 = $note["$random"]; $var2 = $com["$random"]; for($i= 0;$i < sizeof($somevar);$i++) { //my other statements function ($var1, $vart2); } Please any help Quote Link to comment https://forums.phpfreaks.com/topic/183305-loop-in-statment/ Share on other sites More sharing options...
Alex Posted November 29, 2009 Share Posted November 29, 2009 You could do something like this: $arr = Array( $_POST['note1'], $_POST['com1'], $_POST['note2'], $_POST['com2'] ); for($i = 0, $c = count($arr);$i < $c;$i+=2) { something($arr[$i], $arr[$i+1]); } Quote Link to comment https://forums.phpfreaks.com/topic/183305-loop-in-statment/#findComment-967566 Share on other sites More sharing options...
mraza Posted November 29, 2009 Author Share Posted November 29, 2009 Thanks sir it worked but now the problem is they are giving both same result, means if i write something in file with $_POST['note1'] it will also be written with $_POST['note2'] and then move to next and same again whatever is in $_POST['note1'] it will be written by $_POST['note2'], if you see there $content, it is being written twice, what i want is as in my first post i used rand() so when the loop run again it will change the value of $var1 and $var2 .here i tried the code maybe something i am missing for sure : $arr = Array( $_POST['note1'], $_POST['com1'], $_POST['note2'], $_POST['com2'] ); for($a= 0;$a< sizeof($somevar);$a++) { //my other statments for content for($i = 0, $c = count($arr);$i < $c;$i+=2) { somefunction($content,$arr[$i], $arr[$i+1]); } } Thanks for help Quote Link to comment https://forums.phpfreaks.com/topic/183305-loop-in-statment/#findComment-967582 Share on other sites More sharing options...
mraza Posted November 29, 2009 Author Share Posted November 29, 2009 sorry for double post i could not edit about if you notice in somefunction($content,$arr[$i], $arr[$i+1]); i have $content, so when the loop runs second time it prints $content again, i wants to move it to next as i am using in script so only when the function run next time it change the value of $var1 and $var2 in somefunction ($content, $var1, $var2); Quote Link to comment https://forums.phpfreaks.com/topic/183305-loop-in-statment/#findComment-967588 Share on other sites More sharing options...
Alex Posted November 29, 2009 Share Posted November 29, 2009 I'm not sure why you have that second for statement, or what you're trying to do at all. Can you post your entire code and explain what you're trying to do a little better? Quote Link to comment https://forums.phpfreaks.com/topic/183305-loop-in-statment/#findComment-967597 Share on other sites More sharing options...
mraza Posted November 29, 2009 Author Share Posted November 29, 2009 sorry sir for confusion i will try to explain here is the original code, codes file are large so i am posting what is important: $user = $_POST['user']; $pass = $_POST['pass']; $user2= $_POST['user2']; $pass2 = $_POST['pass2']; for($i= 0;$i < sizeof($links);$i++) { echo "On Page". $body . "<br />" ; $curl = $turl[0].$links[$i]["href"]; $lcontent = getcontent($curl); $content = getthreadcontent($lcontent); getting($title,$content,$web,$user,$pass); } here what i want is when next time loop run it will change the value of $user and $pass to $user2 and $pass2, so basically what i am trying to do is post with other username next content. Quote Link to comment https://forums.phpfreaks.com/topic/183305-loop-in-statment/#findComment-967600 Share on other sites More sharing options...
Alex Posted November 29, 2009 Share Posted November 29, 2009 What about something like this? $arr = Array( Array( $_POST['note1'], $_POST['com1'] ), Array( $_POST['note2'], $_POST['com2'] ) ); for($i = 0, $c = count($arr);$i < $c;$i++) { $curl = $turl[0].$links[$i]["href"]; $lcontent = getcontent($curl); $content = getthreadcontent($lcontent); something($content, $arr[$i][0], $arr[$i][1]); } As long as you're excluding content it's hard for me to understand exactly what you're trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/183305-loop-in-statment/#findComment-967606 Share on other sites More sharing options...
mraza Posted November 29, 2009 Author Share Posted November 29, 2009 thanks sir for you quick support much appreciated, i want to run this loop for($i= 0;$i < sizeof($links);$i++) as you can see in my previous post so until $links will finish loop will run but $user and $pass will change randomly only to that both variables i mention above when next time loop runs. Quote Link to comment https://forums.phpfreaks.com/topic/183305-loop-in-statment/#findComment-967614 Share on other sites More sharing options...
Alex Posted November 29, 2009 Share Posted November 29, 2009 You want to loop through the entire $links array and randomly choose the user/pass combination? Then do something like this: $arr = Array( Array( $_POST['note1'], $_POST['com1'] ), Array( $_POST['note2'], $_POST['com2'] ) ); for($i = 0, $c = count($links);$i < $c;$i++) { $curl = $turl[0].$links[$i]["href"]; $lcontent = getcontent($curl); $content = getthreadcontent($lcontent); $rand = array_rand($arr); something($content, $arr[$rand][0], $arr[$rand][1]); } Quote Link to comment https://forums.phpfreaks.com/topic/183305-loop-in-statment/#findComment-967618 Share on other sites More sharing options...
Zane Posted November 29, 2009 Share Posted November 29, 2009 Not tested but might work. $var1 = $_POST['note1']; $var2 = $_POST['com1']; $var3 = $_POST['note2']; $var4 = $_POST['com2']; $x = 1; for($i= 0;$i $x = ($x someFunction(${'var'.$x}, ${'var'.$x++}); $x++; } Quote Link to comment https://forums.phpfreaks.com/topic/183305-loop-in-statment/#findComment-967622 Share on other sites More sharing options...
mraza Posted November 29, 2009 Author Share Posted November 29, 2009 EXACTLY THAT sir AlexWD that was i looking Thank you so much it wok like a charm Edit: @Zanus i just saw your method will test now... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/183305-loop-in-statment/#findComment-967625 Share on other sites More sharing options...
Alex Posted November 29, 2009 Share Posted November 29, 2009 That won't work. It'll give output like: first iteration: name1, name1; second iteration name2, name2; third name1, name1, etc.. I think you mean something like this: $x = 1; for($i= 0;$i < 4;$i++) { $x = ($x < 4) ? $x : ($x = 1); someFunction(${'var'.$x}, ${'var'.++$x}); $x++; } If you're going to do it that way might as well put $x inside the for()'s parameters to make it cleaner (like below). But I was under the impression that the OP wanted it to be random. for($i= 0, $x = 1;$i < sizeof($somevar);$i++, $x = (++$x < 4) ? $x : 1) someFunction(${'var'.$x}, ${'var'.++$x}); Quote Link to comment https://forums.phpfreaks.com/topic/183305-loop-in-statment/#findComment-967627 Share on other sites More sharing options...
mraza Posted November 29, 2009 Author Share Posted November 29, 2009 Thanks sir you are right the last one too works, one last question regarding to this what if i have more array like (I am using the last script by your sir Alex with array_rand): $user = $_POST['user']; $pass = $_POST['pass']; $user2= $_POST['user2']; $pass2 = $_POST['pass2']; $user3 = $_POST['user3']; $pass3 = $_POST['pass3']; $user4= $_POST['user4']; $pass4 = $_POST['pass4']; //shoud i do this $arr = Array( Array($tuser1, $tpass1), Array($tuser2, $tpass2 ), Array($tuser3, $tpass3), Array($tuser4, $tpass4 ) ); THANKS ALOTSSSS Quote Link to comment https://forums.phpfreaks.com/topic/183305-loop-in-statment/#findComment-967635 Share on other sites More sharing options...
Alex Posted November 29, 2009 Share Posted November 29, 2009 You're misspelling some of the variables in the array, but yea that's the general idea. Quote Link to comment https://forums.phpfreaks.com/topic/183305-loop-in-statment/#findComment-967638 Share on other sites More sharing options...
mraza Posted November 29, 2009 Author Share Posted November 29, 2009 Thanks you Sir all done.. Quote Link to comment https://forums.phpfreaks.com/topic/183305-loop-in-statment/#findComment-967653 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.