For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > December 2004 > Newbie question, probably stupidly simple .. sorry !!









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Newbie question, probably stupidly simple .. sorry !!
Ha .. What ??!!??

2004-12-10, 8:56 pm

Hi all, I have recently completed a php course (beginner I hasten to add)
and one of the unfinished projects was to create a bingo card. I have made a
working version of part of it (creates five random numbers from 90, and
orders them into columns based on their number) but when i try to run this
as a function, I get 'Warning: array_rand(): First argument has to be an
array in blah/blah.php on line 15'. Any help would be gratefully recieved,
thanks, Scruffy.
code follows >>>>>
<?php
srand ((float) microtime() * 10000000);
for($i=0;$i<=90;$i++){
global $numbers1;
$numbers1[]=$i;
global $numbers2;
$numbers2[]=$i;
global $numbers3;
$numbers3[]=$i;
}
function drawtable(){
unset($numbers1[0]);
for ($i=0;$i<5;$i++)
{
$name=array_rand($numbers1); <<<<<<<<<<<<<<<<<<<<line 15
$name2[]=$numbers1[$name];
if ($name<10) {for ($j=0;$j<10;$j++){unset($numbers1[$j]);}}else
if ($name>=10 && $name <=79){
for ($loop=10;$loop<=70;$loop+=10)
{
for($loop2=0;$loop2<=9;$loop2++)
{
if(($loop+$loop2)==$name){for
($j=$loop;$j< =($loop+9);$j++){unset($numbers1[$j]);}}

}
}
}else
{for ($j=80;$j<=90;$j++){unset($numbers1[$j]);}}
}
sort($name2);

for($loop=10;$loop<=90;$loop+=10)
{
print "<td>";
for ($i=0;$i<5;$i++)
{
$check=$name2[$i];
if ($check==90&&$loop==90) {print $check;}else
if($check<($loop)&&$check>=($loop-10)){print $check;}else{print
" ";}

}
print "</td>";
}

//foreach($name2 as $key=>$value){
//print $key." -key, $value -value - name2<br>\n";
//}
}
?<html>
<head>
<title>Bingo !!</title>
</head>
<body>
<table align="center" border="1">
<tr>
<?php
drawtable();
?>
</tr>
</table>
</body>
</html>>


Hilarion

2004-12-10, 8:56 pm

Array $numbers1 is defined outside of "drawtable" function. If you want
to use this "global" variable value, then you'll have to put "global
$numbers1" into this function, or move also the variable initialization
into the function or pass the variable as the function parameter.

If you post your code, then make it a bit more readable.
Compare the one you sent with the one below (no changes in syntax made).

Hilarion

<?php

srand ((float) microtime() * 10000000);
for( $i=0; $i<=90; $i++ )
{
global $numbers1;
$numbers1[] = $i;
global $numbers2;
$numbers2[] = $i;
global $numbers3;
$numbers3[] = $i;
}

function drawtable()
{
unset( $numbers1[0] );
for( $i=0; $i<5; $i++ )
{
$name = array_rand( $numbers1 ); //<<<<<<<<<<<<<<<<<<<<line 15
$name2[] = $numbers1[$name];
if ($name < 10)
{
for( $j=0; $j<10; $j++ )
{
unset( $numbers1[$j] );
}
}
else if ($name>=10 && $name <=79)
{
for( $loop=10; $loop<=70; $loop+=10 )
{
for( $loop2=0; $loop2<=9; $loop2++ )
{
if(($loop+$loop2)==$name)
{
for( $j=$loop; $j<=($loop+9); $j++ )
{
unset( $numbers1[$j] );
}
}
}
}
}
else
{
for( $j=80; $j<=90; $j++ )
{
unset( $numbers1[$j] );
}
}
}
sort( $name2 );

for( $loop=10; $loop<=90; $loop+=10 )
{
print "<td>";
for( $i=0; $i<5; $i++ )
{
$check = $name2[$i];
if ($check==90 && $loop==90)
{
print $check;
}
else if ($check<($loop) && $check>=($loop-10))
{
print $check;
}
else
{
print " ";
}
}
print "</td>";
}

//foreach( $name2 as $key => $value )
//{
// print $key." -key, $value -value - name2<br>\n";
//}
}
?>
<html>
<head>
<title>Bingo !!</title>
</head>
<body>
<table align="center" border="1">
<tr>
<?php
drawtable();
?>
</tr>
</table>
</body>
</html>


Ha .. What ??!!??

2004-12-11, 8:55 am

Many thanks for your help, will try it out later, and sorry about the syntax
(consider my wrists slapped !!)



"Hilarion" <hilarion@SPAM.op.SMIECI.pl> wrote in message
news:cpdf96$smg$1@news.onet.pl...
> Array $numbers1 is defined outside of "drawtable" function. If you want
> to use this "global" variable value, then you'll have to put "global
> $numbers1" into this function, or move also the variable initialization
> into the function or pass the variable as the function parameter.
>
> If you post your code, then make it a bit more readable.
> Compare the one you sent with the one below (no changes in syntax made).
>
> Hilarion
>
> <?php
>
> srand ((float) microtime() * 10000000);
> for( $i=0; $i<=90; $i++ )
> {
> global $numbers1;
> $numbers1[] = $i;
> global $numbers2;
> $numbers2[] = $i;
> global $numbers3;
> $numbers3[] = $i;
> }
>
> function drawtable()
> {
> unset( $numbers1[0] );
> for( $i=0; $i<5; $i++ )
> {
> $name = array_rand( $numbers1 ); //<<<<<<<<<<<<<<<<<<<<line 15
> $name2[] = $numbers1[$name];
> if ($name < 10)
> {
> for( $j=0; $j<10; $j++ )
> {
> unset( $numbers1[$j] );
> }
> }
> else if ($name>=10 && $name <=79)
> {
> for( $loop=10; $loop<=70; $loop+=10 )
> {
> for( $loop2=0; $loop2<=9; $loop2++ )
> {
> if(($loop+$loop2)==$name)
> {
> for( $j=$loop; $j<=($loop+9); $j++ )
> {
> unset( $numbers1[$j] );
> }
> }
> }
> }
> }
> else
> {
> for( $j=80; $j<=90; $j++ )
> {
> unset( $numbers1[$j] );
> }
> }
> }
> sort( $name2 );
>
> for( $loop=10; $loop<=90; $loop+=10 )
> {
> print "<td>";
> for( $i=0; $i<5; $i++ )
> {
> $check = $name2[$i];
> if ($check==90 && $loop==90)
> {
> print $check;
> }
> else if ($check<($loop) && $check>=($loop-10))
> {
> print $check;
> }
> else
> {
> print " ";
> }
> }
> print "</td>";
> }
>
> //foreach( $name2 as $key => $value )
> //{
> // print $key." -key, $value -value - name2<br>\n";
> //}
> }
> ?>
> <html>
> <head>
> <title>Bingo !!</title>
> </head>
> <body>
> <table align="center" border="1">
> <tr>
> <?php
> drawtable();
> ?>
> </tr>
> </table>
> </body>
> </html>
>



Ha .. What ??!!??

2004-12-11, 8:55 am

I did as you said and put the global in the function, and all is well ine
the world again !!!
If you dont mind, can you explain the other ways around that you described
(ie move also the variable initialization
into the function or pass the variable as the function parameter) as Im
afraid you lost me a bit with these.
Many thanks again, Scruffy.


Hilarion

2004-12-11, 8:55 am

> If you dont mind, can you explain the other ways around that you described (ie move also the variable initialization into the
> function


<?php

srand ((float) microtime() * 10000000);

function drawtable()
{
for( $i=0; $i<=90; $i++ )
{
$numbers1[] = $i;
$numbers2[] = $i;
$numbers3[] = $i;
}

unset( $numbers1[0] );
for( $i=0; $i<5; $i++ )
{
// rest of the script as it was
?>



> or pass the variable as the function parameter)


<?php

srand ((float) microtime() * 10000000);


for( $i=0; $i<=90; $i++ )
{
$numbers1[] = $i;
$numbers2[] = $i;
$numbers3[] = $i;
}

function drawtable( $numbers1 )
{
unset( $numbers1[0] );

// rest of the function as it was
}
?>
<html>
<head>
<title>Bingo !!</title>
</head>
<body>
<table align="center" border="1">
<tr>
<?php
drawtable( $numbers1 );
?>
</tr>
</table>
</body>
</html>


Ha .. What ??!!??

2004-12-11, 8:55 pm

Many thanks again, you have cleared that up for me. Thanks again, Scruffy.
"Hilarion" <hilarion@SPAM.op.SMIECI.pl> wrote in message
news:cpeh62$ajs$1@news.onet.pl...
>
> <?php
>
> srand ((float) microtime() * 10000000);
>
> function drawtable()
> {
> for( $i=0; $i<=90; $i++ )
> {
> $numbers1[] = $i;
> $numbers2[] = $i;
> $numbers3[] = $i;
> }
>
> unset( $numbers1[0] );
> for( $i=0; $i<5; $i++ )
> {
> // rest of the script as it was
> ?>
>
>
>
>
> <?php
>
> srand ((float) microtime() * 10000000);
>
>
> for( $i=0; $i<=90; $i++ )
> {
> $numbers1[] = $i;
> $numbers2[] = $i;
> $numbers3[] = $i;
> }
>
> function drawtable( $numbers1 )
> {
> unset( $numbers1[0] );
>
> // rest of the function as it was
> }
> ?>
> <html>
> <head>
> <title>Bingo !!</title>
> </head>
> <body>
> <table align="center" border="1">
> <tr>
> <?php
> drawtable( $numbers1 );
> ?>
> </tr>
> </table>
> </body>
> </html>
>
>



Ha .. What ??!!??

2004-12-15, 3:57 pm

Many thanks for your help, will try it out later, and sorry about the syntax
(consider my wrists slapped !!)



"Hilarion" <hilarion@SPAM.op.SMIECI.pl> wrote in message
news:cpdf96$smg$1@news.onet.pl...
> Array $numbers1 is defined outside of "drawtable" function. If you want
> to use this "global" variable value, then you'll have to put "global
> $numbers1" into this function, or move also the variable initialization
> into the function or pass the variable as the function parameter.
>
> If you post your code, then make it a bit more readable.
> Compare the one you sent with the one below (no changes in syntax made).
>
> Hilarion
>
> <?php
>
> srand ((float) microtime() * 10000000);
> for( $i=0; $i<=90; $i++ )
> {
> global $numbers1;
> $numbers1[] = $i;
> global $numbers2;
> $numbers2[] = $i;
> global $numbers3;
> $numbers3[] = $i;
> }
>
> function drawtable()
> {
> unset( $numbers1[0] );
> for( $i=0; $i<5; $i++ )
> {
> $name = array_rand( $numbers1 ); //<<<<<<<<<<<<<<<<<<<<line 15
> $name2[] = $numbers1[$name];
> if ($name < 10)
> {
> for( $j=0; $j<10; $j++ )
> {
> unset( $numbers1[$j] );
> }
> }
> else if ($name>=10 && $name <=79)
> {
> for( $loop=10; $loop<=70; $loop+=10 )
> {
> for( $loop2=0; $loop2<=9; $loop2++ )
> {
> if(($loop+$loop2)==$name)
> {
> for( $j=$loop; $j<=($loop+9); $j++ )
> {
> unset( $numbers1[$j] );
> }
> }
> }
> }
> }
> else
> {
> for( $j=80; $j<=90; $j++ )
> {
> unset( $numbers1[$j] );
> }
> }
> }
> sort( $name2 );
>
> for( $loop=10; $loop<=90; $loop+=10 )
> {
> print "<td>";
> for( $i=0; $i<5; $i++ )
> {
> $check = $name2[$i];
> if ($check==90 && $loop==90)
> {
> print $check;
> }
> else if ($check<($loop) && $check>=($loop-10))
> {
> print $check;
> }
> else
> {
> print " ";
> }
> }
> print "</td>";
> }
>
> //foreach( $name2 as $key => $value )
> //{
> // print $key." -key, $value -value - name2<br>\n";
> //}
> }
> ?>
> <html>
> <head>
> <title>Bingo !!</title>
> </head>
> <body>
> <table align="center" border="1">
> <tr>
> <?php
> drawtable();
> ?>
> </tr>
> </table>
> </body>
> </html>
>



Ha .. What ??!!??

2004-12-16, 3:56 am

Many thanks again, you have cleared that up for me. Thanks again, Scruffy.
"Hilarion" <hilarion@SPAM.op.SMIECI.pl> wrote in message
news:cpeh62$ajs$1@news.onet.pl...
>
> <?php
>
> srand ((float) microtime() * 10000000);
>
> function drawtable()
> {
> for( $i=0; $i<=90; $i++ )
> {
> $numbers1[] = $i;
> $numbers2[] = $i;
> $numbers3[] = $i;
> }
>
> unset( $numbers1[0] );
> for( $i=0; $i<5; $i++ )
> {
> // rest of the script as it was
> ?>
>
>
>
>
> <?php
>
> srand ((float) microtime() * 10000000);
>
>
> for( $i=0; $i<=90; $i++ )
> {
> $numbers1[] = $i;
> $numbers2[] = $i;
> $numbers3[] = $i;
> }
>
> function drawtable( $numbers1 )
> {
> unset( $numbers1[0] );
>
> // rest of the function as it was
> }
> ?>
> <html>
> <head>
> <title>Bingo !!</title>
> </head>
> <body>
> <table align="center" border="1">
> <tr>
> <?php
> drawtable( $numbers1 );
> ?>
> </tr>
> </table>
> </body>
> </html>
>
>



peter

2004-12-18, 3:55 pm

should it not be $numbers[1] and not $numbers1[]

Look at functions array_pop() snd array_push() on www.php.net


Ha .. What ??!!?? wrote:
> Hi all, I have recently completed a php course (beginner I hasten to add)
> and one of the unfinished projects was to create a bingo card. I have made a
> working version of part of it (creates five random numbers from 90, and
> orders them into columns based on their number) but when i try to run this
> as a function, I get 'Warning: array_rand(): First argument has to be an
> array in blah/blah.php on line 15'. Any help would be gratefully recieved,
> thanks, Scruffy.
> code follows >>>>>
> <?php
> srand ((float) microtime() * 10000000);
> for($i=0;$i<=90;$i++){
> global $numbers1;
> $numbers1[]=$i;
> global $numbers2;
> $numbers2[]=$i;
> global $numbers3;
> $numbers3[]=$i;rideon.ch/php/schnee.php?location=%5Ball%5D
> }
> function drawtable(){
> unset($numbers1[0]);
> for ($i=0;$i<5;$i++)
> {
> $name=array_rand($numbers1); <<<<<<<<<<<<<<<<<<<<line 15
> $name2[]=$numbers1[$name];
> if ($name<10) {for ($j=0;$j<10;$j++){unset($numbers1[$j]);}}else
> if ($name>=10 && $name <=79){
> for ($loop=10;$loop<=70;$loop+=10)
> {
> for($loop2=0;$loop2<=9;$loop2++)
> {
> if(($loop+$loop2)==$name){for
> ($j=$loop;$j< =($loop+9);$j++){unset($numbers1[$j]);}}

> }
> }
> }else
> {for ($j=80;$j<=90;$j++){unset($numbers1[$j]);}}
> }
> sort($name2);
>
> for($loop=10;$loop<=90;$loop+=10)
> {
> print "<td>";
> for ($i=0;$i<5;$i++)
> {
> $check=$name2[$i];
> if ($check==90&&$loop==90) {print $check;}else
> if($check<($loop)&&$check>=($loop-10)){print $check;}else{print
> " ";}
>
> }
> print "</td>";
> }
>
> //foreach($name2 as $key=>$value){
> //print $key." -key, $value -value - name2<br>\n";
> //}
> }
> ?<html>
> <head>
> <title>Bingo !!</title>
> </head>
> <body>
> <table align="center" border="1">
> <tr>
> <?php
> drawtable();
> ?>
> </tr>
> </table>
> </body>
> </html>>
>
>

peter

2004-12-20, 8:56 pm

should it not be $numbers[1] and not $numbers1[]

Look at functions array_pop() snd array_push() on www.php.net


Ha .. What ??!!?? wrote:
> Hi all, I have recently completed a php course (beginner I hasten to add)
> and one of the unfinished projects was to create a bingo card. I have made a
> working version of part of it (creates five random numbers from 90, and
> orders them into columns based on their number) but when i try to run this
> as a function, I get 'Warning: array_rand(): First argument has to be an
> array in blah/blah.php on line 15'. Any help would be gratefully recieved,
> thanks, Scruffy.
> code follows >>>>>
> <?php
> srand ((float) microtime() * 10000000);
> for($i=0;$i<=90;$i++){
> global $numbers1;
> $numbers1[]=$i;
> global $numbers2;
> $numbers2[]=$i;
> global $numbers3;
> $numbers3[]=$i;rideon.ch/php/schnee.php?location=%5Ball%5D
> }
> function drawtable(){
> unset($numbers1[0]);
> for ($i=0;$i<5;$i++)
> {
> $name=array_rand($numbers1); <<<<<<<<<<<<<<<<<<<<line 15
> $name2[]=$numbers1[$name];
> if ($name<10) {for ($j=0;$j<10;$j++){unset($numbers1[$j]);}}else
> if ($name>=10 && $name <=79){
> for ($loop=10;$loop<=70;$loop+=10)
> {
> for($loop2=0;$loop2<=9;$loop2++)
> {
> if(($loop+$loop2)==$name){for
> ($j=$loop;$j< =($loop+9);$j++){unset($numbers1[$j]);}}

> }
> }
> }else
> {for ($j=80;$j<=90;$j++){unset($numbers1[$j]);}}
> }
> sort($name2);
>
> for($loop=10;$loop<=90;$loop+=10)
> {
> print "<td>";
> for ($i=0;$i<5;$i++)
> {
> $check=$name2[$i];
> if ($check==90&&$loop==90) {print $check;}else
> if($check<($loop)&&$check>=($loop-10)){print $check;}else{print
> " ";}
>
> }
> print "</td>";
> }
>
> //foreach($name2 as $key=>$value){
> //print $key." -key, $value -value - name2<br>\n";
> //}
> }
> ?<html>
> <head>
> <title>Bingo !!</title>
> </head>
> <body>
> <table align="center" border="1">
> <tr>
> <?php
> drawtable();
> ?>
> </tr>
> </table>
> </body>
> </html>>
>
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com