JonnyPHP Posted December 8, 2013 Share Posted December 8, 2013 So this is my 2nd PHP class ever. And this right here is part of my final. I'm sure you all know what the "shuffle" function does. What I thought I could make it do was randomize a bunch of echoes I have. Is that possible? Any help would be greatly appreciated. KCCO!Here's the code I need tweaked: <html><head> <title>Characters</title> <link rel="stylesheet" href="assets/css/bootstrap.css" media="screen"> <link rel="stylesheet" href="assets/css/bootstrap-responsive.css" media="screen"> <link rel="stylesheet" href="assets/css/normalize.css" type="text/css" media="screen"> <link rel="stylesheet" href="assets/css/style.css" media="screen"></head><body> <div class="navbar"> <div class="navbar-inner"> <a class="brand" href="#"></a> <ul class="nav"> <li class="active"><a href="Characters.php">Previous Clients</a></li> <li><a href="workForSupers.php">Work for Supers</a></li> </ul> </div> </div><!-- END of navbar--> <div align="center"> <h1>Supers we've helped locate</h1> <? date_default_timezone_set("America/Los_Angeles"); # set default timezone $imageDir = "images/"; # Comics is a PARENT class - Emily, Jenny, and so forth all are CHILDREN of Comics, meaning: They inherit the properties and methods included inside Comics. # For example: echo $joker->comiccharacters; will print out the $comiccharacters stored in Comics class Comics { const IMAGEDIR = "images/"; public $comiccharacters = "Comics"; } class Spiderman extends Comics { public $name = "Spiderman"; public $location = "New York City"; public $superpower = "his spider-sense, augmented speed and strength and his web shooters."; public $sex = "male"; public $published = "1963-03-01."; public $hero = true; public $asseskicked = 37; public $personscaught = [12,16,19,29,51]; public $powerlevel= '++++++'; public $email = "SpiderMan4Reals@gmail.com"; # A Constructor is automatically called when PHP is loaded public function __construct() { # set path for imageDir $this->imagePath = parent::IMAGEDIR . "spiderman.jpg"; } } $spiderman = new Spiderman(); class Magneto extends Comics { public $name = "Magneto"; public $location = "New York City."; public $superpower = "power of magnetism."; public $sex = "male"; public $published = "1963-10-15."; public $hero = false; public $asseskicked = 52; public $personscaught = [21,11,21,43,31]; public $powerlevel= '+++++++'; public $email = "ElMagneto@yahoo.com"; # A Constructor is automatically called when PHP is loaded public function __construct() { # set path for imageDir $this->imagePath = parent::IMAGEDIR . "magneto.jpg"; } } $magneto = new Magneto(); class Nightwing extends Comics { public $name = "Nightwing"; public $location = "Gotham City"; public $superpower = "is his peak physical conditioning, gadgets, and detective skills."; public $sex = "male"; public $published = "1985-01-06"; public $hero = true; public $asseskicked = 76; public $personscaught = [32,41,53,48,63]; public $powerlevel= '++++++'; public $email = "NightwingIsAwesome@hotmail.com"; # A Constructor is automatically called when PHP is loaded public function __construct() { # set path for imageDir $this->imagePath = parent::IMAGEDIR . "nightwing.jpg"; } } $nightwing = new Nightwing(); class GreenArrow extends Comics { public $name = "Green Arrow"; public $location = "Starling City"; public $superpower = "is his peak physical conditioning and accuracy with his gadgets."; public $sex = "male"; public $published = "1941-11-19"; public $hero = true; public $asseskicked = 62; public $personscaught = [17,17,29,31,49]; public $powerlevel= '++++++'; public $email = "ImTheGreenArrow@gmail.com"; # A Constructor is automatically called when PHP is loaded public function __construct() { # set path for imageDir $this->imagePath = parent::IMAGEDIR . "greenarrow.jpg"; } } $greenArrow = new GreenArrow(); class TheJoker extends Comics { public $name = "The Joker"; public $location = "Gotham City"; public $superpower = "being completely insane and psychotic."; public $sex = "male"; public $published = "1940-04-11"; public $hero = false; public $asseskicked = 112; public $personscaught = [61,79,103,94,52]; public $powerlevel= "++++++++"; public $email = "KillingForAJoke@gmail.com"; # A Constructor is automatically called when PHP is loaded public function __construct() { # set path for imageDir $this->imagePath = parent::IMAGEDIR . "joker.jpg"; } } $thejoker = new TheJoker(); # Create an array containing all the members classes $members = array($spiderman, $magneto, $nightwing, $greenArrow, $thejoker); # Calculate Ages - this cycles through all of the classes, and calculates the age for each band member, and places it in a property named "$age" foreach($members as $obj) { $obj->age = calculateAge($obj->published); } # Display our Jump Menu echo "<form name=\"jump\"> <p align=\"center\"> <select name=\"menu\" onchange='window.location.href=this.options[this.selectedIndex].value'> <option selected>All Characters</option> <option value=\"?sort=name\">Sort by Name</option> <option value=\"?sort=ageLoToHi\">Sort by Age (youngest to oldest)</option> <option value=\"?sort=ageHiToLo\">Sort by Age (oldest to youngest)</option> </select></p> </form>"; # Access the sort value from the url: blah.com/?sort=blam $sort = ''; if (isset($_GET['sort'])) { $sort = $_GET['sort']; } # Let's introduce all our members echo "<h2>Heroes and Villains Alike</h2>"; for ($i=0; $i < count($members); $i++) { switch ( $sort ) { # sort=name -- sorts alphabetically by 'name' in the member array case "name" : usort($members, 'sortByName_ascend'); displayProfile($members[$i]); break; case "nameRev" : usort($members, 'sortByName_descend'); displayProfile($members[$i]); break; # sort=ageLoToHi -- sorts alphabetically by 'age' ascending case "ageLoToHi" : usort($members, 'sortClassesByAgeLoHi'); displayProfile($members[$i]); break; # sort=ageHiToLo -- sorts alphabetically by 'age' descending case "ageHiToLo" : usort($members, 'sortClassesByAge_HiLo'); displayProfile($members[$i]); break; # sort=ageHiToLo -- sorts alphabetically by 'age' descending case "asseskicked" : uksort($members, 'sortAsseskicked'); displayProfile($members[$i]); break; # default -- spit out all band members according to their order in the array default : displayProfile($members[$i]); break; } } # This is a custom sorting function that works with usort(). # If you provide usort() with either an array or a set of objects, # it goes through all the items and compares them, and puts them in order # according to the rules below. function sortClassesByAgeLoHi($a, $b) { if ( $a->published == $b->published ) { #return 0; # this makes equivalent aged folks disappear return 1; } elseif ( $a->published > $b->published ) { return -1; } else { return 1; } } function sortClassesByAge_HiLo($a, $b) { return sortClassesByAgeLoHi($b, $a); # just reverse the arguments } function sortByName_ascend($a, $b) { if ( $a->name == $b->name ) { #return 0; # this makes equivalent aged folks disappear return 1; } elseif ( $a->name > $b->name ) { return 1; } else { return -1; } } function sortByName_descend($a, $b) { return sortByName_ascend($b, $a); # just reverse the arguments } # FUNCTIONS - Store all functions down here - I could also store them in another file function displayProfile($memberClass) { echo "<div class='memberProfile'><p>"; # spit out the image of our character # <img src="images/spiderman.jpg" /><br /> echo "<img src=\"".$memberClass->imagePath."\" width=200px /><br />"; #should make it so that when you refresh the page, it randomizes the order of the elements in the array shuffle($memberProfile);THE FOLLOWING IS SPECIFICALLY WHAT I WANT TO RANDOMIZE # spit out our caption echo "This is ".$memberClass->name.".<br>"; //counts the "+" for the power level echo "He has a power level of ".strlen($memberClass->powerlevel)." <br> "; echo "He resides in ".$memberClass->location.". <br>"; echo "He was first published in ".displayBirthdate($memberClass->published).".<br>"; echo "Making him ".calculateAge($memberClass->published)." years old! <br>"; //wraps the string after the specified # of characters echo "His superpower is ".wordwrap($memberClass->superpower,35,"<br>\n")."<br>"; echo "And in that time he's kicked the asses of ".$memberClass->asseskicked." people! <br>"; //finds the average & wraps the string after the specified # of characters echo "They normally capture their respective enemy ".average($memberClass->personscaught)." times a year.<br>"; echo "You can reach them at <a href=\"#\"> ".$memberClass->email.".</a>. <br> </p></div> "; } function displayBirthdate($bandmateBirthdate) { # January 1, 1984 $birthDateFormatted = date('F j, Y', strtotime($bandmateBirthdate)); return $birthDateFormatted; } function calculateAge($bandmateBirthdate) { #$currentTime = time(); # as a Unix timestamp list($year, $month, $day) = explode("-",$bandmateBirthdate); $elapsedYears = date('Y') - $year; $age = $elapsedYears; # calculates number of years elapsed # If current date < birthdate (i.e. Oct01 < Dec01), then subtract 1 year if ( date('n') < $month || ( date('n') == $month && date('j') < $day ) ) { $age = $age - 1; } return $age; } //example how to get the average in an array function average($average) { if (!is_array($average)) return false; return array_sum($average)/count($average); } ?></body> <script type="text/javascript" src="js/bootstrap.js"></script></html> Characters.php Quote Link to comment https://forums.phpfreaks.com/topic/284627-help-with-array-function-shuffle/ Share on other sites More sharing options...
Solution Ch0cu3r Posted December 8, 2013 Solution Share Posted December 8, 2013 (edited) Change your echo's to store the strings into an array, Use shuffle to randomize their order, then use implode to echo the new order Code for displayProfile function function displayProfile($memberClass) { echo "<div class='memberProfile'><p>"; # spit out the image of our character # <img src="images/spiderman.jpg" /><br /> echo "<img src=\"".$memberClass->imagePath."\" width=200px /><br />"; # spit out our caption $lines = array(); $lines[] = "This is ".$memberClass->name; //counts the "+" for the power level $lines[] = "He has a power level of ".strlen($memberClass->powerlevel); $lines[] = "He resides in ".$memberClass->location; $lines[] = "He was first published in ".displayBirthdate($memberClass->published); $lines[] = "Making him ".calculateAge($memberClass->published)." years old!"; //wraps the string after the specified # of characters $lines[] = "His superpower is ".wordwrap($memberClass->superpower,35); $lines[] = "And in that time he's kicked the asses of ".$memberClass->asseskicked." people!"; //finds the average & wraps the string after the specified # of characters $lines[] = "They normally capture their respective enemy ".average($memberClass->personscaught)." times a year."; $lines[] = "You can reach them at <a href=\"#\"> ".$memberClass->email."</a>"; shuffle($lines); // randomize the order of the lines above echo implode("<br />\n", $lines) . "</p></div>"; // output the randomized order } Edited December 8, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/284627-help-with-array-function-shuffle/#findComment-1461683 Share on other sites More sharing options...
JonnyPHP Posted December 8, 2013 Author Share Posted December 8, 2013 Oh my f****'in god! IT WORKS! IT REALLY WORKS!! Thank you!! Quote Link to comment https://forums.phpfreaks.com/topic/284627-help-with-array-function-shuffle/#findComment-1461688 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.