simmsy Posted December 4, 2011 Share Posted December 4, 2011 Hi im struggling to find out how to get an if statement working with my random quote creator, here is the coding: <?php include 'connect.php'; session_start(); $username = $_SESSION['loginusername']; $result = mysql_query("SELECT * FROM boxerinfo WHERE fighterrequest = '$username'"); while($row = mysql_fetch_array($result)) { $quotes[] = 'Go ahead, bite the big apple, don\'t mind the maggots, uh huh.'; $quotes[] = 'Happiness is a warm gun. (bang-bang, shoot-shoot.)'; $quotes[] = 'You may be a lover but you ain\'t no dancer.'; $quotes[] = 'Yeah you got lucky, babe. When I found you.'; $quotes[] = 'Out here in the fields, I fight for my meals.'; $quotes[] = 'You go back, jack, do it again. Wheel turnin\' round and round.'; $quotes[] = 'Don\'t let the sound of your own wheels drive you crazy.'; $quotes[] = 'Lord I\'m learning so much more, than back when I knew it all.'; $quotes[] = 'You get too much you get too high. Not enough and you\'re gonna die.'; $quotes[] = 'You were wrong when you said, \'everything was gonna be alright.\''; $quotes[] = 'I know just what you need. I might just have the thing.'; $quotes[] = 'I am hot, and when I\'m not, I\'m cold as ice.'; $random_number = rand(0,count($quotes)-1); echo $quotes[$random_number]; } ?> I just want if $quotes = (a certain quote) then insert into mysql table, anyone know how to do this on an array? thanks Quote Link to comment https://forums.phpfreaks.com/topic/252445-random-quotes-if-statement/ Share on other sites More sharing options...
QuickOldCar Posted December 4, 2011 Share Posted December 4, 2011 $quotes = array("Go ahead, bite the big apple, don't mind the maggots, uh huh.","Happiness is a warm gun. (bang-bang, shoot-shoot.)","You may be a lover but you ain't no dancer.","Yeah you got lucky, babe. When I found you.","Out here in the fields, I fight for my meals."); shuffle($quotes); $rand_quote = array_rand($quotes,2); echo $quotes[$rand_quote[0]] . "<br />"; Quote Link to comment https://forums.phpfreaks.com/topic/252445-random-quotes-if-statement/#findComment-1294302 Share on other sites More sharing options...
QuickOldCar Posted December 4, 2011 Share Posted December 4, 2011 or do you want to match a quote, and if matches then insert? What would determine your "a certain quote"? Match a word within a quote or match the exact quote. Quote Link to comment https://forums.phpfreaks.com/topic/252445-random-quotes-if-statement/#findComment-1294303 Share on other sites More sharing options...
simmsy Posted December 4, 2011 Author Share Posted December 4, 2011 Yea I wanted if $quotes = "Go ahead, bite the big apple, don't mind the maggots, uh huh." then INSERT INTO ....... Just wanted to select a specific quote from the array Quote Link to comment https://forums.phpfreaks.com/topic/252445-random-quotes-if-statement/#findComment-1294304 Share on other sites More sharing options...
QuickOldCar Posted December 4, 2011 Share Posted December 4, 2011 Did you just add those quotes in there in the mysql while loop to show the type data you get try something like this $match_quote = "Go ahead, bite the big apple, don't mind the maggots, uh huh."; while($row = mysql_fetch_array($result)) { if(preg_match("/$match_quote/i", $row['quotes'])){ //mysql_query("INSERT INTO some_table (username,quote) VALUES ($row['username'], $row['quotes'])"); echo "it matched"; } } Is not your exact values, but giving you the idea I don't see the full purpose for doing this though. Quote Link to comment https://forums.phpfreaks.com/topic/252445-random-quotes-if-statement/#findComment-1294312 Share on other sites More sharing options...
QuickOldCar Posted December 4, 2011 Share Posted December 4, 2011 I guess can also just check if they are equal to one another as well if($match_quote == $row['quotes']){ //mysql_query("INSERT INTO some_table (username,quote) VALUES ($row['username'], $row['quotes'])"); echo "it matched"; } Quote Link to comment https://forums.phpfreaks.com/topic/252445-random-quotes-if-statement/#findComment-1294321 Share on other sites More sharing options...
simmsy Posted December 4, 2011 Author Share Posted December 4, 2011 sorry the coding is this <?php include 'connect.php'; session_start(); $username = $_SESSION['loginusername']; $result = mysql_query("SELECT * FROM boxerinfo WHERE fighterrequest = '$username'"); while($row = mysql_fetch_array($result)) { $quotes[] = ''.$username.' Go ahead, bite the big apple, don\'t mind the maggots, uh huh.'; $quotes[] = ''.$username.' Happiness is a warm gun. (bang-bang, shoot-shoot.)'; $quotes[] = ''.$username.'You may be a lover but you ain\'t no dancer.'; $quotes[] = ''.$username.'Yeah you got lucky, babe. When I found you.'; $quotes[] = ''.$row['username'].'Out here in the fields, I fight for my meals.'; $quotes[] = ''.$row['username'].'You go back, jack, do it again. Wheel turnin\' round and round.'; $quotes[] = ''.$row['username'].'Don\'t let the sound of your own wheels drive you crazy.'; $quotes[] = ''.$row['username'].'Lord I\'m learning so much more, than back when I knew it all.'; $quotes[] = ''.$row['username'].'You get too much you get too high. Not enough and you\'re gonna die.'; $quotes[] = ''.$username.'You were wrong when you said, \'everything was gonna be alright.\''; $quotes[] = ''.$username.'I know just what you need. I might just have the thing.'; $quotes[] = ''.$username.'I am hot, and when I\'m not, I\'m cold as ice.'; $random_number = rand(0,count($quotes)-1); echo $quotes[$random_number]; } ?> I have the screen refreshing every 10 seconds (via javascript) and I wanted to input the quotes into MYSQL table in the order they come out randomly on the page so needed an if ($qutoes = ''.$username.' Go ahead, bite the big apple, don\'t mind the maggots, uh huh.'){ INSERT INTO.................} thanks for all help Quote Link to comment https://forums.phpfreaks.com/topic/252445-random-quotes-if-statement/#findComment-1294336 Share on other sites More sharing options...
QuickOldCar Posted December 4, 2011 Share Posted December 4, 2011 I still don't see the point in what you are doing. You could simply just append their username to the front of any quote upon display, versus trying to save all these values into a database. Doing something like below would save a lot of resources, no selects and inserts needed session_start(); $username = $_SESSION['loginusername']; $quotes = array("Go ahead, bite the big apple, don't mind the maggots, uh huh.","Happiness is a warm gun. (bang-bang, shoot-shoot.)","You may be a lover but you ain't no dancer.","Yeah you got lucky, babe. When I found you.","Out here in the fields, I fight for my meals."); shuffle($quotes); $rand_array = array_rand($quotes,2); $random_quote = $username . " ". $quotes[$rand_array[0]]; echo $random_quote . "<br />"; and if you need to use someone else's user name then you can select one random user from the database and append that to it instead echo $row['username'] . " ". $quotes[$rand_quote[0]] . "<br />"; But lets do this as a function, I made a simple users array to emulate your while loop $users_array = array("Angry Mike","Flexy Phil","Barbaria","Hank the Shank","Cool Carlito"); function getQuote($username){ $quotes = array("Go ahead, bite the big apple, don't mind the maggots, uh huh.","Happiness is a warm gun. (bang-bang, shoot-shoot.)","You may be a lover but you ain't no dancer.","Yeah you got lucky, babe. When I found you.","Out here in the fields, I fight for my meals."); shuffle($quotes); $rand_array = array_rand($quotes,2); $random_quote = $username . " ". $quotes[$rand_array[0]]; //mysql_query("INSERT INTO some_table (username,quote) VALUES ($username, $random_quote)"); return $random_quote; } foreach($users_array as $user){ echo getQuote($user) . "<br />"; } just one of the many results when refreshed it: I didn't add as many quotes in the array Angry Mike Out here in the fields, I fight for my meals. Flexy Phil You may be a lover but you ain't no dancer. Barbaria Yeah you got lucky, babe. When I found you. Hank the Shank Yeah you got lucky, babe. When I found you. Cool Carlito Happiness is a warm gun. (bang-bang, shoot-shoot.) Quote Link to comment https://forums.phpfreaks.com/topic/252445-random-quotes-if-statement/#findComment-1294366 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.