?
Solved

PHP Ramdom selector working details.

Posted on 2006-06-02
5
Medium Priority
?
246 Views
Last Modified: 2013-12-12
I am writing a report on the workings of a Radom selector that I have on our website. The selector is built in PHP coding (version 4.3.11) on a MySQL database (version  4.0.27)

How it works is once enabled and a member joins that are being placed into a member’s database. When we click the select winner button it generates a member randomly.

We use this for competition purposes. We need to know the functionality of the coding. We need as much information on the operating procedures as possible. If there are any other examples on the web.

Here is the code

Button.
 <form name="formwinner" method="post" action="membergetmember.php">
        <input type="hidden" name="txtwinner" value="<?print($arr[rand(0,$counter-1)]);?>">
        <input type="submit" name="Submit2" value="Select Winner !">
      </form>

Code.
//print("randon no is: ".rand(0,$counter-1)." ");
      //for($jj=0;$jj<$counter;$jj++)
//      {
//       print($jj." ".$arr[$jj]."<BR>");
//      }

0
Comment
Question by:Adamylo
  • 3
  • 2
5 Comments
 
LVL 10

Expert Comment

by:Kshitij Ahuja
ID: 16822837
Your Code is using $counter variable, i need to know where is this counter coming from,imean how you are generating this $counter variable.


**************************************************************************
Button.
 <form name="formwinner" method="post" action="membergetmember.php">
        <input type="hidden" name="txtwinner" value="<?print($arr[rand(0,$counter-1)]);?>">
        <input type="submit" name="Submit2" value="Select Winner !">
      </form>
**************************************************************************
The above section of code is basically a form that is used to feed the values to the php script.
> Where is  $counter coming from? how you are generating it?
> what is the use of "txtwinner" being passed?
**************************************************************************

Code.
//print("randon no is: ".rand(0,$counter-1)." ");
     //for($jj=0;$jj<$counter;$jj++)
//     {
//      print($jj." ".$arr[$jj]."<BR>");
//     }


$arr['$jj'] ????????

Frankly it is difficult to understand the functionality like this.You will need to post your full code here .or alteast the part showing all variable u are using here in the above code.
0
 

Author Comment

by:Adamylo
ID: 16822877
This is the whole code

<?

## v5.20 -> may. 20, 2005

session_start();




if ($_SESSION['adminarea']!="Active") {

      echo "<script>document.location.href='login.php'</script>";

} else {



include_once ("../config/config.php");



if (isset($_POST['savesettsok'])) {

      if (count($_POST['delete'])>0) {

            for ($i=0;$i<count($_POST['delete']);$i++) {

                  $deletec[$i]=mysql_query("DELETE FROM probid_suggested_categories WHERE id='".$_POST['delete'][$i]."'");

            }

      }

      $savedSettings="yes";

}



include ("header.php");



$valuee=$_POST['txtmgm'];
$qry="update mgmsettings set val=".$valuee;
$vall=mysql_query($qry);

if(isset($_POST['Submit3']))
{
$qry="Delete from refer";
$vall=mysql_query($qry);
}

?>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td rowspan="2"><img src="images/i_tables.gif" border="0"></td>
    <td width="100%"><img src="images/pixel.gif" height="24" width="1"></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td width="100%" align="right" background="images/bg_part.gif" class="head"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Member
      Get Member Program</b></font>&nbsp;&nbsp;</td>
    <td><img src="images/end_part.gif"></td>
  </tr>
</table>
<br>
<? echo ($savedSettings=="yes")?"<p align=\"center\">".$a_lang[S_CATS_UPDATED]."</p>":""; ?>
<table width="100%" border="0" cellpadding="4" cellspacing="2">
  <tr>
    <td colspan="5" align="center" class="c3">
      <form name="formmgm" method="post" action="membergetmember.php">
        <div align="right"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><b>MGM
          Value</b></font>
          <?
            $getscats=mysql_query("SELECT * FROM mgmsettings");
            $rowss=mysql_fetch_object($getscats);
            $mgmvalue=$rowss->val;
            ?>
          <input type="text" name="txtmgm" size="5" value="<?print($rowss->val);?>">
          <input type="submit" name="Submit" value="Change">
          &nbsp;&nbsp; </div>
      </form>
    </td>
  </tr>
  <tr>
    <form name="formreset" method="post" action="membergetmember.php">
      <td colspan="5" align="center" class="c3">
      <input type="submit" name="Submit3" value="Reset MGM Program">
    </td>
      </form>
  </tr>
  <tr>
    <td colspan="5" align="center" class="c3"><b> <font face="Verdana, Arial, Helvetica, sans-serif" size="2">Members
      List</font></b></td>
  </tr>
  <tr>
    <td colspan="2">&nbsp;</td>
    <td colspan="3" align="center">&nbsp;</td>
  </tr>
  <?
if(isset($_POST['txtwinner']))
{
?>
  <tr>
    <td colspan="5" valign="top">
      <div align="right"><b></b></div>
      <div align="left"> <font face="Verdana, Arial, Helvetica, sans-serif" size="2">
        <b>
        <?
              $ress=mysql_query("SELECT username,email FROM probid_users WHERE id=".$_POST['txtwinner']);
              $rowss=mysql_fetch_object($ress);
              print("         Winner ID: ".$_POST['txtwinner']."<BR>");
              print("         Username: ".$rowss->username."<BR>");
            print("         Email: ".$rowss->email."<BR>");
        ?>
        </b></font></div>
    </td>
  </tr>
  <?}?>
  <tr>
    <td colspan="2">&nbsp;</td>
    <td width="240" align="center">&nbsp;</td>
    <td width="205" align="center">&nbsp;</td>
    <td width="188" align="center">&nbsp;</td>
  </tr>
  <tr class="c4">
    <td width="67"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">ID</font></b></td>
    <td align="left" width="251"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">USER
      ID</font></b></td>
    <td width="240" align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">E-MAIL</font></b></td>
    <td width="205" align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Refer
      Persons</font></b></td>
    <td width="188" align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Draw
      Entries </font></b></td>
  </tr>
  <?
      $counter=0;
      $getscats=mysql_query("SELECT * FROM refer");

      while ($row=mysql_fetch_array($getscats)) { ?>
  <input type="hidden" name="cid[]" value="<?=$row['id'];?>">
  <tr class="<? echo (($count++)%2==0)?"c1":"c2"; ?>">
    <td width="67">
      <?=$row['referer'];?>
    </td>
    <td width="251">
      <?
              //getSqlField("SELECT username FROM probid_users WHERE id='".$row['userid']."'","username");
              $ress=mysql_query("SELECT username,email FROM probid_users WHERE id=".$row['referer']);
              $rowss=mysql_fetch_object($ress);
              print($rowss->username);
            ?>
      &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="3">
      <?
        if($row['referer']==$_POST['txtwinner'])
        {
        print("Winner");
        }
        ?>
      </font></b></font></td>
    <td align="center" width="240">
      <?print($rowss->email);?>
    </td>
    <td align="center" width="205">
      <?=$row['totalpersons'];?>
    </td>
    <td align="center" width="188">
      <?//$row['totalpersons'];
            $poll=0;
            $tpersons=$row['totalpersons'];
            for($j=1;$j<=$tpersons;$j+=$mgmvalue)
            {
            $poll=$poll+1;
            $arr[$counter++]=$row['referer'];
            }
            print($poll);
            ?>
    </td>
  </tr>
  <? }
      //print("randon no is: ".rand(0,$counter-1)." ");
      //for($jj=0;$jj<$counter;$jj++)
//      {
//       print($jj." ".$arr[$jj]."<BR>");
//      }
      
      ?>
  <tr>
    <td colspan="5" align="center">
      <form name="formwinner" method="post" action="membergetmember.php">
        <input type="hidden" name="txtwinner" value="<?print($arr[rand(0,$counter-1)]);?>">
        <input type="submit" name="Submit2" value="Select Winner !">
      </form>
    </td>
  </tr>
</table>
<?       include ("footer.php");

} ?>
0
 
LVL 10

Accepted Solution

by:
Kshitij Ahuja earned 500 total points
ID: 16823193
1. Is this code working. I doubt. :)
2. If it is working i would like to see it working first before commenting.
3. I am Assuming the file  you posted here is membergetmember.php.
4. Since i dnt hve your db structure and fields of the table,  i will make the comments mentioned below in the chunk of code itself :



*******************************************

  <?
     $counter=0;        //CREATES A COUNTER VARIABLE AND SETS IT'S VALUE TO ZERO
     $getscats=mysql_query("SELECT * FROM refer"); //FETCHES ALL TEH DATA(ROWS) FROM 'refer' TABLE

     while ($row=mysql_fetch_array($getscats))    // FOR EACH ROW FETCHED ABOVE, IT TRANSFERS THE FIELDS IN THE ROW TO THE $row ARRAY, SO THAT WE CAN USE THEM TO PROCESS AS WE WISH
     { ?>
  <input type="hidden" name="cid[]" value="<?=$row['id'];?>">  //AN INPUT TYPE USED TO PASS VALUE USING POST METHOD; IS HIDDEN ; AND HAS THE VALUE OF THE ID FIELD OF THE FIRST ROW FROM THE $row ARRAY

  <tr class="<? echo (($count++)%2==0)?"c1":"c2"; ?>">
    <td width="67">
      <?=$row['referer'];?>  
    </td>
    <td width="251">
      <?
            //getSqlField("SELECT username FROM probid_users WHERE id='".$row['userid']."'","username");
            $ress=mysql_query("SELECT username,email FROM probid_users WHERE id=".$row['referer']);
            $rowss=mysql_fetch_object($ress);
            print($rowss->username);
           
            //ABOVE SQL IS FETCHING THE USERNAME FROM probid_users TABLE WHERE ID IS SAME AS THE "referer" FIELD OF THE CURRENT ROW BEING PROCESSSED (THE ROW WE FETCHED FROM FIRST SQL PREVIOUS TO THIS ONE).
          ?>
         
         
      &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="3">
      <?
       if($row['referer']==$_POST['txtwinner'])   // IF THE referer FIELD OF THE ROW BEING PROCESSED IS EQUAL TO (OR IS SAME AS) THE VALUE OF "txtwinner" FIELD( WHICH YOU ARE PASSING USING THE FORM BUTTON IN THE END) THEN DO THIS
       {
       print("Winner"); // YES, DO THIS ! PRINT "WINNER" AS THE TEXT
       }
       ?>
      </font></b></font></td>
    <td align="center" width="240">
      <?print($rowss->email);?> //PRINT EMAIL
    </td>
    <td align="center" width="205">
      <?=$row['totalpersons'];?>
    </td>

    <td align="center" width="188">
      <?//$row['totalpersons'];
          $poll=0;
          $tpersons=$row['totalpersons'];  //TRANFER THE VALUE TO TOTALPERSONS TO THE tpersons VARIABLE
          for($j=1;$j<=$tpersons;$j+=$mgmvalue)  //LOOP FOR VALUE OF $j=1 THE VALUE WHEN $j LESS THAN OR EQUAL TO THE VALUE      
                                                 OF $tpersons VARIABLE
          {
          $poll=$poll+1;      //INCREMENTS THE VALUE OF $poll VARIABLE BY 1
          $arr[$counter++]=$row['referer'];  //ASSIGNS THE VALUE OF referer FIELD OF THE CURRENT ROW(THE ONE WE FETCH FROM FIRST SQL STATEMENT) TO THE $arr ARRAY AND ASSIGNS SPECIFIC NUMBER TO THIS ARRAY
          }
          print($poll);  //PRINTS $poll VARIABLE'S VALUE
          ?>
    </td>
  </tr>
  <? }


     //print("randon no is: ".rand(0,$counter-1)." ");   //RANDOMLY GENERATES A NUMBER BEGINNING FROM '0' (THE FIRST DIGIT
                              //YOU USE IN THE rand() FUNCTION SPECIFIES THE START OF RANGE FROM WHICH A RANDOM NUMBER IS //TO BE CHOOSEN. AND THE UPPER LIMIT OF THE RANGE IS ONE LESS THAT THAN CURRENT VALUE
                              //OF $counter VARIABLE. FOR EX. rand(0,5) WILL GENERATE A RANDOM NUMBER FROM 0 TO 5
                             
     //for($jj=0;$jj<$counter;$jj++)  //FOR EACH VALUE OF $jj VARIABLE BEGINING FROM 0 AND UPTILL THE CURRENT VALUE  
                                      // OF $counter VARIABLE,
//     {
//      print($jj." ".$arr[$jj]."<BR>");  //PRINT $jj VARIABLE'S VALUE AND THE $arr ARRAY VALUE WHERE THE $arr ARRAY'S
                                          //NUMBER IS $jj...CONFUSED ? :) LEMME TELL U MORE CLEARLY. CAN YOU SEE THE COMMENT ABOVE WHERE WE SAID;
                                         
                                          "...//ASSIGNS THE VALUE OF referer FIELD OF THE CURRENT ROW(THE ONE WE FETCH FROM FIRST SQL STATEMENT) TO THE $arr ARRAY AND ASSIGNS SPECIFIC NUMBER TO THIS ARRAY"....
                                         
                                          THE PLACE WHERE CODE SAYS..$arr[$counter++]=$row['referer'];...
                                         
                                          REMEMBER I TOLD THAT WHEN $row['referer'] VARIABLE'S VALUE IS TRANSFERED TO  $array[$counter++] ARRAY, THIS ARRAY IG GIVEN A NUMBER FOR IDENTIFICATION PURPOSES....IN CURRENT LINE OF THE CODE, IF THIS ARRAY NUMBER IS EQUAL TO $jj OF $arr[$jj]....THE VALUE OF $jj AND THE CORROSPONING ARRAY $arr HAVE THE NUMBER [$jj].........NOTE THIS NUMBER IS NOT THE VALUE OF THE ARRAY,BUT AN IDENTITY THAT IS ASSIGNED TO THAT ARRAY...FOR EX. $arr[0],$arr[1],$arr[2],$arr[3],...THE $jj CORROSPONDS TO THE NUMBERS, 0,1,2,3...Etx....
                                       
                                         
                                         
                                         
//     }
 
     
     ?>
  <tr>
    <td colspan="5" align="center">
      <form name="formwinner" method="post" action="membergetmember.php">
        <input type="hidden" name="txtwinner" value="<?print($arr[rand(0,$counter-1)]);?>">  
           
           //FIELD txtwinner WILL BE PASSED TO THIS PAGE, membergetmember.php, THE ONE WHICH WE HAVE USED TO PROCESS  //THE CODE ABOVE...:)...THE VALUE THAT THIS txtwinner FIELD WILL HAVE IS GENERATED FROM THAT ARRAY $arr WITH THE IDENTITY (AS I DESCRIBED ABOVE) GENERATED RANDOMLY, WHERE THE RANDOM RANGE BEGINS FROM 0 TO '1 LESS THAN THE VALUE OF $counter'.....
       
       
        <input type="submit" name="Submit2" value="Select Winner !"> //KICKS OFF THE START.... :))
      </form>
    </td>
  </tr>
</table>
0
 
LVL 10

Expert Comment

by:Kshitij Ahuja
ID: 16823199
MY 2nd point is useless, pls ignore it as i have already commented...:)
0
 

Author Comment

by:Adamylo
ID: 16823321
The code does work, last time i checked it did. I will find some way to show you. :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month17 days, 12 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question