Jump to content

Random Statements


Tenaciousmug

Recommended Posts

Alright, I have a random thing going on. Works perfectly fine. This is the code:

 

<?php
include("config.php");

//To change the odds, change the second number in the rand() function.
$rand = floor(rand(0,1));
if($rand == 1)
{
$sql = "SELECT * FROM randomevents WHERE rarity <= '10'";
$result = mysqli_query($cxn, $sql);
while ($row = mysqli_fetch_assoc($result))
{
	$event[] = $row['phrase'];
	if ($row['type'] == 'gainsp')
	{
		$rand = rand(200,500);
		$sql = "UPDATE members SET starpoints = starpoints+$rand WHERE userid='".$_SESSION['userid']."'";
		mysqli_query($cxn, $sql) or die("Query died: updating starpoints");
	}
}

//This will pick a random event and show it
$renum = floor(rand(0,count($event)));

$eventdisplay = $event[$renum];
}
?>

 

In the database, I have the phrases set as:

"You have gained {$rand} starpoints!".

How do you make that variable echo out as the $rand I'm generating on this page?

It just keeps posting as is. When I went the {$rand} to display as the number I'm generating to set their starpoints to.

So I guess how do you hold a variable in the database?

 

Does anyone understand what I'm asking?

Here as an image to help understand:

 

databasevariables.png

Link to comment
Share on other sites

Try using a simple str_replace, you'll get something like this...

 

  if(strstr($eventdisplay,'{$rand}')) {
    echo str_replace('{$rand}',$rand,$eventdisplay);
  } else {
    echo 'No event to display.';
  }

 

Try the above code and place it below $eventdisplay, tell me how it goes :)

 

Regards, PaulRyan.

Link to comment
Share on other sites

Well I got it doing it another way.

Pretty long, but I don't feel like confuzzling my brain anymore. xD

I put:

if ($row['type'] == "gainsp")
	{
		$rand = rand(200,500);
		$displaya = str_replace("rand", "".$rand."", "".$display."");
		$sql = "UPDATE members SET starpoints = starpoints+$rand WHERE userid='".$_SESSION['userid']."'";
		mysqli_query($cxn, $sql) or die("Query died: updating starpoints");
		$eventdisplay = "<table cellspacing=\"0\" class=\"events\" align=\"center\">
		<tr>
		<td width=\"350px\"><center><b><h1>Random Event</h1></b></center></td>
		</tr>
		<tr>
		<td><img src=\"http://www.elvonica.com/".$image."\" style=\"position:relative;float:left;\">
		<p><center>".$displaya."</center></p>
		</td>
		</table><br>";
	}
	elseif ($row['type'] == "losesp")
	{
		$rand = rand(50,100);
		$displaya = str_replace("rand", "".$rand."", "".$display."");
		$sql = "UPDATE members SET starpoints = starpoints-$rand WHERE userid='".$_SESSION['userid']."'";
		mysqli_query($cxn, $sql) or die("Query died: updating starpoints");
		$eventdisplay = "<table cellspacing=\"0\" class=\"events\" align=\"center\">
		<tr>
		<td width=\"350px\"><center><b><h1>Random Event</h1></b></center></td>
		</tr>
		<tr>
		<td><img src=\"http://www.elvonica.com/".$image."\" style=\"position:relative;float:left;\">
		<p><center>".$displaya."</center></p>
		</td>
		</table><br>";
	}

 

Thanks though! (:

Link to comment
Share on other sites

Crap nevermind. Now I'm getting an error whenever its wanting to show the "youve gained starpoints" random event.

 

<?php
include("config.php");

//To change the odds, change the second number in the rand() function.
$rand = rand(1,3);
if($rand == 1)
{
$sql = "SELECT * FROM randomevents WHERE rarity <= '10'";
$result = mysqli_query($cxn, $sql);
while ($row = mysqli_fetch_assoc($result))
{
	$event[] = $row['phrase'];
}

//This will pick a random event and show it
$renum = rand(0,count($event)-1);
$display = $event[$renum];

if ($display == "")
{
	$eventdisplay = "";
}
else
{
	$sql = "SELECT * FROM randomevents WHERE phrase='".$display."'";
	$result = mysqli_query($cxn, $sql);
	$row = mysqli_fetch_array($result);

	$image = $row['image'];

	if ($row['type'] == "gainsp")
	{
		$rand = rand(200,500);
		$displaya = str_replace("rand", "".$rand."", "".$display."");
		$sql = "UPDATE members SET starpoints = starpoints+$rand WHERE userid='".$_SESSION['userid']."'";
		mysqli_query($cxn, $sql) or die("Query died: updating starpoints");
		$eventdisplay = "<table cellspacing=\"0\" class=\"events\" align=\"center\">
		<tr>
		<td width=\"350px\"><center><b><h1>Random Event</h1></b></center></td>
		</tr>
		<tr>
		<td><img src=\"http://www.elvonica.com/".$image."\" style=\"position:relative;float:left;\">
		<p><center>".$displaya."</center></p>
		</td>
		</table><br>";
	}
	elseif ($row['type'] == "losesp")
	{
		$rand = rand(50,100);
		$displaya = str_replace("rand", "".$rand."", "".$display."");
		$sql = "UPDATE members SET starpoints = starpoints-$rand WHERE userid='".$_SESSION['userid']."'";
		mysqli_query($cxn, $sql) or die("Query died: updating starpoints");
		$eventdisplay = "<table cellspacing=\"0\" class=\"events\" align=\"center\">
		<tr>
		<td width=\"350px\"><center><b><h1>Random Event</h1></b></center></td>
		</tr>
		<tr>
		<td><img src=\"http://www.elvonica.com/".$image."\" style=\"position:relative;float:left;\">
		<p><center>".$displaya."</center></p>
		</td>
		</table><br>";
	}
	else
	{}
}
}
else
{
$eventdisplay = "";
}
?>

 

Error message:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home1/elvonica/public_html/randomevents.php on line 27
Link to comment
Share on other sites

A moderator suggesting the use of EVAL?!?! With data coming from an outside source? BAD IDEA!

 

Echo your $sql variable after line 25. That should let you know what's going on with your query. My guess is you need to change it to

 

$sql = "SELECT * FROM randomevents WHERE phrase='".mysqli_real_escape_string($cxn,$display)."'";

Link to comment
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.