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
dalyraptor
Forum Newbie
Posts: 5 Joined: Thu Aug 26, 2004 6:50 am
Post
by dalyraptor » Thu Aug 26, 2004 6:50 am
feyd | Please use Code: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
im having trouble implementing a class in php4. right now the code is very ugly and there may be more wrong with it than my current problem.
i want to pass and array into the class, but when I do, i dont seem to be able to access the values inside the array, when i echo (as you will see in the code) i get ArrayArray.
the layout of the array is:Code: Select all
Array (
Array (v1, v2)
Array (v1, v2)
.....
)
code:
Code: Select all
<?php
$arr = Array ();
$arr[] = Array ("START", 0);
$arr[] = Array ("ONE", 1);
$arr[] = Array ("TWO", 2);
$arr[] = Array ("FINISHED TWO", 3);
$arr[] = Array("FINISHED ONE", 4);
$arr[] = Array ("ONE", 5);
$arr[] = Array ("TWO", 6);
$arr[] = Array ("FINISHED TWO", 7);
$arr[] = Array("FINISHED ONE", ;
$arr[] = Array ("END", 9);
class TimeFunct
{
var $arr;
var $arr_totals = Array(Array());
var $total_time;
var $i;
var $strng = "";
function giveTimeArray($theArray)
{
(array) $this->$arr = (array) $theArray;
return $this->$arr[2][0];
}
function timeLabelFound($time_array, $timeLable)
{
for ($i=0; $i<count($time_array); $i++)
{
if ($time_array[$i][0]==$timeLable) return $i;
}
return false;
}
function outputTimes()
{
for ($i=0; $i<count($this->$arr)-1; $i++)
{
$timeLable = $this->$arr[$i][0] . " -to- " . $this->$arr[$i+1][0];
return $this->$arr[4][1];
$time = $this->$arr[$i+1][1] - $this->$arr[$i][1];
return $timeLable;
if ($j = timeLabelFound($arr_totals, $timeLable))
{
$arr_totals[$j][1] += $time;
} else {
$arr_totals[] = Array ($timeLable, $time);
}
}
$this->$total_time = $arr[count($arr)-1][1] - $arr[0][1];
for ($i=1; $i<count($arr_totals); $i++)
{
$time = $arr_totals[$i][1];
$time_precent = $time / $total_time * 100;
$strng .= 'Time: '.number_format( $time, 6 ).' seconds ( '.number_format( $time_precent, 2 ).'% ) for '.$arr_totals[$i][0]. '<br>';
}
return $strng;
}
}
$tme = new TimeFunct;
echo ($tme->giveTimeArray($arr) );
echo ( $tme->outputTimes() );
?>
feyd | Please use Code: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
delorian
Forum Contributor
Posts: 223 Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland
Post
by delorian » Thu Aug 26, 2004 6:58 am
Try to use print_r instead of echo. echo() cannot be used for displaing arrays.
dalyraptor
Forum Newbie
Posts: 5 Joined: Thu Aug 26, 2004 6:50 am
Post
by dalyraptor » Thu Aug 26, 2004 8:06 am
the type being returned ($arr[2][0]) should be a number, but it actually the whole array $arr.
dalyraptor
Forum Newbie
Posts: 5 Joined: Thu Aug 26, 2004 6:50 am
Post
by dalyraptor » Thu Aug 26, 2004 8:34 am
feyd | Please use Code: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
i've taken out all the unnecessary stuff:Code: Select all
<?php
$arr = Array ();
$arr[] = Array ("START", 0);
$arr[] = Array ("ONE", 1);
$arr[] = Array ("TWO", 2);
$arr[] = Array ("FINISHED TWO", 3);
$arr[] = Array("FINISHED ONE", 4);
$arr[] = Array ("ONE", 5);
$arr[] = Array ("TWO", 6);
$arr[] = Array ("FINISHED TWO", 7);
$arr[] = Array("FINISHED ONE", ;
$arr[] = Array ("END", 9);
class TimeFunct
{
var $arr;
var $arr_totals = Array(Array());
var $total_time;
var $i;
var $strng = "";
function &giveTimeArray($theArray)
{
$this->$arr = $theArray;
return $this->$arr[2][0];
}
}
$tme = new TimeFunct;
echo ($tme->giveTimeArray($arr) );
?>
feyd | Please use Code: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Aug 26, 2004 9:50 am
why not use associative arrays?
dalyraptor
Forum Newbie
Posts: 5 Joined: Thu Aug 26, 2004 6:50 am
Post
by dalyraptor » Thu Aug 26, 2004 10:20 am
Sorry about not using the code tags.
Well the array comes from a different piece of code written by someone else. Im trying to expand on it without modifying the other code. I didnt know that associatve arrays would work and dont see why they should work over the current way. Is that not a bug?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Aug 26, 2004 10:33 am
Code: Select all
function &giveTimeArray($theArray)
{
$this->$arr = $theArray;
return $this->$arr[2][0];
}
is actually the likely problem.. $this->arr is the proper access hook.
dalyraptor
Forum Newbie
Posts: 5 Joined: Thu Aug 26, 2004 6:50 am
Post
by dalyraptor » Thu Aug 26, 2004 11:16 am
doh! that was it. im new to php, this is my second day!
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Aug 26, 2004 11:23 am
Welcome to the world of PHP
We hope you stay a while. Try the bangers, they're delicious.