ERuiz Posted October 27, 2007 Share Posted October 27, 2007 Hello guys and gals, Look at the following code: if ($carrier == "Chautauqua Airlines") { $reg_number = rand (100, 999); $reg_number = "N".$reg_number."SK"; } As you can see, if $carrier equals Chautauqua Airlines, then it will make a random number from 100-999 and build the following result: N###SK What I want to know is how can I also make this code so that instead of always using SK, it would randomly select between using SK or JQ or RP? Thanks for any help, as always. Regards, ERuiz Quote Link to comment https://forums.phpfreaks.com/topic/75012-solved-random-letter/ Share on other sites More sharing options...
unidox Posted October 27, 2007 Share Posted October 27, 2007 Well it depends what you want, do you want a random string with alphanum, with a certain ammt of digits, or do you just want to choose from SK,JQ,RP Quote Link to comment https://forums.phpfreaks.com/topic/75012-solved-random-letter/#findComment-379298 Share on other sites More sharing options...
darkfreaks Posted October 27, 2007 Share Posted October 27, 2007 <?php $random_letter_ucase = chr(rand(ord("A"), ord("Z"))); if ($carrier == "Chautauqua Airlines") { $reg_number = rand (100, 999); $reg_number = ($random_letter_ucase+1),$reg_number,($random_letter_ucase+1); ///should output something like FG###SD }?> Quote Link to comment https://forums.phpfreaks.com/topic/75012-solved-random-letter/#findComment-379299 Share on other sites More sharing options...
ERuiz Posted October 27, 2007 Author Share Posted October 27, 2007 do you just want to choose from SK,JQ,RP Exactly, I simply want it to randomly choose between these 3 options: SK, JQ, RP Quote Link to comment https://forums.phpfreaks.com/topic/75012-solved-random-letter/#findComment-379302 Share on other sites More sharing options...
darkfreaks Posted October 27, 2007 Share Posted October 27, 2007 how about <?php $array= array("SK","JQ","RP"); $randarray= shuffle($array); echo $randarray; echo $regnumber; echo $randarray; ?> Quote Link to comment https://forums.phpfreaks.com/topic/75012-solved-random-letter/#findComment-379307 Share on other sites More sharing options...
ERuiz Posted October 27, 2007 Author Share Posted October 27, 2007 how about <?php $array= array("SK","JQ","RP"); $randarray= shuffle($array); echo $randarray; sprintf($regnumber); echo $randarray; ?> The result of $randarray is coming back as 1 Quote Link to comment https://forums.phpfreaks.com/topic/75012-solved-random-letter/#findComment-379310 Share on other sites More sharing options...
darkfreaks Posted October 27, 2007 Share Posted October 27, 2007 link me? it should be outputting something like <?php /// SK###JQ ?> <?php $array= array("SK","JQ","RP"); $randarray= shuffle($array); echo $randarray; echo $regnumber; echo $randarray; ?> Quote Link to comment https://forums.phpfreaks.com/topic/75012-solved-random-letter/#findComment-379311 Share on other sites More sharing options...
MadTechie Posted October 27, 2007 Share Posted October 27, 2007 <?php $array= array("SK","JQ","RP"); $randarray= array_rand($array); $randarray2= array_rand($array); echo $array[$randarray]; sprintf($regnumber); echo $array[$randarray2]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/75012-solved-random-letter/#findComment-379312 Share on other sites More sharing options...
darkfreaks Posted October 27, 2007 Share Posted October 27, 2007 oh my bad thanks tech Quote Link to comment https://forums.phpfreaks.com/topic/75012-solved-random-letter/#findComment-379313 Share on other sites More sharing options...
MadTechie Posted October 27, 2007 Share Posted October 27, 2007 darkfreaks remember shuffle will shuffle an array BUT whats does it return!!! shuffle($array); not $randarray=shuffle($array); as it returns a boolean EDIT: oh if you must have different random items then shuffle works well ie <?php $array= array("SK","JQ","RP"); shuffle($array); echo $array[0]; echo $regnumber; echo $array[1]; ?> which i think darkfreaks was aiming for Quote Link to comment https://forums.phpfreaks.com/topic/75012-solved-random-letter/#findComment-379315 Share on other sites More sharing options...
darkfreaks Posted October 27, 2007 Share Posted October 27, 2007 LOL i just scrolled down on the function on php.net i should read more thouroughly Quote Link to comment https://forums.phpfreaks.com/topic/75012-solved-random-letter/#findComment-379316 Share on other sites More sharing options...
ERuiz Posted October 27, 2007 Author Share Posted October 27, 2007 <?php $array= array("SK","JQ","RP"); $randarray= array_rand($array); $randarray2= array_rand($array); echo $array[$randarray]; sprintf($regnumber); echo $array[$randarray2]; ?> Thanks! That did the trick! Also MANY thanks to darkfreaks, who also helped out. Thanks to both you guys. ERuiz Quote Link to comment https://forums.phpfreaks.com/topic/75012-solved-random-letter/#findComment-379319 Share on other sites More sharing options...
darkfreaks Posted October 27, 2007 Share Posted October 27, 2007 your welcome sorry i couldnt be of more help ruiz but im just learning how to randomize arrays and didnt know what function to quite use as there is many functions in PHP that will randomize an array also hit topic solved Quote Link to comment https://forums.phpfreaks.com/topic/75012-solved-random-letter/#findComment-379321 Share on other sites More sharing options...
MadTechie Posted October 27, 2007 Share Posted October 27, 2007 in darkfreaks defence i have seen LOTS of people attempt shuffle like that.. also please be aware.. that the code you use will allow results like SK###SK and JQ###JQ where as the following code will not.. EDIT: oh if you must have different random items then shuffle works well ie <?php $array= array("SK","JQ","RP"); shuffle($array); echo $array[0]; echo $regnumber; echo $array[1]; ?> which i think darkfreaks was aiming for Quote Link to comment https://forums.phpfreaks.com/topic/75012-solved-random-letter/#findComment-379324 Share on other sites More sharing options...
darkfreaks Posted October 27, 2007 Share Posted October 27, 2007 oh sorry i forgot to number array i guess its like using any variable to row *slaps head* that and i shouldnt put a variable before shuffle haha Quote Link to comment https://forums.phpfreaks.com/topic/75012-solved-random-letter/#findComment-379325 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.