Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Newbie question, probably stupidly simple .. sorry !!
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>>



Report this thread to moderator Post Follow-up to this message
Old Post
Ha .. What ??!!??
12-11-04 01:56 AM


Re: Newbie question, probably stupidly simple .. sorry !!
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>



Report this thread to moderator Post Follow-up to this message
Old Post
Hilarion
12-11-04 01:56 AM


Re: Newbie question, probably stupidly simple .. sorry !!
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>
>



Report this thread to moderator Post Follow-up to this message
Old Post
Ha .. What ??!!??
12-11-04 01:55 PM


Re: Newbie question, probably stupidly simple .. sorry !!
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.



Report this thread to moderator Post Follow-up to this message
Old Post
Ha .. What ??!!??
12-11-04 01:55 PM


Re: Newbie question, probably stupidly simple .. sorry !!
> 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>



Report this thread to moderator Post Follow-up to this message
Old Post
Hilarion
12-11-04 01:55 PM


Re: Newbie question, probably stupidly simple .. sorry !!
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>
>
>



Report this thread to moderator Post Follow-up to this message
Old Post
Ha .. What ??!!??
12-12-04 01:55 AM


Re: Newbie question, probably stupidly simple .. sorry !!
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>
>



Report this thread to moderator Post Follow-up to this message
Old Post
Ha .. What ??!!??
12-15-04 08:57 PM


Re: Newbie question, probably stupidly simple .. sorry !!
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>
>
>



Report this thread to moderator Post Follow-up to this message
Old Post
Ha .. What ??!!??
12-16-04 08:56 AM


Re: Newbie question, probably stupidly simple .. sorry !!
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>>
>
>

Report this thread to moderator Post Follow-up to this message
Old Post
peter
12-18-04 08:55 PM


Re: Newbie question, probably stupidly simple .. sorry !!
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>>
>
>

Report this thread to moderator Post Follow-up to this message
Old Post
peter
12-21-04 01:56 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP Language archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:51 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.