how to manipulate an array???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

opps, sorry, that should have been :
foreach(explode(',', $_POST['gagal']) as $gag){
User avatar
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

Post by apek »

i get unsupported operand types...

Code: Select all

while($periksa=mysql_fetch_array($viewCheck))
{
 
  $MTTF=$_POST["MTTF"];
  $bd=$_POST["purataMasaBD"];
  $pb=$_POST["purataMasaPPM"];
  $bilInspection=$periksa["n"];
  $bt1=exp(-$MTTF*$bilInspection);
  $bt2=$bt1/($MTTF/$bilInspection);
  $bt3=1-$bt2;
  //$gagal=explode(',', $_POST["gagal"]);
  //$dt1=$gagal*$bilInspection*$bd*$bt3+$pb;
  foreach(explode(',', $_POST['gagal']) as $gag){
    $dt1[]=($gag*$bilInspection*$bd*$bt3)+$pb;
}
  $dt2=$bilInspection+$pb;
  $minDownTime=$dt1/$dt2;
  $minDownTime1=number_format($minDownTime, 5, '.', '');
  echo "<pre>";
  print_r($dt1);
  echo "</pre>";
  $SQLupd=mysql_query("UPDATE mindowntime SET Dn = '$minDownTime1' where n='$bilInspection'");

}
the error pointed at
$minDownTime=$dt1/$dt2;
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

$minDownTime=$dt1/$dt2;

$dt1 is now an array ... so you can't divide it.
Just so you're clear what's happening ..
you are posting from a form, a string that might look like '1,2,3,4'
Then you take each of those numbers in turn and do a calculation on them, storing the result in another array, $dt1.

So instead of $minDownTime=$dt1/$dt2; i'm guessing you want $minDownTime=array_sum($dt1)/$dt2; which will first add up all those individual calculations and divide the sum of them by $dt2.
User avatar
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

Post by apek »

what do you mean by
which will first add up all those individual calculations
User avatar
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

Post by apek »

i understand it...
but i dont want to add up all the values in the array...
i want to manipulate it one by one....
just like i said before...
do u know wat i mean??? :cry:
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Well, $dt1 is now an array of values, those values being the result of each individual calculation, $dt1[]=($gag*$bilInspection*$bd*$bt3)+$pb; ... so what do you want to do with that array? I just assumed you want to add them all up and divide by $dt2
User avatar
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

Post by apek »

the $gag is like =1,2,3..12
all i want is on the first loop the $gag=1..
the second loop the $gag=2...and so on...
i dont know how to xplain... :cry:
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

That's ok. The confusion is ... what goes inside this loop? ie what do you want to do with $gag ?
Maybe post some example code of how it should be?
User avatar
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

Post by apek »

this is my code:

Code: Select all

while($periksa=mysql_fetch_array($viewCheck))
{

  $MTTF=$_POST["MTTF"];
  $bd=$_POST["purataMasaBD"];
  $pb=$_POST["purataMasaPPM"];
  $bilInspection=$periksa["n"];
  $bt1=exp(-$MTTF*$bilInspection);
  $bt2=$bt1/($MTTF/$bilInspection);
  $bt3=1-$bt2;
  //$gagal=explode(',', $_POST["gagal"]);
  //$dt1=$gagal*$bilInspection*$bd*$bt3+$pb;
  foreach(explode(',', $_POST['gagal']) as $gag){
    $dt1[]=($gag*$bilInspection*$bd*$bt3)+$pb;
}
  $dt2=$bilInspection+$pb;
  $minDownTime=$dt1/$dt2;
  $minDownTime1=number_format($minDownTime, 5, '.', '');
  echo "<pre>";
  print_r($dt1);
  echo "</pre>";
  $SQLupd=mysql_query("UPDATE mindowntime SET Dn = '$minDownTime1' where n='$bilInspection'");

}
all of them is in the while loop...
what i want to do is the array posted from the first page will be stored in $gag...
and lets say $gag=1,2,3...12

and in the first loop,
$dt1[]=($gag*$bilInspection*$bd*$bt3)+$pb;
will be executed with $gag=1,
and one the second loop,
$dt1[]=($gag*$bilInspection*$bd*$bt3)+$pb;
will be executed with $gag=2,
and so on...

do u get it???
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Nope, still don't get it. That's exactly what your code should be doing now, what i'm unclear about is what you want to do with each of the values .. ($gag*$bilInspection*$bd*$bt3)+$pb; ... it 'sounds' like you want ..

Code: Select all

{ 

  $MTTF=$_POST["MTTF"]; 
  $bd=$_POST["purataMasaBD"]; 
  $pb=$_POST["purataMasaPPM"]; 
  $bilInspection=$periksa["n"]; 
  $bt1=exp(-$MTTF*$bilInspection); 
  $bt2=$bt1/($MTTF/$bilInspection); 
  $bt3=1-$bt2; 
  foreach(explode(',', $_POST['gagal']) as $gag){ 
    $dt1=($gag*$bilInspection*$bd*$bt3)+$pb; 
    $dt2=$bilInspection+$pb; 
    $minDownTime=$dt1/$dt2; 
    $minDownTime1=number_format($minDownTime, 5, '.', ''); 
    echo "<pre>"; 
    print_r($dt1); 
    echo "</pre>"; 
   $SQLupd=mysql_query("UPDATE mindowntime SET Dn   = '$minDownTime1' where n='$bilInspection'");
   } 
}
but again, i'm guessing as i'm still unclear..sorry. If i've got it wrong then paste some code that shows how it should work .. even if it doesn't it will give us an idea of what you are aiming for exactly.
User avatar
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

Post by apek »

i tried that..
it shows me
0.83782

0.86300

0.83782

0.84411

0.83782

1.68770

0.63636

1.04557

1.05816

and many more..
suppose to show only 12 values only...
where the rest comes from??? :roll:
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

What does var_dump($_POST['gagal']); show if you put it just before the foreach() line ?
User avatar
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

Post by apek »

wow...it shows this:

Code: Select all

string(71) "0.032,0.036,0.032,0.033,0.032,0.167,0.000,0.065,0.067,0.032,0.033,0.000"

0.032

0.036

0.032

0.033

0.032

0.167

0.000

0.065

0.067

0.032

0.033

0.000
WITH 12 set of that!!


and the
0.032

0.036

0.032

0.033

0.032

0.167

0.000

0.065

0.067

0.032

0.033

0.000

is exactly the correct value..
but why it appears in 12 set???
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

So $_POST['gagal'] has 12 numbers in it .. the values going down the screen you see are just from the :
echo "<pre>";
print_r($dt1);
echo "</pre>";
bit, which is for debugging and you can remove it now.
So you're saying the 12 numbers are ok?
User avatar
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

Post by apek »

yup...
but i wonder why its in 12 set....

the true output is:

Code: Select all

string(71) "0.032,0.036,0.032,0.033,0.032,0.167,0.000,0.065,0.067,0.032,0.033,0.000"

0.032

0.036

0.032

0.033

0.032

0.167

0.000

0.065

0.067

0.032

0.033

0.000

string(71) "0.032,0.036,0.032,0.033,0.032,0.167,0.000,0.065,0.067,0.032,0.033,0.000"

0.032

0.036

0.032

0.033

0.032

0.167

0.000

0.065

0.067

0.032

0.033

0.000

string(71) "0.032,0.036,0.032,0.033,0.032,0.167,0.000,0.065,0.067,0.032,0.033,0.000"

0.032

0.036

0.032

0.033

0.032

0.167

0.000

0.065

0.067

0.032

0.033

0.000

string(71) "0.032,0.036,0.032,0.033,0.032,0.167,0.000,0.065,0.067,0.032,0.033,0.000"

0.032

0.036

0.032

0.033

0.032

0.167

0.000

0.065

0.067

0.032

0.033

0.000

string(71) "0.032,0.036,0.032,0.033,0.032,0.167,0.000,0.065,0.067,0.032,0.033,0.000"

0.032

0.036

0.032

0.033

0.032

0.167

0.000

0.065

0.067

0.032

0.033

0.000

string(71) "0.032,0.036,0.032,0.033,0.032,0.167,0.000,0.065,0.067,0.032,0.033,0.000"

0.032

0.036

0.032

0.033

0.032

0.167

0.000

0.065

0.067

0.032

0.033

0.000

string(71) "0.032,0.036,0.032,0.033,0.032,0.167,0.000,0.065,0.067,0.032,0.033,0.000"

0.032

0.036

0.032

0.033

0.032

0.167

0.000

0.065

0.067

0.032

0.033

0.000

string(71) "0.032,0.036,0.032,0.033,0.032,0.167,0.000,0.065,0.067,0.032,0.033,0.000"

0.032

0.036

0.032

0.033

0.032

0.167

0.000

0.065

0.067

0.032

0.033

0.000

string(71) "0.032,0.036,0.032,0.033,0.032,0.167,0.000,0.065,0.067,0.032,0.033,0.000"

0.032

0.036

0.032

0.033

0.032

0.167

0.000

0.065

0.067

0.032

0.033

0.000

string(71) "0.032,0.036,0.032,0.033,0.032,0.167,0.000,0.065,0.067,0.032,0.033,0.000"

0.032

0.036

0.032

0.033

0.032

0.167

0.000

0.065

0.067

0.032

0.033

0.000

string(71) "0.032,0.036,0.032,0.033,0.032,0.167,0.000,0.065,0.067,0.032,0.033,0.000"

0.032

0.036

0.032

0.033

0.032

0.167

0.000

0.065

0.067

0.032

0.033

0.000

string(71) "0.032,0.036,0.032,0.033,0.032,0.167,0.000,0.065,0.067,0.032,0.033,0.000"

0.032

0.036

0.032

0.033

0.032

0.167

0.000

0.065

0.067

0.032

0.033

0.000
Post Reply