Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
counting the amount in an array / 90 then and place into another array
Old 09-06-2007, 08:48 AM counting the amount in an array / 90 then and place into another array
Neller's Avatar
Novice Talker

Posts: 13
Name: Alan Neller
Location: England
Trades: 0
Ok guys sorry about the lame title but couldn't think of a better way to describe what I'm trying to do I will try to do it better below:

Ok think of it as a football (soccer for some) match. I have an array of 60 commentry clips

Code:
 
$commentry_cips = array(1 => 'kicks off',2 => 'takes a shot',3 => 'etc etc);
So I have all my commentry needed for the match, then I set the length of the match.

Code:
 
$full_time = 90 + rand(1,5);
$curr_time = 0;
Ok.. now I need to somehow get all the commentry clips to print out at random times within the 90mins I used a basic loop like below

Code:
 
foreach($commentry_clips as $key => $value) {
    $curr_time = rand($curr_time +1, $curr_time +5);
 
    //make sure clips don't get printed out after full time
     if(!$curr_time >= $full_time) {
          echo $curr_time . ': ' . $value;
     }
}
The above works ok except sometimes a few get missed out because of the rand();

for example when it's looping if the rand() line ends up picking numbers 2,3,2,3,4,1,2,3 etc then all the clips get used, if it picks 4,5,3,4,5,4 then becuse it hits the $full_time var quick loads get chopped off.

what I need is to get all the commentry clips printed out onto the page at random times but they all need to fit in within the 90mins of play.

and of course say i refreshed the page it would jumble them all up again so they came out at different times again

Thanks in advance and sorry for the long winded explaination just tried not to miss anything out

Last edited by Neller; 09-06-2007 at 08:57 AM..
Neller is offline
Reply With Quote
View Public Profile Visit Neller's homepage!
 
 
Register now for full access!
Old 09-06-2007, 09:06 AM Re: counting the amount in an array / 90 then and place into another array
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
I'm not sure if I completely understand what you are trying to do but you can try using the shuffle() function to randomize your array and then print out each one or you can use the array_rand fucntion to pick a certain number of elements out of your array at random. Neither of these will result in repeating indexes.

shuffle: http://us.php.net/shuffle
array_rand: http://us3.php.net/array_rand
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 09-06-2007, 09:14 AM Re: counting the amount in an array / 90 then and place into another array
Neller's Avatar
Novice Talker

Posts: 13
Name: Alan Neller
Location: England
Trades: 0
Quote:
Originally Posted by NullPointer View Post
I'm not sure if I completely understand what you are trying to do but you can try using the shuffle() function to randomize your array and then print out each one or you can use the array_rand fucntion to pick a certain number of elements out of your array at random. Neither of these will result in repeating indexes.

shuffle: http://us.php.net/shuffle
array_rand: http://us3.php.net/array_rand
Yeah I already use the shuffle() to jumble up my array, they dont get repeated its more they get missed out.

I basicly need to print out the array in a loop (easy) but I need that to run along side the minutes.

Say a football match is 90mins long I need those commentry clips to print out with a random number in that 90mins for example


2: play is through on goal
6: player shoots
15: player dives for a penalty
20: GOOOOOOOOOOAL

So i need to make my array get random time(s) and print out in those times, I cant manually set the numbers 2,6 etc because everytime the page was refreshed say i need that match report / times to be different.

I also want to make sure I use every clip in my commentry array
Neller is offline
Reply With Quote
View Public Profile Visit Neller's homepage!
 
Old 09-06-2007, 09:18 AM Re: counting the amount in an array / 90 then and place into another array
Neller's Avatar
Novice Talker

Posts: 13
Name: Alan Neller
Location: England
Trades: 0
Just thought of a way I can do it and going to search php.net for a function to do it now But I will still post what I need incase I don't find it..

Is there a function that could say pull 4 random numbers between 1-10
Neller is offline
Reply With Quote
View Public Profile Visit Neller's homepage!
 
Old 09-06-2007, 09:28 AM Re: counting the amount in an array / 90 then and place into another array
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Here's what I would do in that case:

-Create a session
-Store your array of messages as a session variable
-Store another array as a session variable and everytime you print out a message place it at the end of the this array in order to keep track of the order you have printed out the messages.
-Everytime someone goes to your page it should print out all of the messages in the stored array plus another random one from the other array
-use the header('refresh: 60; url=yourpage.php') function to refresh your page every 60 seconds.

PHP Code:
<?php

session_start
();
if(!isset(
$_SESSION['messages']))
{
    
$messages = array(message1message2message3);
    
$messages shuflfle($messages);
    
$_SESSION['messages'] = $messages;
    
$_SESSION['count'] = 0;
}
else
    
$messages $_SESSION['messages'];

if(!isset(
$_SESSION['printed']))
{
    
$printed = array($messages[0]);
    
$_SESSION['count']++;
}
else
{
     
$printed $_SESSION['printed'];
     
$printed $messages[$_SESSION['count']];
     
$_SESSION['count']++;
}
print_r($printed);

header('refresh: 60; thispage.php');
?>
This is all of the top of my head so there might be some errors.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 09-06-2007 at 09:40 AM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 09-06-2007, 09:33 AM Re: counting the amount in an array / 90 then and place into another array
Neller's Avatar
Novice Talker

Posts: 13
Name: Alan Neller
Location: England
Trades: 0
Quote:
Originally Posted by NullPointer View Post
Here's what I would do in that case:

-Create a session
-Store your array of messages as a session variable
-Store another array as a session variable and everytime you print out a message place it at the end of the this array in order to keep track of the order you have printed out the messages.
-Everytime someone goes to your page it should print out all of the messages in the stored array plus another random one from the other array
-use the header('refresh: 60; url=yourpage.php') function to refresh your page every 60 seconds.

I'll try to code something up to this effect
thank you Null I appreate your help but ive actually just found a way to do what I need don't know why I never thought of it before.

Code:
 
$num = 0;
foreach(range(1,60) as $value) {
 if($num <= 90) {
 $num = rand($num +1,$num +3);
}
}
This gets me my random numbers 1-90 and always makes sure it doesnt go over the 60 (commentry clips) I can now assign these numbers into my commentry clips array and print them our accordingly, everytime someone comes to the page they should always be different.

Thanks again for all your help... if you dont see this before you have done that code please post it still so I can check it out aswell.
Neller is offline
Reply With Quote
View Public Profile Visit Neller's homepage!
 
Reply     « Reply to counting the amount in an array / 90 then and place into another array
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.18010 seconds with 12 queries