11 Replies - 979 Views - Last Post: 18 December 2008 - 06:07 PM Rate Topic: -----

#1 RayRayAngel  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 15-December 08

Random Array

Posted 18 December 2008 - 04:28 AM

Is it possible to make something like this?
// add to the weight and item arrays below
$weight[0] = 1; $item[0] = "Try Again 1";
$weight[1] = 1; $item[1] = "Try Again 2";
$weight[2] = 1; $item[2] = "Try Again 3";
// end of arrays
for ( $a=0; $a<count($weight); $a++ ) 
for ( $b=1; $b<=$weight[$a]; $b++ ) $pick[] = $a;
$selected = $item[$pick[rand(0,count($pick)-1)]];
echo $selected;

But instead of having it say "Try Again" execute some SQL like this:
mysql_query("UPDATE Users SET Back1 = '1' WHERE UserID='$user'");


EDIT:
I forgot the weight value is VERY important.

This post has been edited by RayRayAngel: 18 December 2008 - 04:35 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Random Array

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2993
  • View blog
  • Posts: 10,342
  • Joined: 08-August 08

Re: Random Array

Posted 18 December 2008 - 06:03 AM

What's your goal? Do you want to have the user guess a weight and keep tabs of how often they get it wrong? That would go something like this:
<?php
$guess = $_POST['guess'];
while($guess != $weight)
	{
	function_to_record_guess($guess);
	function_to_display_form();		
	}
?>


Was This Post Helpful? 0
  • +
  • -

#3 nightscream  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 237
  • Joined: 04-December 08

Re: Random Array

Posted 18 December 2008 - 06:03 AM

and what do you want to store in the $item array?
because an update query doesn't return anything(if i'm not mistaking)
Was This Post Helpful? 0
  • +
  • -

#4 RayRayAngel  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 15-December 08

Re: Random Array

Posted 18 December 2008 - 06:09 AM

By weight I mean how often an item shows up. The higher the weight the more often it shows up. Does that make any sense?
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2993
  • View blog
  • Posts: 10,342
  • Joined: 08-August 08

Re: Random Array

Posted 18 December 2008 - 06:43 AM

Maybe something like this:
<?php
$weight = array();
$item = array(0,1,2,3,4,5,6,7,8,9);

for($a=0;$a<count($item);$a++)
	{
	$selected = rand(0,9);
	for($b=0;$b<count($item);$b++)
		{
		if ($selected == $item[$b])
			{
			$weight[$b]++;
			}
		}
	}
var_dump($weight);
?>


This post has been edited by CTphpnwb: 18 December 2008 - 06:44 AM

Was This Post Helpful? 1
  • +
  • -

#6 RayRayAngel  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 15-December 08

Re: Random Array

Posted 18 December 2008 - 01:07 PM

Hmmm that might be what I'm looking for. I am going to have to look at it some more and try to understand it a bit better. This all goes back to the game I am developing and when someone selects a number I want it to randomly post some text like Sorry Try Again or in the case of certain items tell them Great Work and then post to the SQL database which item they won. I think this will do that.
Was This Post Helpful? 0
  • +
  • -

#7 RayRayAngel  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 15-December 08

Re: Random Array

Posted 18 December 2008 - 02:23 PM

This is what I originally had to put it in context:
// add to the weight and item arrays below
$weight[0] = 10; $item[0] = "You Won Color Blitz Wallpaper";
$weight[1] = 100; $item[1] = "Sorry, Try Again";
$weight[2] = 1; $item[2] = "You Won";
// end of arrays
for ( $a=0; $a<count($weight); $a++ ) 
for ( $b=1; $b<=$weight[$a]; $b++ ) $pick[] = $a;
$selected = $item[$pick[rand(0,count($pick)-1)]];
echo $selected;



See I want Sorry to show up the most often, then sometimes they win a rare item and very very rarely they have a chance at winning another prize. Instead of just echoing what they won I want to echo that text and then execute an SQL update.
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2993
  • View blog
  • Posts: 10,342
  • Joined: 08-August 08

Re: Random Array

Posted 18 December 2008 - 02:56 PM

Why not do something like this?
(Not tested - done from iPhone!)
$x=rand(0,100);
if ($x<= 60) {
echo "sorry";
} else
if($x>60 && $x <=90) {
echo "you win prize A";
} else 
if($x>90) {
echo "you win special prize!";
}


Was This Post Helpful? 1
  • +
  • -

#9 RayRayAngel  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 15-December 08

Re: Random Array

Posted 18 December 2008 - 05:02 PM

EXACTLY What I wanted. So I do a random number then do if statements. Excellent!

View PostCTphpnwb, on 18 Dec, 2008 - 01:56 PM, said:

Why not do something like this?
(Not tested - done from iPhone!)
$x=rand(0,100);
if ($x<= 60) {
echo "sorry";
} else
if($x>60 && $x <=90) {
echo "you win prize A";
} else 
if($x>90) {
echo "you win special prize!";
}


Was This Post Helpful? 0
  • +
  • -

#10 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Random Array

Posted 18 December 2008 - 05:09 PM

View PostCTphpnwb, on 18 Dec, 2008 - 04:56 PM, said:

(Not tested - done from iPhone!)


Now that's dedication to your craft. Kudos!
Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2993
  • View blog
  • Posts: 10,342
  • Joined: 08-August 08

Re: Random Array

Posted 18 December 2008 - 05:32 PM

View PostValek, on 18 Dec, 2008 - 04:09 PM, said:

View PostCTphpnwb, on 18 Dec, 2008 - 04:56 PM, said:

(Not tested - done from iPhone!)


Now that's dedication to your craft. Kudos!

More like an addiction. The funny thing is, I just got another "not enough experience" rejection from another HR person.
:(
Was This Post Helpful? 0
  • +
  • -

#12 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Random Array

Posted 18 December 2008 - 06:07 PM

I know what you mean by PHP being an addiction. I got dual monitors specifically so I could code while doing other things.

And yeah, the "not enough experience" response is really irritating. Everyone wants experience, but no one's willing to offer any. Kinda defeats things a bit.

I know I'm relatively new to it, time-wise, so I haven't even bothered applying for a job with it. I figure I'll get a degree in it and finish some projects as a portfolio, and go from there. That's the only thing I can think of to do with it, really. That and internships if possible.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1