Tenaciousmug Posted May 7, 2011 Share Posted May 7, 2011 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: Quote Link to comment https://forums.phpfreaks.com/topic/235800-random-statements/ Share on other sites More sharing options...
spiderwell Posted May 8, 2011 Share Posted May 8, 2011 whats in the database is just a string with what 'looks' like a php variable, php wont recognise it, you would have to use str_replace() to put your actual $rand variable into that string. then it should echo out correctly Quote Link to comment https://forums.phpfreaks.com/topic/235800-random-statements/#findComment-1212102 Share on other sites More sharing options...
Tenaciousmug Posted May 8, 2011 Author Share Posted May 8, 2011 Oh wow. You have any clue how I would do that? Sorry, my brain is completely dead and I can't even think of where to start on that one. But thanks for the.. further solution. xP Quote Link to comment https://forums.phpfreaks.com/topic/235800-random-statements/#findComment-1212106 Share on other sites More sharing options...
PaulRyan Posted May 8, 2011 Share Posted May 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235800-random-statements/#findComment-1212109 Share on other sites More sharing options...
Tenaciousmug Posted May 8, 2011 Author Share Posted May 8, 2011 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! (: Quote Link to comment https://forums.phpfreaks.com/topic/235800-random-statements/#findComment-1212110 Share on other sites More sharing options...
Tenaciousmug Posted May 8, 2011 Author Share Posted May 8, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/235800-random-statements/#findComment-1212112 Share on other sites More sharing options...
Zane Posted May 8, 2011 Share Posted May 8, 2011 just use eval Quote Link to comment https://forums.phpfreaks.com/topic/235800-random-statements/#findComment-1212114 Share on other sites More sharing options...
Tenaciousmug Posted May 8, 2011 Author Share Posted May 8, 2011 I did that. It does the same thing. They're basically the same. Quote Link to comment https://forums.phpfreaks.com/topic/235800-random-statements/#findComment-1212118 Share on other sites More sharing options...
xyph Posted May 8, 2011 Share Posted May 8, 2011 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)."'"; Quote Link to comment https://forums.phpfreaks.com/topic/235800-random-statements/#findComment-1212134 Share on other sites More sharing options...
Tenaciousmug Posted May 8, 2011 Author Share Posted May 8, 2011 Wow, you're a genius. (: It's because the first phrase for receiving starpoints had an apostrophe. And the other one didn't. That's why that one was showing. Thank you so much! (: Quote Link to comment https://forums.phpfreaks.com/topic/235800-random-statements/#findComment-1212139 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.