I using this function to generate a random number it work awesome however I will like know if there is a way to find out what was the previous number that function created. I will most grateful πŸ™‚ I could get some help whit thisπŸ˜ƒ

$min = 1;
$max = 238;
$disallowed = array(4,5,6,8,9,10,12,13,18,19,20,23);

function mt_rand_n($min="1",$max,$disallowed) {
if (!is_array($disallowed)) return false;
$numbers = array_values(array_diff(range($min,$max),$disallowed));
if (count($numbers) < 1) return false;
return $numbers[mt_rand(0,count($numbers) - 1)];
}

$id_rand = mt_rand_n($min, $max, $disallowed);

    ichimac wrote:

    I will like know if there is a way to find out what was the previous number that function created

    Store it?

      pretty good idea but I really don't want to do it like that because then the table could be pretty big lol....

        That depends on what you're trying to do.

          ichimac wrote:

          pretty good idea but I really don't want to do it like that because then the table could be pretty big lol....

          Then store the seed (i.e., you should seed the PRNG yourself) and regenerate the sequence until the desired point.

          Weedpacket wrote:

          That depends on what you're trying to do.

          I agree. What are you trying to do?

            because what I'm trying to do is this:
            the function will created a random number i put that random number and a mysql string to call a user profile and then gives you the option to post a comment, but the way it is at the moment that comment goes to next random number generated not the current one, for example if have user_id 8, but next number coming is 23 the comment goes to user 23 not 8.

            I hope it make sense

              Store the random number generated in a session, then retrieve it when you want to store the comment. Or, since you are presumably retrieving data based on this random number and then displaying the data to the user along with the comment form, you can also store the random number in a hidden form field.

                dude you are a genius I'll try that

                  i try using bouth of your solutions but I still have the same problem.. this is the all code for the page maybe I'm doing something wrong

                  <?php
                  require_once("includes/session.php");
                  include('includes/connection.inc.php');
                  include('includes/corefuncs.php');
                  $conn = dbConnect('query');

                  $min = 42;
                  $max = 118;
                  $disallowed = array(117);

                  function mt_rand_n($min="1",$max,$disallowed) {
                  if (!is_array($disallowed)) return false;
                  $numbers = array_values(array_diff(range($min,$max),$disallowed));
                  if (count($numbers) < 1) return false;
                  return $numbers[mt_rand(0,count($numbers) - 1)];
                  }

                  $id_rand = mt_rand_n($min, $max, $disallowed);
                  
                  $sql = "SELECT feed_id,user_nickname,gender,image_normal,user_age,user_location,user_status FROM feed WHERE feed_id='$id_rand' ";
                  $result = mysql_query($sql) or die(mysql_error());
                  $row = mysql_fetch_assoc($result);

                  ?>
                  <div id="dude">
                  <form id="rating" name="rating" method="post" >
                  <input type="radio" name="rating" value="1" onClick="this.form.submit()" />Good
                  <input type="radio" name="rating" value="2" onClick="this.form.submit()" />Really Good
                  <input type="radio" name="rating" value="3" onClick="this.form.submit()" />Awesome
                  </p>
                  </form>

                  <p><?php echo $row['user_nickname']; ?> / <?php echo $row['user_age'];?> / <?php echo $row['user_status'];?>/
                  <?php echo $row['user_location'];?></p>
                  <img src="<?php echo $row['image_normal']; ?>" />

                  <p><?php echo $row['user_feed']; ?></p></td>
                  <td width="83" align="left" valign="top" bgcolor="#FFFFFF">
                  feed_id : <?php echo $row['feed_id']; ?>

                  <form id="comment" name="comment" method="post">
                  <label for="article" class="style2">Que cuentas</label>
                  feed_id : <?php echo $row['feed_id']; ?>
                  <p><textarea name="article" cols="35" rows="4" class="style3" id="article"></textarea></p>
                  <p><input type="submit" name="comment" id="comment" value="comment" />
                  <input name="message_for" type="hidden" id="message_for" value="<?php echo $row['feed_id'];?>"/>
                  </p>
                  </form>
                  <?php
                  //**************************************************************************
                  if (array_key_exists('comment', $_POST)) {

                  // create database connection
                  $conn = dbConnect('admin');
                  // remove backslashes
                  nukeMagicQuotes();

                  // prepare an array of expected items
                  $expected = array('carry_user_id','feed_id','article', 'message_for');
                  // make $POST data safe for insertion into database
                  foreach ($
                  POST as $key => $value) {
                  if (in_array($key, $expected)) {
                  ${$key} = mysql_real_escape_string($value);
                  }
                  }

                  // initialize error array
                  $comment_message = array();
                  // check length of usecomment

                  if (empty($article)) {
                  $comment_message[] = 'Please enter your comment';
                  }

                  //if (!$comment_message) {
                  	if ($row['feed_id'] ) {
                  		echo $row['feed_id'];
                  
                  // prepare the SQL query
                  $sql_comment = "INSERT INTO journal (carry_user_id, article, created, message_for)
                       			VALUES('$user_id', '$article', NOW(),'{$row['feed_id']}')";
                  
                  // process the query
                  $comment_result = mysql_query($sql_comment) or die(mysql_error());
                   		}

                  }
                  //******************************************************************
                  ?>
                  </td></tr><tr bgcolor="#CCCCCC">
                  <th align="left" valign="top" bgcolor="#FFFFFF" >

                    </th>
                    <td align="left" valign="bottom" bgcolor="#FFFFFF">
                  
                    </td>
                  </tr>

                  </table>

                  </div>
                  <br class="clearfloat" />
                  <?php include('includes/footer.php'); ?>

                    sorry about the previous message the code is all one color i took some screenshots I think this is better

                      Please post your PHP code in [php][/php] bbcode tags.

                        so that's how you do it ...thank you very much

                        <?php
                        require_once("includes/session.php");
                        include('includes/connection.inc.php');
                        include('includes/corefuncs.php');
                        $conn = dbConnect('query');
                        
                        
                        $min = 42;
                        $max = 118;
                        $disallowed = array(117);
                        
                        function mt_rand_n($min="1",$max,$disallowed) {
                        if (!is_array($disallowed)) return false;
                        $numbers = array_values(array_diff(range($min,$max),$disallowed));
                        if (count($numbers) < 1) return false;
                        return $numbers[mt_rand(0,count($numbers) - 1)];
                        }
                        
                        $id_rand = mt_rand_n($min, $max, $disallowed);
                        
                        $sql = "SELECT feed_id,user_nickname,gender,image_normal,user_age,user_location,user_status FROM feed WHERE feed_id='$id_rand' ";
                        $result = mysql_query($sql) or die(mysql_error());
                        $row = mysql_fetch_assoc($result);
                        
                        
                        ?>
                        <div id="dude">
                        <form id="rating" name="rating" method="post" >
                        <input type="radio" name="rating" value="1" onClick="this.form.submit()" />Good
                        <input type="radio" name="rating" value="2" onClick="this.form.submit()" />Really Good
                        <input type="radio" name="rating" value="3" onClick="this.form.submit()" />Awesome
                        </p>
                        </form>
                        
                        <p><?php echo $row['user_nickname']; ?> / <?php echo $row['user_age'];?> / <?php echo $row['user_status'];?>/
                        <?php echo $row['user_location'];?></p>
                        <img src="<?php echo $row['image_normal']; ?>" />
                        <p><?php echo $row['user_feed']; ?></p></td>
                        <td width="83" align="left" valign="top" bgcolor="#FFFFFF">
                        feed_id : <?php echo $row['feed_id']; ?>
                        
                        <form id="comment" name="comment" method="post">
                        <label for="article" class="style2">Que cuentas</label>
                        feed_id : <?php echo $row['feed_id']; ?>
                        <p><textarea name="article" cols="35" rows="4" class="style3" id="article"></textarea></p>
                        <p><input type="submit" name="comment" id="comment" value="comment" />
                        <input name="message_for" type="hidden" id="message_for" value="<?php echo $row['feed_id'];?>"/>
                        </p>
                        </form>
                        <?php
                        //**************************************************************************
                        if (array_key_exists('comment', $_POST)) {
                        
                        // create database connection
                        $conn = dbConnect('admin');
                        // remove backslashes
                        nukeMagicQuotes();
                        
                        // prepare an array of expected items
                        $expected = array('carry_user_id','feed_id','article', 'message_for');
                        // make $_POST data safe for insertion into database
                        foreach ($_POST as $key => $value) {
                        if (in_array($key, $expected)) {
                        ${$key} = mysql_real_escape_string($value);
                        }
                        }
                        
                        // initialize error array
                        $comment_message = array();
                        // check length of usecomment
                        
                        if (empty($article)) {
                        $comment_message[] = 'Please enter your comment';
                        }
                        
                        //if (!$comment_message) {
                        if ($row['feed_id'] ) {
                        echo $row['feed_id'];
                        
                        // prepare the SQL query
                        $sql_comment = "INSERT INTO journal (carry_user_id, article, created, message_for)
                        VALUES('$user_id', '$article', NOW(),'{$row['feed_id']}')";
                        
                        // process the query
                        $comment_result = mysql_query($sql_comment) or die(mysql_error());
                        }
                        }
                        //******************************************************************
                        ?>
                        </td></tr><tr bgcolor="#CCCCCC">
                        <th align="left" valign="top" bgcolor="#FFFFFF" >
                        
                        </th>
                        <td align="left" valign="bottom" bgcolor="#FFFFFF">
                        
                        </td>
                        </tr>
                        </table>
                        
                        </div>
                        <br class="clearfloat" />
                        <?php include('includes/footer.php'); ?>
                        
                          5 days later

                          i try to store that value on a database but for some reason it keeps storing the new value not the old one

                            Write a Reply...