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
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

how to manipulate an array???

Post by apek »

this a snippet from my 1st page:

Code: Select all

while($periksa2=mysql_fetch_array($viewCheck2, MYSQL_ASSOC))
{
?>

	<TR>
	  <TD><font face="verdana" size="2"><?php   echo $periksa2["bulan"]; ?></font></TD>
	  <TD><font face="verdana" size="2"><?php   echo $periksa2["bilHari"]; ?></font></TD>
	  <TD><font face="verdana" size="2"><?php   echo $periksa2["jumlah"]; ?></font></TD>

<?php 
  $jumlahRosak=trim($periksa2["jumlah"]);
  $bilHari=trim($periksa2["bilHari"]);
  $failureRate=($jumlahRosak/$bilHari);
  $failure=number_format($failureRate, 3, '.', '')
?>


	  <TD><font face="verdana" size="2"><?php   echo $failure; ?></font></TD>
<?php 

$SQLupd=query("UPDATE tempfailure SET failureRate = '$failure' WHERE bulan = '{$periksa2["bulan"]}'", $link, __LINE__, __FILE__);
$array1[]=$failure;

?>
	</TR>
<?php 
} 
?>
</table>

<input type="hidden" name="purataMasaBD" value="<?=$purataMasaBD1?>">
<input type="hidden" name="lambda" value="<?=$lambda1?>">
<input type="hidden" name="purataMasaPPM" value="<?=$purataMasaPPM2?>">
<input type="hidden" name="MTTF" value="<?=$MTTF1?>">
<input type="hidden" name="gagal" value="<?=$array1?>">
<input type="hidden" name="noTag" value="<?=$_POST["noTag"]?>">
<input type="hidden" name="year" value="<?=$_POST["year"]?>">
<input type="hidden" name="bil" value="<?=$bil?>">
notice that the $array1 is a collection an array..lets say its 1,2,3,4...12

and then i post it to the second page to manipulate the array...
and heres the snippet:

Code: Select all

<?php 
$viewCheck=query("SELECT * FROM mindowntime order by id", $link, __LINE__, __FILE__);




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=$_POST["gagal"];
  $dt1=$gagal*$bilInspection*$bd*$bt3+$pb;
  $dt2=$bilInspection+$pb;
  $minDownTime=$dt1/$dt2;
  $minDownTime1=number_format($minDownTime, 5, '.', '');
  echo $gagal."\n"; 
  $SQLupd=mysql_query("UPDATE mindowntime SET Dn = '$minDownTime1' where n='$bilInspection'");

} 

?>
<CENTER>
<?php
notice that the $gagal is taken from the 1st page before..
but when i echo the array,it print out Array Array Array Array Array..Array

it suppose to print out certain numbers,not the Array words...
and how to manipulate the array that i post from the 1st page??
lets say the array 1,2,3...12..
and on the first loop i want to manipulate the 1...and the second i want to manipulate the 2...and so on...do u understand??
sorry for my poor english..

FYI,i tried the SESSION way...
but it gives me "unsupported operand" error??

so pls array experts,HELP me!!!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

If it prints out Array, Array, Array, and so on, then each element of your array is in itself an array. Try

Code: Select all

echo "<pre>";
print_r($array_1);
echo "</pre>";
to see your array structure. It sounds like you're going to have to go one level deeper. Or you could make on of the second level array values, an index for the first level array. Make sense?
User avatar
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

Post by apek »

i dont understand what u said...
and the important is,i cant manipulate the data due to its contains...
its not contains values...it contains Array,Array..Array

how to fix that
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Ok, say I've got a setup like this:

Code: Select all

$internal_1 = array("orange","green","blue");
$internal_2 = array("apple","banana","raspberry");
Then I put both internal arrays in another array

Code: Select all

$external[0] = $internal_1;
$external[1] = $internal_2;
If I print out each element in $external, I'll get this

Code: Select all

echo $external[0];
will print out "Array" because that's what the element is. To find out everything in the external array, type:

Code: Select all

echo "<pre>";
print_r($external);
echo </pre>";
which will get you something like:

Code: Select all

Array
(
    [0] => Array
        (
            [0] => orange
            [1] => green
            [2] => blue
        )

    [1] => Array
        (
            [0] => apple
            [1] => banana
            [2] => raspberry
        )

)
This is what you're doing - putting an array in an array. Like I said, you will have to go deeper than just one level, to get to the data.
User avatar
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

Post by apek »

so can u fix my code,i didn't see it much in your code...
i'm a newbie actually...only to much question made me to the level "Developer" in this forum...
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: how to manipulate an array???

Post by pickle »

I didn't fix your code because I'm trying to help you help yourself. What I have done is put the appropriate echo statement in place of echo $gagal. Take a look near the end of the code to see what I did. Try this and see what the output is.

Code: Select all

while($periksa2=mysql_fetch_array($viewCheck2, MYSQL_ASSOC))
{
?>

	<TR>
	  <TD><font face="verdana" size="2"><?php   echo $periksa2["bulan"]; ?></font></TD>
	  <TD><font face="verdana" size="2"><?php   echo $periksa2["bilHari"]; ?></font></TD>
	  <TD><font face="verdana" size="2"><?php   echo $periksa2["jumlah"]; ?></font></TD>

<?php 
  $jumlahRosak=trim($periksa2["jumlah"]);
  $bilHari=trim($periksa2["bilHari"]);
  $failureRate=($jumlahRosak/$bilHari);
  $failure=number_format($failureRate, 3, '.', '')
?>


	  <TD><font face="verdana" size="2"><?php   echo $failure; ?></font></TD>
<?php 

$SQLupd=query("UPDATE tempfailure SET failureRate = '$failure' WHERE bulan = '{$periksa2["bulan"]}'", $link, __LINE__, __FILE__);
$array1[]=$failure;

?>
	</TR>
<?php 
} 
?>
</table>

<input type="hidden" name="purataMasaBD" value="<?=$purataMasaBD1?>">
<input type="hidden" name="lambda" value="<?=$lambda1?>">
<input type="hidden" name="purataMasaPPM" value="<?=$purataMasaPPM2?>">
<input type="hidden" name="MTTF" value="<?=$MTTF1?>">
<input type="hidden" name="gagal" value="<?=$array1?>">
<input type="hidden" name="noTag" value="<?=$_POST["noTag"]?>">
<input type="hidden" name="year" value="<?=$_POST["year"]?>">
<input type="hidden" name="bil" value="<?=$bil?>">

<?php 
$viewCheck=query("SELECT * FROM mindowntime order by id", $link, __LINE__, __FILE__);




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=$_POST["gagal"];
  $dt1=$gagal*$bilInspection*$bd*$bt3+$pb;
  $dt2=$bilInspection+$pb;
  $minDownTime=$dt1/$dt2;
  $minDownTime1=number_format($minDownTime, 5, '.', '');
  //change made here
  echo "<pre>";
  print_r($gagal);
  echo "</pre>";
  //end of changes
  //original line:  echo $gagal."\n"; 
  $SQLupd=mysql_query("UPDATE mindowntime SET Dn = '$minDownTime1' where n='$bilInspection'");

} 

?>
<CENTER>
<?php
User avatar
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

Post by apek »

ok..
this is the output..:
Array

Array

Array

Array

Array

Array

Array

Array

Array

Array

Array

Array

Array

Array

Array

Array

Array

Array

Array

Array

Array

i think theres no different like i did...
only the <pre> statement make the array to the new line...
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

<input type="hidden" name="gagal" value="<?=$array1?>">
You can't post an array like that, the best bet is to make it a string first, then rebuild the array on the page you post to, Eg
<input type="hidden" name="gagal" value="<?php echo join(',', $array1) ?>">

Then where you have $gagal=$_POST["gagal"];
do
$gagal= explode(',', $_POST["gagal"]);
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" error...
argghhh... :x
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

On which line exactly?
User avatar
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

Post by apek »

the error pointed at the second line...
i believe it came from the first line...

Code: Select all

$gagal=explode(',', $_POST["gagal"]);
  $dt1=$gagal*$bilInspection*$bd*$bt3+$pb;
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

$dt1=$gagal*$bilInspection*$bd*$bt3+$pb;
Yeah, you can't multiply a string. $gagal is a string like '1,2,3' .. what exactly do you want to do with that string that involves multiplying it? ie do you want to add all the numbers up first, then multiply? eg like (1+2+3) *$bilInspection*$bd*$bt3+$pb; ??
User avatar
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

Post by apek »

no...
i want to like this...
lets say the array is 1,2,3..12
on the first loop the

Code: Select all

$dt1=$gagal*$bilInspection*$bd*$bt3+$pb;
will be executed with $gagal =1....
and then on the second loop the $gagal=2...

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

Post by markl999 »

Then you need to change

Code: Select all

$gagal=explode(',', $_POST["gagal"]); 
$dt1=$gagal*$bilInspection*$bd*$bt3+$pb;

to

Code: Select all

foreach($_POST['gagal'] as $gag){
    $dt1=$gag*$bilInspection*$bd*$bt3+$pb;
}
But of course that will overwrite $dt1 everytime .. so you need to decide how you are going to store each of the calculations, amybe with $dt[] = $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 the "Warning: Invalid argument supplied for foreach()" error... :cry:
Post Reply