redarrow Posted August 12, 2006 Share Posted August 12, 2006 I am trying to get the rand number to come out in the for loop as 10 new round numbers can you help please cheers.please give an example and exsplain the example cheers.[code]<?php$a=rand(1,5000);for($i=0; $i<9; $i++){echo "$a<br>";}?>[/code]The result but not as mixed numbers.[code]496249624962496249624962496249624962[/code]this is the long way that the above should do[code]<?php$a=rand(1,5000);$b=rand(1,5000);$c=rand(1,5000);$d=rand(1,5000);$e=rand(1,5000);$f=rand(1,5000);$g=rand(1,5000);$h=rand(1,5000);$i=rand(1,5000);$j=rand(1,5000);echo " $a<br> $b<br> $c<br> $d<br> $e<br> $f<br> $g<br> $h<br> $i<br> $j<br>";?>[/code]result but should work as first code above with the result of 10 new numbers.[code]40362456484021381618425044291774425526[/code] Quote Link to comment https://forums.phpfreaks.com/topic/17327-solved-fun-with-fwrite-mkdir-unlink-foreach-rmdir-thanks-all/ Share on other sites More sharing options...
Orio Posted August 12, 2006 Share Posted August 12, 2006 Why not:[code]<?php//incase you want to know the random number for laterfor($i=0; $i<9; $i++){$rand[$i]=rand(1,5000);echo $rand[$i]."<br>";}//incase you just want to echo random numbersfor($i=0; $i<9; $i++){echo(rand(1,5000));}?>[/code]Orio. Quote Link to comment https://forums.phpfreaks.com/topic/17327-solved-fun-with-fwrite-mkdir-unlink-foreach-rmdir-thanks-all/#findComment-73644 Share on other sites More sharing options...
redarrow Posted August 12, 2006 Author Share Posted August 12, 2006 thank you so much why can this not make all the 10 directory cheers.makes the first dir but not the other 10 in thedir[code]<?phpfor($i=0; $i<9; $i++){$rand[$i]=rand(1,5000);$dir=$rand[$i]."<br>";$a=mkdir(thedir);mkdir("thedir/$dir");}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/17327-solved-fun-with-fwrite-mkdir-unlink-foreach-rmdir-thanks-all/#findComment-73653 Share on other sites More sharing options...
wildteen88 Posted August 12, 2006 Share Posted August 12, 2006 Well the problem you are executing this line:$a=mkdir(thedir);each time. Now when PHP goes through the loop the first time it creates the directory thedir, but the secound time it goes to create it again. But it cant as thedir already exists. So move [code=php:0]$a=mkdir(thedir);[/code] out of the for loop and place it at the top.Also remove [code=php:0]."<br>"[/code] from this [code=php:0]$dir=$rand[$i]."<br>";[/code]Now your code should now work. This is what you code should loook like:[code=php:0]<?php// we create the directory firstmkdir('thedir');for($i = 0; $i < 9; $i++){ $rand[$i] = rand(1, 5000); $dir = $rand[$i]; mkdir("thedir/$dir"); echo "Created thedir/" . $dir . "<br />\n";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/17327-solved-fun-with-fwrite-mkdir-unlink-foreach-rmdir-thanks-all/#findComment-73655 Share on other sites More sharing options...
redarrow Posted August 12, 2006 Author Share Posted August 12, 2006 thank you wild teen learning getting thereknow if i wanted to put the dir into a array i use blob then if i wanted to fwrite into the dir chose via array blob how cheers mate. Quote Link to comment https://forums.phpfreaks.com/topic/17327-solved-fun-with-fwrite-mkdir-unlink-foreach-rmdir-thanks-all/#findComment-73658 Share on other sites More sharing options...
redarrow Posted August 12, 2006 Author Share Posted August 12, 2006 Is this delete format wrong cheers.[code]<?php$dirname="thedir";$del=array("a","b","c");foreach($del AS $deleted){unlink("$dirname/$deleted");}unlink("thedir");<?[/code] Quote Link to comment https://forums.phpfreaks.com/topic/17327-solved-fun-with-fwrite-mkdir-unlink-foreach-rmdir-thanks-all/#findComment-73673 Share on other sites More sharing options...
redarrow Posted August 12, 2006 Author Share Posted August 12, 2006 This was for learning the file statements for fun.i used fwriteunlinkarrayfile_existsforeachunlinkall the code does is delete any file that was test.txt then deletes any dir a.b.c then creates a dir a,b,c then writed to the dir then prints what its done good fun.[code]<?php$dirname="thedir";$del_files=array("a","b","c");foreach($del_files AS $del){unlink("$dirname/$del/test.txt");}$dirname="thedir";$del=array("a","b","c");foreach($del AS $deleted){rmdir("$dirname/$deleted");}rmdir("thedir");$dirname="thedir";$a=mkdir($dirname);$d=array("a","b","c");foreach($d AS $a){$x=mkdir("$dirname/$a");echo " dir made is $a <br>";}$mess="this is redarrow";$a=fopen("$dirname/a/test.txt","a");if(file_exists("$dirname/a/test.txt")){fwrite($a,$mess);echo " message writen test.txt<br>";}$mess="redarrow loves php";$a=fopen("$dirname/b/test.txt","a");if(file_exists("$dirname/b/test.txt")){fwrite($a,$mess);echo " message writen test.txt<br>";}$mess="love php";$a=fopen("$dirname/c/test.txt","a");if(file_exists("$dirname/c/test.txt")){fwrite($a,$mess);echo " message writen test.txt";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/17327-solved-fun-with-fwrite-mkdir-unlink-foreach-rmdir-thanks-all/#findComment-73676 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.