Jump to content


Photo

[solved] fun with fwrite mkdir unlink foreach rmdir thanks all


  • Please log in to reply
6 replies to this topic

#1 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 12 August 2006 - 11:12 AM

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.

<?php

$a=rand(1,5000);

for($i=0; $i<9; $i++){

echo "$a<br>";
}

?>


The result but not as mixed numbers.

4962
4962
4962
4962
4962
4962
4962
4962
4962



this is the long way that the above should do

<?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>";

?>

result but should work as first code above with the result of 10 new numbers.
4036
2456
4840
2138
1618
4250
4429
177
4425
526

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 12 August 2006 - 11:55 AM

Why not:
<?php
//incase you want to know the random number for later
for($i=0; $i<9; $i++){
$rand[$i]=rand(1,5000);
echo $rand[$i]."<br>";
}


//incase you just want to echo random numbers
for($i=0; $i<9; $i++){
echo(rand(1,5000));
}

?>

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 12 August 2006 - 12:16 PM

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
<?php
for($i=0; $i<9; $i++){
$rand[$i]=rand(1,5000);
$dir=$rand[$i]."<br>";
$a=mkdir(thedir);
mkdir("thedir/$dir");
}
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 12 August 2006 - 12:35 PM

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
$a=mkdir(thedir);
out of the for loop and place it at the top.

Also remove
."<br>"
from this
$dir=$rand[$i]."<br>";


Now your code should now work. This is what you code should loook like:
<?php

// we create the directory first
mkdir('thedir');

for($i = 0; $i < 9; $i++)
{
    $rand[$i] = rand(1, 5000);

    $dir = $rand[$i];

    mkdir("thedir/$dir");

    echo "Created thedir/" . $dir . "<br />\n";
}

?>


#5 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 12 August 2006 - 12:40 PM

thank you wild teen learning getting there

know 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.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#6 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 12 August 2006 - 01:40 PM

Is this delete format wrong cheers.

<?php
$dirname="thedir";

$del=array("a","b","c");

foreach($del AS $deleted){

unlink("$dirname/$deleted");
}
unlink("thedir");
<?

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#7 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 12 August 2006 - 01:49 PM


This was for learning the file statements for fun.

i used

fwrite
unlink
array
file_exists
foreach
unlink

all 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.

<?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";
}


?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users