Can someone HELP me?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

woohoogf
Forum Newbie
Posts: 10
Joined: Thu Dec 02, 2010 7:41 pm

Can someone HELP me?

Post by woohoogf »

Please help me with this.

Code: Select all

<?php
$rand = mt_rand(1,10);
$day = date("j");

$query = mysql_query("UPDATE table SET num=num+$rand WHERE day!='$day'");
?>
When I write this code and test, rand take number and update in all rows the same number.
But I need to UPDATE different numbers (1,10) in all rows.

What is the solution?
Thank You in advance.
Last edited by Benjamin on Fri Dec 03, 2010 10:19 pm, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Can someone HELP me?

Post by Jonah Bron »

Use the SQL RAND() function instead. Manual here:

http://dev.mysql.com/doc/refman/5.0/en/ ... ction_rand

It shows how to get a random number in a given range.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Can someone HELP me?

Post by califdon »

woohoogf wrote:

Code: Select all

<?php
$rand = mt_rand(1,10);
$day = date("j");

$query = mysql_query("UPDATE table SET num=num+$rand WHERE day!='$day'");
?>
When I write this code and test, rand take number and update in all rows the same number.
But I need to UPDATE different numbers (1,10) in all rows.
You have assigned a randomly selected number to the variable $rand. But you have used that fixed number in your query, so it is doing exactly what you asked it to do.

As Jonah said, you must tell the database engine to use a different random number for each row, if that is what you want. SQL is the language that speaks with the database engine, not PHP.
woohoogf
Forum Newbie
Posts: 10
Joined: Thu Dec 02, 2010 7:41 pm

Re: Can someone HELP me?

Post by woohoogf »

Thank You so much for helping.
But I'm having problem with understand what is written in the link.
Sorry me for this. But I'm a little newbie.
So if it's not hard for someone, please write it like in php file like I wrote.
With that way I'll understand.
Thank You in advance.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Can someone HELP me?

Post by califdon »

woohoogf wrote:Thank You so much for helping.
But I'm having problem with understand what is written in the link.
Sorry me for this. But I'm a little newbie.
So if it's not hard for someone, please write it like in php file like I wrote.
With that way I'll understand.
Thank You in advance.
MySQL Manual: wrote:To obtain a random integer R in the range i <= R < j, use the expression FLOOR(i + RAND() * (j – i)). For example, to obtain a random integer in the range the range 7 <= R < 12, you could use the following statement:

SELECT FLOOR(7 + (RAND() * 5));
Applying this to your problem, it should be something like this:

Code: Select all

<?php
$day = date("j");

$query = mysql_query("UPDATE table SET num = num + FLOOR(1 + RAND()*9) WHERE day <> '$day'");
?>
woohoogf
Forum Newbie
Posts: 10
Joined: Thu Dec 02, 2010 7:41 pm

Re: Can someone HELP me?

Post by woohoogf »

califdon wrote:
woohoogf wrote:Thank You so much for helping.
But I'm having problem with understand what is written in the link.
Sorry me for this. But I'm a little newbie.
So if it's not hard for someone, please write it like in php file like I wrote.
With that way I'll understand.
Thank You in advance.
MySQL Manual: wrote:To obtain a random integer R in the range i <= R < j, use the expression FLOOR(i + RAND() * (j – i)). For example, to obtain a random integer in the range the range 7 <= R < 12, you could use the following statement:

SELECT FLOOR(7 + (RAND() * 5));
Applying this to your problem, it should be something like this:

Code: Select all

<?php
$day = date("j");

$query = mysql_query("UPDATE table SET num = num + FLOOR(1 + RAND()*9) WHERE day <> '$day'");
?>


Thank You so much. I put the script You wrote and it's working as I wanted.
But I want to ask something.
With this script from what number to what is the rand?

From 1 to 9 ?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Can someone HELP me?

Post by califdon »

Code: Select all

FLOOR(1 + RAND()*9) 
The FLOOR() function returns the largest integer that is equal to or lower than the floating point number that is given as the argument. Thus, FLOOR(3.14159) is 3, FLOOR(127.4) is 127. The RAND() function returns a pseudo-random floating point number that is between 0 and 1. If you multiply RAND() by some integer, you will get a floating point number between 0 and that integer. So the expression above will return an integer between 1 and 10 (1 + 9).

[Edit: Now that I look at that again, it is unlikely to ever produce 10 because that's the extreme upper limit. In order to get the value 10 to occur, you'd probably need to change the 9 to a 10.]
Last edited by califdon on Fri Dec 03, 2010 2:22 pm, edited 1 time in total.
Reason: Second thoughts
woohoogf
Forum Newbie
Posts: 10
Joined: Thu Dec 02, 2010 7:41 pm

Re: Can someone HELP me?

Post by woohoogf »

Thank You very much.
But the problem is that I want to have averagely 6, using only 1,2,3,4,5,6,7,8,9 numbers
For example with rand() function I can write this:

Code: Select all

<?php 
$rand = rand(1,12)
if ($rand > 8 )
{
$rand = 9;
}
and this will bring me this 1,2,3,4,5,6,7,8,9,9,9,9
The sum = 72 and total numbers are 12.
So 72 / 12 = 6.
In average I got 6.

What I must write to have average 6 with FLOOR() function?
If it's not hard please guide me this with example of php code like I wrote.
Thank You in advance.
Last edited by Benjamin on Fri Dec 03, 2010 10:19 pm, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Can someone HELP me?

Post by califdon »

I think you are confusing PHP and SQL. What I was suggesting is not PHP, it is SQL. And I don't think your concept of an average value can be applied to a random series. Why don't you tell us what you are trying to accomplish and perhaps we can help you.
woohoogf
Forum Newbie
Posts: 10
Joined: Thu Dec 02, 2010 7:41 pm

Re: Can someone HELP me?

Post by woohoogf »

As I'm newbie I don't know that things.
I never met SQL language so I thought it was PHP.

I tell I want to put in table rows 1-9 number with averagely 6.
It's possible, but I don't know how.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Can someone HELP me?

Post by califdon »

SQL is a completely separate language that communicates with a database.

PHP is a language to perform logical operations with values and send HTML to a browser that has requested a web page or other information. One of the things PHP can do is send SQL statements to a database.

It would help you to read beginning tutorials on all these languages, which are ALL DIFFERENT:
HTML
CSS
Javascript
SQL

You still haven't explained what you want to accomplish. Saying "I want to put in table rows that average 6" doesn't mean much. WHY do want to do that? That's what I have been asking.
woohoogf
Forum Newbie
Posts: 10
Joined: Thu Dec 02, 2010 7:41 pm

Re: Can someone HELP me?

Post by woohoogf »

oh I just understood all the system how it works as You explained here:
FLOOR(3.14159) is 3, FLOOR(127.4) is 127. The RAND() function returns a pseudo-random floating point number that is between 0 and 1. If you multiply RAND() by some integer, you will get a floating point number between 0 and that integer. So the expression above will return an integer between 1 and 10 (1 + 9).

[Edit: Now that I look at that again, it is unlikely to ever produce 10 because that's the extreme upper limit. In order to get the value 10 to occur, you'd probably need to change the 9 to a 10.]
Thank you very much for explaining this all.
I just want to ask why there is no case to get 10? As RAND()*9 can give 9 and 1+9 is 10.

And what about what I want to make:
I want to make something like referral bot system, but not referral bot.
I need that script for my work which works like referral bot in PTCs.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Can someone HELP me?

Post by califdon »

I'm glad you followed the explanation and learned something. The reason it needs to go to 10 is that RAND() in SQL returns a value between 0.00 and 1.00. Since the number it returns is random (pseudo-random, actually, meaning that the results will look random to us, but may not meet the mathematical definition of being truly random), it will almost never return the exact same floating point number. It might return 0.99765302 one time and 0.99153827 another time, but in order to be used with the FLOOR() function, it would have to be precisely 1.000000000 so that when it is multiplied by 9 it would equal 9, to be added to 1, making 10. That would almost never occur, so you need to make sure that it will produce a number higher than 9, which requires that the range must be 1 to 10. I hope that makes sense.
woohoogf wrote:I want to make something like referral bot system, but not referral bot.
I need that script for my work which works like referral bot in PTCs.
I'm afraid you're not explaining WHY you want to make the "average" (which I don't think really even applies here) any particular number. The whole point of a random selection is to not have any pattern to the results. I think maybe what you are thinking of is that you want there to be an equal chance for any particular selection to occur. That's exactly what a random choice does for you. There isn't any concept of "average".
woohoogf
Forum Newbie
Posts: 10
Joined: Thu Dec 02, 2010 7:41 pm

Re: Can someone HELP me?

Post by woohoogf »

Thank You very much now I understand all.

Just I thought it takes between 0,1,2,3,4,5,6,7,8,9, but now I understand that it takes from 0.0000000 to 8.9999999
So everything is clear thank You :)
woohoogf
Forum Newbie
Posts: 10
Joined: Thu Dec 02, 2010 7:41 pm

Re: Can someone HELP me?

Post by woohoogf »

Sorry but one more question :)

Is it ok if rows are for example 5000?

or if it's more then 100 or 1000, there can be problem, error, bug I don't know.
Or maybe it will work properly even if it is 10.000 ?

Thank You in advance.
Post Reply