timmah1 Posted December 19, 2008 Share Posted December 19, 2008 how can i make a custom order for a foreach statement? I need to have the order like this utl,sngl,mnth,ssn,po,spl but right now, it's showing the order in random I have this foreach( $packages as $key => $value) but i have no idea how to order what is pulled Any help would be greatly appreciated Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/137719-out-put-order/ Share on other sites More sharing options...
Mchl Posted December 19, 2008 Share Posted December 19, 2008 foreach takes array elements in order using numeric keys so: $packages[0] comes first, then $packages[1], $packages[2] etc... Quote Link to comment https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-719880 Share on other sites More sharing options...
timmah1 Posted December 19, 2008 Author Share Posted December 19, 2008 So right now, I loop the foreach statement, and display like this echo "<strong>".$key1."</strong><br />".nl2br($value)."<br /><br />"; So should I take the loop off? Quote Link to comment https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-719882 Share on other sites More sharing options...
redarrow Posted December 19, 2008 Share Posted December 19, 2008 Please show us the order off the current array, Then show us how u want the array ordered. There plenty off array functions to do this, but first we need to see a array to understand your request. i am currently learning advance arrays and functions with advance loops... the more you show us and demonstrate what u got the more we understand. me guessing ur current array like driving a car with square wells. Quote Link to comment https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-719884 Share on other sites More sharing options...
timmah1 Posted December 19, 2008 Author Share Posted December 19, 2008 Here is the code I'm using while($a = mysql_fetch_assoc($q)) { $packages; $packages["$a[package]"] = $a['picks']; } foreach( $packages as $key => $value){ if($key == "ult"){ if($value == ""){ $key1 = ""; } else { $key1 = "Ultimate Lock Pick Package"; } } if($key == "sngl"){ if($value == ""){ $key1 = ""; } else { $key1 = "Single Pick Package"; } } if($key == "mnth"){ if($value == ""){ $key1 = ""; } else { $key1 = "Month Package"; } } if($key == "ssn"){ if($value == ""){ $key1 = ""; } else { $key1 = "Season Package"; } } if($key == "po"){ if($value == ""){ $key1 = ""; } else { $key1 = "Playoffs Package"; } } if($key == "spl"){ if($value == ""){ $key1 = ""; } else { $key1 = "Special Package"; } } echo "<strong>".$key1."</strong><br />".nl2br($value)."<br /><br />"; } I need it to display in this order 1. ult 2. sngl 3. mnth 4. ssn 5. po 6. spl Quote Link to comment https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-719887 Share on other sites More sharing options...
premiso Posted December 19, 2008 Share Posted December 19, 2008 <?php $array = array("utl" => "test", "sngl" => "Test2", "mnth" => "test3"); sort($array); $dispArray = array("utl", "sngl", "mnth"); $cnt = count($dispArray); for ($i=0; $i<$cnt;$i++) { $newArray[$dispArray[$i]] = $array[$dispArray[$i]]; } ?> Not sure if that is what you are looking for, but yea for your own special order I would think you need something like that. Quote Link to comment https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-719890 Share on other sites More sharing options...
timmah1 Posted December 19, 2008 Author Share Posted December 19, 2008 So scrap what I have, and use this? What's the "test" for? Quote Link to comment https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-719892 Share on other sites More sharing options...
premiso Posted December 19, 2008 Share Posted December 19, 2008 So scrap what I have, and use this? What's the "test" for? I just needed an array so Icould test the functionality. You would want to put the columns how you want them displayed exactly in order in the $dispArray definition and replace $array with the data array you want ordered. EDIT: Decided to make it a function <?php $array = array("utl" => "test", "sngl" => "Test2", "mnth" => "test3"); $dispArray = array("utl", "sngl", "mnth"); print_r($orderMyArray($array, $dispArray)); function orderMyArray($array, $orderby) { sort($array); $cnt = count($orderby); for ($i=0; $i<$cnt;$i++) { $newArray[$orderby[$i]] = $array[$orderby[$i]]; } return $newArray; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-719893 Share on other sites More sharing options...
timmah1 Posted December 19, 2008 Author Share Posted December 19, 2008 I'm really confused on that code premiso Quote Link to comment https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-719894 Share on other sites More sharing options...
premiso Posted December 19, 2008 Share Posted December 19, 2008 I'm really confused on that code premiso Thats because you did not write it =) 1. ult 2. sngl 3. mnth 4. ssn 5. po 6. spl <?php while($a = mysql_fetch_assoc($q)) { $packages; $packages[$a['package']] = $a['picks']; } $myOrder = array("utl", "sngl", "mnth", "ssn", "po", "spl"); $packages = orderMyArray($packages, $myOrder); foreach( $packages as $key => $value){ if($key == "ult"){ if($value == ""){ $key1 = ""; } else { $key1 = "Ultimate Lock Pick Package"; } } if($key == "sngl"){ if($value == ""){ $key1 = ""; } else { $key1 = "Single Pick Package"; } } if($key == "mnth"){ if($value == ""){ $key1 = ""; } else { $key1 = "Month Package"; } } if($key == "ssn"){ if($value == ""){ $key1 = ""; } else { $key1 = "Season Package"; } } if($key == "po"){ if($value == ""){ $key1 = ""; } else { $key1 = "Playoffs Package"; } } if($key == "spl"){ if($value == ""){ $key1 = ""; } else { $key1 = "Special Package"; } } echo "<strong>".$key1."</strong><br />".nl2br($value)."<br /><br />"; } function orderMyArray($array, $orderby) { sort($array); $cnt = count($orderby); for ($i=0; $i<$cnt;$i++) { $newArray[$orderby[$i]] = $array[$orderby[$i]]; } return $newArray; } ?> Try that and see if it works. Quote Link to comment https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-719901 Share on other sites More sharing options...
timmah1 Posted December 19, 2008 Author Share Posted December 19, 2008 That makes sense :-) I get an error though. I'm not that familiar with functions Fatal error: Call to undefined function orderMyArray() in /home/vegas/public_html/core/admin/showPicks1.php Quote Link to comment https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-719905 Share on other sites More sharing options...
premiso Posted December 19, 2008 Share Posted December 19, 2008 Note at the end of the code before ?>: function orderMyArray($array, $orderby) { sort($array); $cnt = count($orderby); for ($i=0; $i<$cnt;$i++) { $newArray[$orderby[$i]] = $array[$orderby[$i]]; } return $newArray; } Must be there, or else you get that error. Quote Link to comment https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-719907 Share on other sites More sharing options...
timmah1 Posted December 19, 2008 Author Share Posted December 19, 2008 I have it there Here is the entire code $year = $_POST['year']; $month = $_POST['month']; $day = $_POST['day']; $sport = $_POST['sport']; if($_POST['today'] == "y") { $today = $day1; } else { $today = "$year-$month-$day"; } $sql = "SELECT * FROM dailypicks1 WHERE sport = '$sport' AND active = '$today'"; $q = mysql_query($sql); $numrows = mysql_num_rows($q); if($numrows == 0) { echo "No picks for $today<br>"; echo "<a href='javascript: history.go(-1)'>Go Back</a>"; } echo "<div align='center'><h2>Picks for $sport on $today</h2><br /><br /></div>"; while($a = mysql_fetch_assoc($q)) { $packages; $packages[$a['package']] = $a['picks']; } $myOrder = array("utl", "sngl", "mnth", "ssn", "po", "spl"); $packages = orderMyArray($packages, $myOrder); foreach( $packages as $key => $value){ if($key == "ult"){ if($value == ""){ $key1 = ""; } else { $key1 = "Ultimate Lock Pick Package"; } } if($key == "sngl"){ if($value == ""){ $key1 = ""; } else { $key1 = "Single Pick Package"; } } if($key == "mnth"){ if($value == ""){ $key1 = ""; } else { $key1 = "Month Package"; } } if($key == "ssn"){ if($value == ""){ $key1 = ""; } else { $key1 = "Season Package"; } } if($key == "po"){ if($value == ""){ $key1 = ""; } else { $key1 = "Playoffs Package"; } } if($key == "spl"){ if($value == ""){ $key1 = ""; } else { $key1 = "Special Package"; } } echo "<strong>".$key1."</strong><br />".nl2br($value)."<br /><br />"; } function orderMyArray($array, $orderby) { sort($array); $cnt = count($orderby); for ($i=0; $i<$cnt;$i++) { $newArray[$orderby[$i]] = $array[$orderby[$i]]; } return $newArray; } Quote Link to comment https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-719908 Share on other sites More sharing options...
timmah1 Posted December 19, 2008 Author Share Posted December 19, 2008 i think it might be the if($_POST) Here is the entire code, minus the form <?php require("config.php"); $day1 = date("Y-m-d"); if($_POST['pick']){ $year = $_POST['year']; $month = $_POST['month']; $day = $_POST['day']; $sport = $_POST['sport']; if($_POST['today'] == "y") { $today = $day1; } else { $today = "$year-$month-$day"; } $sql = "SELECT * FROM dailypicks1 WHERE sport = '$sport' AND active = '$today'"; $q = mysql_query($sql); $numrows = mysql_num_rows($q); if($numrows == 0) { echo "No picks for $today<br>"; echo "<a href='javascript: history.go(-1)'>Go Back</a>"; } echo "<div align='center'><h2>Picks for $sport on $today</h2><br /><br /></div>"; while($a = mysql_fetch_assoc($q)) { $packages; $packages["$a[package]"] = $a['picks']; } $myOrder = array("utl", "sngl", "mnth", "ssn", "po", "spl"); $packages = orderMyArray($packages, $myOrder); foreach( $packages as $key => $value) { if($value == ""){ $key1 = ""; } elseif($key == "ult"){ $key1 = "Ultimate Lock Pick Package"; } elseif($key == "sngl"){ $key1 = "Single Pick Package"; } elseif($key == "mnth"){ $key1= "Month Package"; } elseif($key == "ssn"){ $key1 = "Season Package"; } elseif($key == "po"){ $key1 = "Playoffs Package"; } elseif($key == "spl"){ $key1 = "Special Package"; } echo "<strong>".$key1."</strong><br />".nl2br($value)."<br /><br />"; //echo "Special:".$a['spl']; } function orderMyArray($array, $orderby) { sort($array); $cnt = count($orderby); for ($i=0; $i<$cnt;$i++) { $newArray[$orderby[$i]] = $array[$orderby[$i]]; } return $newArray; } } else { ?> Quote Link to comment https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-719915 Share on other sites More sharing options...
premiso Posted December 19, 2008 Share Posted December 19, 2008 Yea, make sure it is outside of any if statements, or else it will not get defined where it needs to be. Quote Link to comment https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-719921 Share on other sites More sharing options...
timmah1 Posted December 19, 2008 Author Share Posted December 19, 2008 ok, i no longer receive an error, but I get no results either Quote Link to comment https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-719930 Share on other sites More sharing options...
timmah1 Posted December 20, 2008 Author Share Posted December 20, 2008 Can anybody tell me why this is not pulling out any results? <?php require("config.php"); $today = date("Y-m-d"); $sport = "nfl"; $sql = "SELECT * FROM dailypicks1 WHERE sport = '$sport' AND active = '$today'"; $q = mysql_query($sql); $numrows = mysql_num_rows($q); if($numrows == 0) { echo "No picks for $today<br>"; echo "<a href='javascript: history.go(-1)'>Go Back</a>"; } echo "<div align='center'><h2>Picks for $sport on $today</h2><br /><br /></div>"; while($a = mysql_fetch_assoc($q)) { $packages; $packages[$a['package']] = $a['picks']; } $myOrder = array("utl", "sngl", "mnth", "ssn", "po", "spl"); $packages = orderMyArray($packages, $myOrder); foreach( $packages as $key => $value){ if($value == ""){ $key1 = ""; } elseif($key == "ult"){ $key1 = "Ultimate Lock Pick Package"; } elseif($key == "sngl"){ $key1 = "Single Pick Package"; } elseif($key == "mnth"){ $key1= "Month Package"; } elseif($key == "ssn"){ $key1 = "Season Package"; } elseif($key == "po"){ $key1 = "Playoffs Package"; } elseif($key == "spl"){ $key1 = "Special Package"; } echo "<strong>".$key1."</strong><br />".nl2br($value)."<br /><br />"; } function orderMyArray($array, $orderby) { sort($array); $cnt = count($orderby); for ($i=0; $i<$cnt;$i++) { $newArray[$orderby[$i]] = $array[$orderby[$i]]; } return $newArray; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-720124 Share on other sites More sharing options...
redarrow Posted December 20, 2008 Share Posted December 20, 2008 TRY IT WITH NO WHERE CLAUSE... SELECT * FROM dailypicks1 ALWAYS BACK UR FILES UP ONLY A SUGGESTION. Quote Link to comment https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-720186 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.