Go Back   Web Hosting UK Forums | Linux Windows Dedicated Server and cPanel VPS Hosting Forum > Support > php issues.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-18-2008, 03:34 PM
Member
 
Join Date: Nov 2007
Posts: 30
Red face issues with php survey

Hi guys I wonder if you can help. Basically I'm creating a business to business survey for my employer. My php knowledge is only low level and I'm having an issue I'm not sure how to remedy.

Once you enter the survey and your details, the "id" field in the database auto increments by one. And once the survey is completed the below code will divide the id by five (one in five win chance), and if its an integer they win a prize (digital photo frame). Simple enough, they either win a prize or not with a 1in5 chance. But now my boss wants me to include a second prize, but still a 1in5 chance to win.


$win = $id / 5;


if (preg_match ("/^\d+$/", $win))
{
$winner="Winner";
$messagecode = 22;
}
else {
$winner="no win";
$messagecode = 1983;
}
if ($winner == "Winner"){ $printwin ="Digital Photo Frame WINNER!";} else {$printwin ="No Winner.";}


The problem is my code will only detect the 1 in 5 chance then tell them they've won the photo frame, I'm not sure how to include a second possible prize. What I thought of doing was finding the total amount of people who are going to survey (say 8000) then saying:

if id is an integer & less than 4000, win a camera
if id is an integer & more than 4000, win a digital frame
if not an integer, no win.

I think this would solve my multiple prize problem, but I'm not sure of the syntax. Could any of you kind souls help a brother out?
Reply With Quote
  #2 (permalink)  
Old 02-18-2008, 05:20 PM
Senior Member
 
Join Date: Jan 2007
Posts: 939
Default

Hi, am not sure what you mean by 1 in 5 chance?? Does that mean 2 winners every 10?
It is a bit of a puzzler as i only ever randomize to pick winners or set some kind of status, but by your post above i can see why you need to do it that way as i assume you pick winners straight away and not after the survey has finnished (which would be easy).
Or have i misunderstood? Anyway you could use your code and also check whether it is odd or even. So say every fifth wins first and every tenth wins second prize.

Code:
 
<?php
$win = $id/ 5; 
 
if(!is_float($win)&&($win &1)) {
$winner="Winner";
$messagecode = 22;
$printwin ="Digital Photo Frame WINNER!";
}
elseif (!is_float($win)&&(!($win &1))) {
$messagecode = 1983;
$printwin ="Second Prize.";
}
else {$printwin ="No Winner.";}
echo "$printwin";
?>
Same as yours apart from the odd-even check and i replaced preg_match with is_float...both perfectly fine
__________________
West Dorset Community
Reply With Quote
  #3 (permalink)  
Old 02-18-2008, 05:28 PM
Member
 
Join Date: Nov 2007
Posts: 30
Default

That may be of some help. But can you explain why you didnt use preg match? What does float actually do? Is that the operator which determines odd and even?
Reply With Quote
  #4 (permalink)  
Old 02-18-2008, 05:35 PM
Senior Member
 
Join Date: Jan 2007
Posts: 939
Default

You can use either, matter of preference really, it is just that is_float is meant for that purpose, it checked whether the number is floating (is not a whole number) so !is_float is opposite to is_float.

$win &1 checks whether the number is odd or not
!$win &1 is not odd so is even
__________________
West Dorset Community
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 12:23 PM
Member
 
Join Date: Nov 2007
Posts: 30
Default

Sorry for not answering sooner. Been busy busy.. Yes that code works a treat thank you very much for your help..

As I say it all works fine. Only problem is now my boss has stated he might want to change the odds off winning to a sliding scale, so the sooner you enter the survey the better chance of winning you have.

I guess I would code it to count the id number and if it is higher than say 200 then divide it by 6 instead of 5 then check if its odd or even whole number. And keep doing this for higher numbers until the odds are much lower at the end.

How would you suggest I write the code for this? Sorry to ask. This is what happens when the boss messes with things.....!!!
Reply With Quote
  #6 (permalink)  
Old 02-29-2008, 12:54 AM
Senior Member
 
Join Date: Jan 2007
Posts: 939
Default

Umm, I would have to think about that one!

First thought would be to use more statements as in:
if ($id>100){
$odds =5
}

And others to suit.


Then to change your $win to :
$win= $id / $odds;

Off the top of my head that is all i have got!
__________________
West Dorset Community
Reply With Quote
  #7 (permalink)  
Old 02-29-2008, 02:44 PM
Member
 
Join Date: Nov 2007
Posts: 30
Default

Sounds logical. I'll give it a go if I'm asked to change it.. Thanks a lot, you've been a great help. My survey would be in tatters without the coding help...Cheers!
Reply With Quote
  #8 (permalink)  
Old 02-29-2008, 03:04 PM
Senior Member
 
Join Date: Jan 2007
Posts: 939
Default

Quote:
Originally Posted by shaunlevett View Post
Sounds logical. I'll give it a go if I'm asked to change it.. Thanks a lot, you've been a great help. My survey would be in tatters without the coding help...Cheers!
You're welcome. good luck.

Just one thought with the above. The second prize won't work if you divide by an even number. Where you were using 5 you got odd and even results (5, 10, 15, 20, etc), so odd were one prize and even another.
If you use 6 say, then there won't be any odd numbers (6,12,18,24,etc)
__________________
West Dorset Community
Reply With Quote
  #9 (permalink)  
Old 02-29-2008, 03:05 PM
Member
 
Join Date: Nov 2007
Posts: 30
Default

Oh yes..I could always do 1 in 7 in that case
Reply With Quote
  #10 (permalink)  
Old 03-06-2008, 10:32 AM
Member
 
Join Date: Nov 2007
Posts: 30
Default

Hello again.....Yes the survey seems to be behaving pretty well thanks
to your input jon.
I've used the below code which is working fine

if ($id <= 100){
$odds =5;
}
if ($id > 100){
$odds =9;
}

Just one question. I need to add different odds variables ie: if the id number is 100-250 the odds are 1 in 9. And if the id number is 250-300 the odds are 1in 19 etc.... can i do this the same way as above like this

if ($id <= 100){
$odds =5;
}
if ($id > 100){
$odds =9;
}
if ($id > 250){
$odds =19;
}

Would this work? I'm just worried that it will think that the last two statement are both true and then mess the whole thing up if you get me?
Reply With Quote
  #11 (permalink)  
Old 03-06-2008, 11:12 AM
Member
 
Join Date: Nov 2007
Posts: 30
Default

I have just added these

if ($id <= 100){
$odds =5;
}
if ($id > 100){
$odds =9;
}
if ($id > 250){
$odds =19;
}
if ($id > 500){
$odds =49;
}

So all I want to check is that if the id number was say 300, then it would'nt think all the statements are true then just explode with confusion. Or will on variable replace the other when it becomes true....Sorry to nag guys. If anyone can check this syntax it would be great thanks
Reply With Quote
  #12 (permalink)  
Old 03-06-2008, 12:35 PM
Senior Member
 
Join Date: Jan 2007
Posts: 939
Default

looks good to me Shaun, yes one will replace the other.#If you want to check then just upload a php page with this code:
PHP Code:
<?php
    $id
=$_REQUEST['id'];
    if (
$id <= 100){
$odds =5;
}
if (
$id 100){
$odds =9;
}
if (
$id 250){
$odds =19;
}
if (
$id 500){
$odds =49;
}
echo 
$odds
 
?>
Then goto the url you uploaded it to and at the end of the address just add ad id variable, so for axample:

http://www.mydomain.com/test.php?id=300

and try other numbers, but think you will find it works as you want it to
__________________
West Dorset Community
Reply With Quote
  #13 (permalink)  
Old 03-06-2008, 02:11 PM
Member
 
Join Date: Nov 2007
Posts: 30
Default

Thanks jon......You are a life saver. May the lord shine upon you..Or if you are an athiest, may he not shine upon you! Anyway thanks a lot. I'll use this code to see if its working
Reply With Quote
Reply

Thread Tools
Display Modes

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 On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:37 AM.
Copyright 2002-2007 WebHosting.uk.com. All rights reserved.
Web Hosting UK Forum