/    Sign up×
Community /Pin to ProfileBookmark

Graphing with PHP

is there anyway to make a GD graph from results of a function

[code=php]

<form action=”<?$_SERVER[‘PHP_SELF’];?>” method=”post”>
<input type=”text” name=”number”>
<br />
<input type=”submit” name=”submit” value=”Calculate”>
</form>
<?php
function coinFlip50()
{
$count=array();
for($j=50; $j–;)
{
$rand=rand(0, 1);

if($rand==”1″)
{
$blah=count($result);
$blah2=$blah+ 1;
$result = array_pad($count, $blah2, 0);
}
}
echo count($result);
echo”<br>”;
}
function coinFlips($number)
{
for($i=$number; $i–;)
{
coinFlip50();
}
}
if(isset($_POST[‘submit’]))
{
coinFlips($number);
}
?>[/code]

I have this php script I just wrote up, is there anyway to take all of the results(the # from each time it loops , you can test the script here: [url]http://complexfellow.com/flip50.php[/url]) and make a graph out of it using GD or something. Perhaps ill even make a faux bar graph using #’s to build it up if you cant do it in GD but I think you can. Would I add each result into an array and then go from there, any help?

to post a comment
PHP

21 Comments(s)

Copy linkTweet thisAlerts:
@ConorauthorOct 04.2004 — can you return an array in a function?
Copy linkTweet thisAlerts:
@ConorauthorOct 04.2004 — I tried this
[code=php]

function coinFlip50()
{
$count=array();
for($j=50; $j--;)
{
$rand=rand(0, 1);

if($rand=="1")
{
$blah=count($result);
$blah2=$blah+ 1;
$result = array_pad($count, $blah2, 0);
}
}
$totalheads=count($result);
return $totalheads;
}
function coinFlips($number)
{
$arraynew=array();
for($i=1; $i <= $number; $i++)
{
$cointotal=coinFlip50();
$graph=array_pad($arraynew,$i,$cointotal);
}
return $graph;
}[/code]

but it returns the same value for each value of the array so like if i do coinFlip(50); it will do 27 or 24 or something for all of them instead of a different # for each of them, any solution>
Copy linkTweet thisAlerts:
@MstrBobOct 04.2004 — Now I'm just ever so confused here. What exactly are you trying to do? And in coinFlip50() you're counting $result, but you never define it! ? This script is a bit odd, what are you trying to do?
Copy linkTweet thisAlerts:
@ConorauthorOct 04.2004 — I define it, in the if($rand==1){//blah} , coinFlip50() function works fine, its the coinFlips function im having trouble with.
Copy linkTweet thisAlerts:
@MstrBobOct 04.2004 — Edit: my thinking is off...
Copy linkTweet thisAlerts:
@JonaOct 04.2004 — [font=trebuchet ms]Maybe...[/font]

[code=php]
function coinFlips($number){
for($i=$number; $i<$number; $i--){
coinFlip50();
}
}
[/code]
Copy linkTweet thisAlerts:
@ConorauthorOct 04.2004 — I can get it to work but I need to get the coinFlip50() number into an array for the later graph and the problem is with the array
Copy linkTweet thisAlerts:
@ConorauthorOct 04.2004 — 25

25

19

33

24

20

25

24

20

26

25

27

27

17

25

25

30

25

26

27

thats the result when I just echo right in the function, heres the result from the array

28

28

28

28

28

28

28

28

28

28

28

28

28

28

28

28

28

28

28

28


so obviously thats where the problem is
Copy linkTweet thisAlerts:
@JonaOct 04.2004 — [code=php]
function coinFlips($number){
$temp=array();
for($i=1; $i <= $number; $i++){
$temp[] = coinFlip50();
}
return $temp;
}[/code]
Copy linkTweet thisAlerts:
@ConorauthorOct 04.2004 — w00t it worked jona, now I need to figure out how to graph with gd library
Copy linkTweet thisAlerts:
@JonaOct 04.2004 — [font=trebuchet ms]Good luck, let me know if you need help. ? [/font]
Copy linkTweet thisAlerts:
@ConorauthorOct 04.2004 — maybe I could do it without GD like they do with the polls on the forum, anyone know how that is done?
Copy linkTweet thisAlerts:
@JonaOct 04.2004 — [i]Originally posted by Conor [/i]

[B]maybe I could do it without GD like they do with the polls on the forum, anyone know how that is done? [/B][/QUOTE]


[font=trebuchet ms]Wait, do you mean just horizontal bars at different lengths specifying a specific number?[/font]
Copy linkTweet thisAlerts:
@ConorauthorOct 04.2004 — yeah it could be any number from 0-50 and i need a bar going to a certain height depending on how many 22's, how many 24's etc..
Copy linkTweet thisAlerts:
@JonaOct 05.2004 — [font=trebuchet ms]Well you would probably start by making an array of all the values that were not duplicated (using array_unique and setting it to a seperate array variable), and then do a foreach loop with a for loop inside of it... Never done a graphic like this in GD...it could be fun, but I'll have to find some time to work on it. ? [/font]
Copy linkTweet thisAlerts:
@ConorauthorOct 05.2004 — just curious as to why you would start with that, shouldnt you just get a count of how many there are of each # and make the graph accordingly.
Copy linkTweet thisAlerts:
@ShrineDesignsOct 05.2004 — use this to get a count of all similar values in an array:[code=php]function count_similar($array)
{
if(!is_array($array))
{
return false;
}
$retval = array_unique($array);
$retval = array_flip($retval);

for($i = 0; $i < count($retval); $i++)
{
$retval[$i] = 0;
}
foreach($array as $key)
{
$retval[$key] += 1;
}
return $retval;
}[/code]
Copy linkTweet thisAlerts:
@ConorauthorOct 05.2004 — is there anyway to have it correlate with a specific one like it will say 26-4 instead of just 4.

heres what I tried

[code=php]
<?php
function coinFlip50()
{
$count=array();
for($j=50; $j--;)
{
$rand=rand(0, 1);

if($rand=="1")
{
$blah=count($result);
$blah2=$blah+ 1;
$result = array_pad($count, $blah2, 0);
}
}
$totalheads=count($result);
return $totalheads;
}
function countSimilar($array)
{
if(!is_array($array))
{
return false;
}
$retval = array_unique($array);
$retval = array_flip($retval);

for($i = 0; $i < count($retval); $i++)
{
$retval[$i] = 0;
}
foreach($array as $key)
{
$retval[$key] += 1;
}
return $retval;
}
function coinFlips($number){
$temp=array();
for($i=1; $i <= $number; $i++){
$temp[] = coinFlip50();
}
return $temp;
}
?>
<form action="<?$_SERVER['PHP_SELF'];?>" method="post">
<input type="text" name="number">
<br />
<input type="submit" name="submit" value="Calculate">
</form>
<?php
if(isset($_POST['submit']))
{
$coinflippage=coinFlips($number);
$count_sim=countSimilar($coinflippage);
array_multisort($coinflippage, SORT_DESC, SORT_NUMERIC, $count_sim , SORT_DESC, SORT_NUMERIC);
for($i=0; $i<count($user); $i++){
echo $coinflippage[$i]."----->".$count_sim[$i]."<BR>";;
}

}
[/code]


I get this error: Warning: array_multisort(): Array sizes are inconsistent in /home2/refresh/public_html/flip50.php on line 56
Copy linkTweet thisAlerts:
@ConorauthorOct 05.2004 — anybody?
Copy linkTweet thisAlerts:
@JonaOct 05.2004 — [font=trebuchet ms]Something like...[/font]

[code=php]
function countSimilar($array)
{
if(!is_array($array))
{
return false;
}
$retval = array_unique($array);
$retval = array_flip($retval);

for($i = 0; $i < count($retval); $i++)
{
$retval[$i] = 0;
}
foreach($array as $key)
{
$retval[$key] = $array[$key]." - ".($retval[$key]+1);
}
return $retval;
}
[/code]
Copy linkTweet thisAlerts:
@ConorauthorOct 06.2004 — hmm they still return a different number of values.
×

Success!

Help @Conor spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...