Jump to content

Recommended Posts

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);

 

because what I'm trying to do is this:

the function will created a random number i add that random number in 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 I have user_id 8, but next number coming is 23 the comment goes to user 23 not 8.

 

I hope it make sense

 

Link to comment
https://forums.phpfreaks.com/topic/157104-mt_rand/
Share on other sites

for example $ramdon_id = mt_rand(1,200) ; will pull like 4,8,9,29,89,23, etc....after that  first number is generated (4) for example the next one up is 8 I will like to keep that previous generated number on a variable if possible $old_id = (what ever that number was) ....

 

I hope it make sense

Link to comment
https://forums.phpfreaks.com/topic/157104-mt_rand/#findComment-827659
Share on other sites

<?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'); ?>

Link to comment
https://forums.phpfreaks.com/topic/157104-mt_rand/#findComment-827676
Share on other sites

Preferably

...code...

as

...code...

doesn't colour code things.

<--- likes colours  ;) I think code tags are ok for a simple 1 liner example.. but going too much beyond that, I would go with php tags instead.

 

EDIT - When possible, try to post only the relevant segment of problematic code instead of the entire mountain of it.. can quickly turn people off seeing how this represents them needing to spend extra time trying to decipher things.

Link to comment
https://forums.phpfreaks.com/topic/157104-mt_rand/#findComment-827774
Share on other sites

thank you thank you.

 

what I'm trying to do is this:

the function will created a random number i add that random number into 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 I have user_id 8, but next number coming is 23 the comment goes to user 23 not 8.

 

users.PNG

 

carry_user_id is the user thats sending the message and message for is where the message is going but is not going to the correct user

Link to comment
https://forums.phpfreaks.com/topic/157104-mt_rand/#findComment-827784
Share on other sites

thank you thank you

 

the function will created a random number i add that random number in 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 I have user_id 8, but next number coming is 23 the comment goes to user 23 not 8.

users.PNG

 

carry_user_id is the person thats sending the message

article is the message

and message for is the user thats receiving it

Link to comment
https://forums.phpfreaks.com/topic/157104-mt_rand/#findComment-827786
Share on other sites

those other lines are just html and css but i'll post

it

<?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);


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>fresaOno | <?php echo $row['user_nickname']; ?>'s home page</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<!--[if IE 5]>
<style type="text/css">
/* place css box model fixes for IE 5* in this conditional comment */
.thrColFixHdr #sidebar1 { width: 180px; }
.thrColFixHdr #sidebar2 { width: 190px; }
</style>
<![endif]--><!--[if IE]>
<style type="text/css">
/* place css fixes for all versions of IE in this conditional comment */
.thrColFixHdr #sidebar2, .thrColFixHdr #sidebar1 { padding-top: 30px; }
.thrColFixHdr #mainContent { zoom: 1; }
/* the above proprietary zoom property gives IE the hasLayout it needs to avoid several bugs */
</style>
<![endif]-->
<style type="text/css">
<!--
.style2 {
/*
color: #FF00FF;*/
color: #DC143C;
font-weight:bold;
font-family: Geneva, Arial, Helvetica, sans-serif;
font-size: 14px;
background-color:transparent;
}
.style3 {
color:	#4169E1;
font-family: Geneva, Arial, Helvetica, sans-serif;
font-weight:bold;
font-size:12px;
background-color:#ECF1EF;
}

div #dude {
color:pink;
font-family: Geneva, Arial, Helvetica, sans-serif;
font-weight:bold;
font-size:16px;
background-image:url(images/me4.png);
background-repeat:no-repeat;
width:480px;
}
.style6 {
font-family: Geneva, Arial, Helvetica, sans-serif;
font-weight: bold; }

div #score {
font-family: Geneva, Arial, Helvetica, sans-serif;
font-weight:bold;
font-size:30px;
color:#FF3300;
}
div #links, #links1, #links2{
font-family: Geneva, Arial, Helvetica, sans-serif;
font-size:14px;
font-weight:bold;
background-image:url(images/banner12.png);
background-repeat:no-repeat;
padding-bottom:1%;
/* border-bottom:dotted;*/
}


table, td, tr,th {
background-color:transparent;
}

a{
text-decoration: none;
}

a:link {
color: #0063DC;
}

a:visited {
   color: #0063DC;
}
a:active {
color:red;
}
a:hover {
background-color:#0063DC;
color:white ;
}
.style1 {color: #FF0084}

-->
</style>
</head>

<body>
<body class="thrColFixHdr">

<div id="container">
<div id="header">
  <!-- end #header -->
</div>
<div id="sidebar1">
  
  <form id="logoutForm" name="logoutForm" method="post" action="">   
      <input name="logout" type="submit" id="logout" value="Log out" />
  </form>
  
</div><!-- end #sidebar1 -->
<div id="sidebar2">
</div>
<div id="mainContent">
  <!-- Start change by Edgar November 3 2008-->
<?php 

  // echo "my current session ID: $current_session";   
  // run this script only if the logout button has been clicked
  if (array_key_exists('logout', $_POST)) {
// empty the $_SESSION array
$_SESSION = array();
// invalidate the session cookie
if (isset($_COOKIE[session_name()])) {
  setcookie(session_name(), '', time()-86400, '/');
}

// end session and redirect
session_destroy();
header('Location: http://localhost/');
exit;
}
?>
     
  <!--End change by Edgar October 13 2008-->
    <!-- end #mainContent -->


<div id="dude">
<form id="rating" name="rating" method="post" >
<!--<input type="hidden" name="image_id" value="$image_id" />-->
<center>freseando </center>
  
  <p>
  
  <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>
  </div> <!--controls border at the end of the page --!>
<!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats -->
<div>
  <table width="412" >
    <tr bgcolor="#CCCCCC">
      <td width="317" align="left" valign="top" bgcolor="#FFFFFF" >
      <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'); ?>

Link to comment
https://forums.phpfreaks.com/topic/157104-mt_rand/#findComment-827817
Share on other sites

Let me know what the var_dumps print out.

<?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);
    var_dump($id_rand);
    $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);


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>fresaOno | <?php echo $row['user_nickname']; ?>'s home page</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<!--[if IE 5]>
<style type="text/css">
/* place css box model fixes for IE 5* in this conditional comment */
.thrColFixHdr #sidebar1 { width: 180px; }
.thrColFixHdr #sidebar2 { width: 190px; }
</style>
<![endif]--><!--[if IE]>
<style type="text/css">
/* place css fixes for all versions of IE in this conditional comment */
.thrColFixHdr #sidebar2, .thrColFixHdr #sidebar1 { padding-top: 30px; }
.thrColFixHdr #mainContent { zoom: 1; }
/* the above proprietary zoom property gives IE the hasLayout it needs to avoid several bugs */
</style>
<![endif]-->
<style type="text/css">
<!--
.style2 {
/*
color: #FF00FF;*/
color: #DC143C;
font-weight:bold;
font-family: Geneva, Arial, Helvetica, sans-serif;
font-size: 14px;
background-color:transparent;
}
.style3 {
color:	#4169E1;
font-family: Geneva, Arial, Helvetica, sans-serif;
font-weight:bold;
font-size:12px;
background-color:#ECF1EF;
}

div #dude {
color:pink;
font-family: Geneva, Arial, Helvetica, sans-serif;
font-weight:bold;
font-size:16px;
background-image:url(images/me4.png);
background-repeat:no-repeat;
width:480px;
}
.style6 {
font-family: Geneva, Arial, Helvetica, sans-serif;
font-weight: bold; }

div #score {
font-family: Geneva, Arial, Helvetica, sans-serif;
font-weight:bold;
font-size:30px;
color:#FF3300;
}
div #links, #links1, #links2{
font-family: Geneva, Arial, Helvetica, sans-serif;
font-size:14px;
font-weight:bold;
background-image:url(images/banner12.png);
background-repeat:no-repeat;
padding-bottom:1%;
/* border-bottom:dotted;*/
}


table, td, tr,th {
background-color:transparent;
}

a{
text-decoration: none;
}

a:link {
color: #0063DC;
}

a:visited {
   color: #0063DC;
}
a:active {
color:red;
}
a:hover {
background-color:#0063DC;
color:white ;
}
.style1 {color: #FF0084}

-->
</style>
</head>

<body>
<body class="thrColFixHdr">

<div id="container">
<div id="header">
  <!-- end #header -->
</div>
<div id="sidebar1">
  
  <form id="logoutForm" name="logoutForm" method="post" action="">   
      <input name="logout" type="submit" id="logout" value="Log out" />
  </form>
  
</div><!-- end #sidebar1 -->
<div id="sidebar2">
</div>
<div id="mainContent">
  <!-- Start change by Edgar November 3 2008-->
<?php 

  // echo "my current session ID: $current_session";   
  // run this script only if the logout button has been clicked
  if (array_key_exists('logout', $_POST)) {
// empty the $_SESSION array
$_SESSION = array();
// invalidate the session cookie
if (isset($_COOKIE[session_name()])) {
  setcookie(session_name(), '', time()-86400, '/');
}

// end session and redirect
session_destroy();
header('Location: http://localhost/');
exit;
}
?>
     
  <!--End change by Edgar October 13 2008-->
    <!-- end #mainContent -->


<div id="dude">
<form id="rating" name="rating" method="post" >
<!--<input type="hidden" name="image_id" value="$image_id" />-->
<center>freseando </center>
  
  <p>
  
  <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>
  </div> <!--controls border at the end of the page --!>
<!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats -->
<div>
  <table width="412" >
    <tr bgcolor="#CCCCCC">
      <td width="317" align="left" valign="top" bgcolor="#FFFFFF" >
      <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);
      }
    }
var_dump($article);
  // initialize error array
  $comment_message = array();
  // check length of usecomment

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

//if (!$comment_message) {
	if ($feed_id] ) {

  	// prepare the SQL query
  	$sql_comment = "INSERT INTO journal (carry_user_id, article, created, message_for)
         			VALUES('$user_id', '$article', NOW(),'$feed_id'')";
        var_dump($sql_comment);

  	// 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'); ?>

Link to comment
https://forums.phpfreaks.com/topic/157104-mt_rand/#findComment-827824
Share on other sites

Preferably

...code...

as

...code...

doesn't colour code things.

<--- likes colours  ;) I think code tags are ok for a simple 1 liner example.. but going too much beyond that, I would go with php tags instead.

 

 does, but only within PHP tags.

 

Ah, thanks for the heads up that. Didn't think about nesting those tags together. I always figured it was always either one or the other.

Link to comment
https://forums.phpfreaks.com/topic/157104-mt_rand/#findComment-827925
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.