timmah1 Posted December 30, 2008 Share Posted December 30, 2008 I have this code that is suppose to put the array in a particular order, it works fine except one thing, it always puts the package "ult" last, when it should be first. Can anybody see why? Here is the code while($a = mysql_fetch_assoc($q)) { $packages; if( $a['package'] == "ult" ) $packages[] = array( $a['package'] => $a['picks'] ); if( $a['package'] == "sngl" ) $packages[] = array( $a['package'] => $a['picks'] ); if( $a['package'] == "mnth" ) $packages[] = array( $a['package'] => $a['picks'] ); if( $a['package'] == "ssn" ) $packages[] = array( $a['package'] => $a['picks'] ); if( $a['package'] == "po" ) $packages[] = array( $a['package'] => $a['picks'] ); if( $a['package'] == "spl" ) $packages[] = array( $a['package'] => $a['picks'] ); for( $i = 0; $i < count($packages); $i++ ) { foreach($packages[$i] as $key => $value) { switch ($key) { case "ult": $key1 = "Ultimate Lock Pick Package"; break; case "sngl": $key1 = "Single Pick Package"; break; case "mnth": $key1 = "Month Package"; break; case "ssn": $key1 = "Season Package"; break; case "po": $key1 = "Playoffs Package"; break; case "spl": $key1 = "Special Package"; break; } } } echo "<strong>" . $key1 . "</strong><br />".nl2br($value)."<br /><br />"; } Thanks in advance Link to comment https://forums.phpfreaks.com/topic/138809-solved-order-again/ Share on other sites More sharing options...
RussellReal Posted December 30, 2008 Share Posted December 30, 2008 I'm sorry.. I don't understand what you mean.. can you give me an example of the return? Link to comment https://forums.phpfreaks.com/topic/138809-solved-order-again/#findComment-725838 Share on other sites More sharing options...
timmah1 Posted December 30, 2008 Author Share Posted December 30, 2008 The return should be "ult" "sngl" "mnth" "ssn" "po" "spl" But right now, its showing "sngl" "mnth" "ssn" "po" "spl" "ult" Link to comment https://forums.phpfreaks.com/topic/138809-solved-order-again/#findComment-725841 Share on other sites More sharing options...
kenrbnsn Posted December 30, 2008 Share Posted December 30, 2008 The order in the final array will be in what ever order the records are gotten from the DB. What is the query you're using? Ken Link to comment https://forums.phpfreaks.com/topic/138809-solved-order-again/#findComment-725844 Share on other sites More sharing options...
timmah1 Posted December 30, 2008 Author Share Posted December 30, 2008 $sql = "SELECT * FROM dailypicks WHERE sport = '$sport' AND active = '$today'"; $q = mysql_query($sql); $numrows = mysql_num_rows($q); if($numrows == 0) { if($sport == "nfl"){ $sports = "NFL Football"; } if($sport == "nba"){ $sports = "NBA Basketball"; } if($sport == "ncaaf"){ $sports = "NCAA Football"; } if($sport == "ncaab"){ $sports = "NCAA Basketball"; } if($sport == "feature"){ $sports = "Featured Sport"; } echo "<div align='center'>No picks for $sports on $today<br></div>"; echo "<div align='center'><a href='javascript: history.go(-1)'>Go Back</a> | <a href='javascript: self.close ()'>Close </a><div align='center'>"; } else { echo "<div align='center'><h2>Picks for $sports on $today</h2><a href='javascript: history.go(-1)'>Go Back</a> | <a href='javascript: self.close ()'>Close </a><br /><br /> </div>"; while($a = mysql_fetch_assoc($q)) { $packages; if( $a['package'] == "ult" ) $packages[] = array( $a['package'] => $a['picks'] ); if( $a['package'] == "sngl" ) $packages[] = array( $a['package'] => $a['picks'] ); if( $a['package'] == "mnth" ) $packages[] = array( $a['package'] => $a['picks'] ); if( $a['package'] == "ssn" ) $packages[] = array( $a['package'] => $a['picks'] ); if( $a['package'] == "po" ) $packages[] = array( $a['package'] => $a['picks'] ); if( $a['package'] == "spl" ) $packages[] = array( $a['package'] => $a['picks'] ); for( $i = 0; $i < count($packages); $i++ ) { foreach($packages[$i] as $key => $value) { switch ($key) { case "ult": $key1 = "Ultimate Lock Pick Package"; break; case "sngl": $key1 = "Single Pick Package"; break; case "mnth": $key1 = "Month Package"; break; case "ssn": $key1 = "Season Package"; break; case "po": $key1 = "Playoffs Package"; break; case "spl": $key1 = "Special Package"; break; } } } echo "<strong>" . $key1 . "</strong><br />".nl2br($value)."<br /><br />"; } } } is the code Link to comment https://forums.phpfreaks.com/topic/138809-solved-order-again/#findComment-725847 Share on other sites More sharing options...
RussellReal Posted December 30, 2008 Share Posted December 30, 2008 sorry.. do me a favor.. do this: print_r($packages); at the end of the script.. and copy/paste the return'd data to this post.. so I can see exactly what $packages is going to look like.. I have a little snippet written for you as of right now, however, idk if it'll work since I don'ty quite know what the return data is.. I see what you typed up.. but I want to see the $packages array to be sure if you don't mind. that'd be rly helpful Link to comment https://forums.phpfreaks.com/topic/138809-solved-order-again/#findComment-725853 Share on other sites More sharing options...
timmah1 Posted December 30, 2008 Author Share Posted December 30, 2008 Doing the search, I get this Array ( [0] => Array ( [0] => sngl ) ) Array ( [0] => Array ( [0] => sngl ) [1] => Array ( [0] => spl ) ) Array ( [0] => Array ( [0] => sngl ) [1] => Array ( [0] => spl ) [2] => Array ( [0] => po ) ) Array ( [0] => Array ( [0] => sngl ) [1] => Array ( [0] => spl ) [2] => Array ( [0] => po ) [3] => Array ( [0] => mnth ) ) Array ( [0] => Array ( [0] => sngl ) [1] => Array ( [0] => spl ) [2] => Array ( [0] => po ) [3] => Array ( [0] => mnth ) [4] => Array ( [0] => ssn ) ) Array ( [0] => Array ( [0] => sngl ) [1] => Array ( [0] => spl ) [2] => Array ( [0] => po ) [3] => Array ( [0] => mnth ) [4] => Array ( [0] => ssn ) [5] => Array ( [0] => ult ) ) Link to comment https://forums.phpfreaks.com/topic/138809-solved-order-again/#findComment-725859 Share on other sites More sharing options...
timmah1 Posted December 30, 2008 Author Share Posted December 30, 2008 Would you like the link so that you can see? Link to comment https://forums.phpfreaks.com/topic/138809-solved-order-again/#findComment-725861 Share on other sites More sharing options...
RussellReal Posted December 30, 2008 Share Posted December 30, 2008 yeas but I'm gonna work something out here for you.. sorry I was away for a little while Link to comment https://forums.phpfreaks.com/topic/138809-solved-order-again/#findComment-726008 Share on other sites More sharing options...
RussellReal Posted December 30, 2008 Share Posted December 30, 2008 <? $packages = array("ult","sngl","mnth","ssn","po","spl"); while ($a = mysql_fetch_assoc($q)) { $packages[array_search($a['package'])] = array( $a['package'] => $a['picks'] ); } print_r($packages); ?> idk if this is exactly what you need.. but here ya go Link to comment https://forums.phpfreaks.com/topic/138809-solved-order-again/#findComment-726021 Share on other sites More sharing options...
timmah1 Posted December 30, 2008 Author Share Posted December 30, 2008 Thanks Russell, These are the results I get Warning: Wrong parameter count for array_search() in /home/vegas/public_html/core/admin/showPicks1.php on line 56 Array ( [0] => ult [1] => sngl [2] => mnth [3] => ssn [4] => po [5] => spl [] => Array ( [ult] => Link to comment https://forums.phpfreaks.com/topic/138809-solved-order-again/#findComment-726414 Share on other sites More sharing options...
kenrbnsn Posted December 30, 2008 Share Posted December 30, 2008 Hint ... when displaying arrays on the browser, use: <?php echo '<pre>' . print_r($array,true) . '</pre>'; ?> It will display in a much more readable fashion. Ken Link to comment https://forums.phpfreaks.com/topic/138809-solved-order-again/#findComment-726464 Share on other sites More sharing options...
timmah1 Posted December 30, 2008 Author Share Posted December 30, 2008 I have the code like this $packages = array("ult","sngl","mnth","ssn","po","spl"); while ($a = mysql_fetch_assoc($q)) { $packages[array_search($a['package'])] = array( $a['package'] => $a['picks'] ); echo '<pre>' . print_r($packages,true) . '</pre>'; //Thanks kenrbnsn } It shows the array as this Array ( [0] => ult [1] => sngl [2] => mnth [3] => ssn [4] => po [5] => spl [] => Array ( But when actually displaying everything with it, it puts [0] => ult last, and I get this error Warning: Wrong parameter count for array_search() in /home/vegas/public_html/core/admin/showPicks1.php on line 56 Link to comment https://forums.phpfreaks.com/topic/138809-solved-order-again/#findComment-726545 Share on other sites More sharing options...
kenrbnsn Posted December 30, 2008 Share Posted December 30, 2008 Try changing your code to: <?php $packages = array("ult"=>'',"sngl"=>'',"mnth"=>'',"ssn"=>'',"po"=>'',"spl"=>''); while ($a = mysql_fetch_assoc($q)) { $packages[$a['package']] = $a['picks']; echo '<pre>' . print_r($packages,true) . '</pre>'; //Thanks kenrbnsn } ?> Does this give you what you want? Ken Link to comment https://forums.phpfreaks.com/topic/138809-solved-order-again/#findComment-726571 Share on other sites More sharing options...
timmah1 Posted December 30, 2008 Author Share Posted December 30, 2008 Perfect! One more question, how do I get it not to show [po] => [spl] => but just the information? Link to comment https://forums.phpfreaks.com/topic/138809-solved-order-again/#findComment-726599 Share on other sites More sharing options...
premiso Posted December 30, 2008 Share Posted December 30, 2008 <?php $packages = array("ult"=>'',"sngl"=>'',"mnth"=>'',"ssn"=>'',"po"=>'',"spl"=>''); while ($a = mysql_fetch_assoc($q)) { $packages[$a['package']] = $a['picks']; echo '<pre>'; myPrintArray($packages); echo '</pre>'; function myPrintArray($array, $key=false) { if (is_array($array)) { if ($key) { print_r($array, true); }else { echo "Array (\n"; foreach ($array as $keys => $val) { echo $val ."\n"; } echo ")\n"; } } } ?> Not sure if PHP has a way to do it, but that should work. Link to comment https://forums.phpfreaks.com/topic/138809-solved-order-again/#findComment-726601 Share on other sites More sharing options...
kenrbnsn Posted December 30, 2008 Share Posted December 30, 2008 Do something like: <?php foreach ($packages as $f => $v) echo $f . ': ' . $v . '<br>'; ?> or if you just want the values, but not the keys: <?php foreach ($packages as $f => $v) echo $v . '<br>'; ?> Ken Link to comment https://forums.phpfreaks.com/topic/138809-solved-order-again/#findComment-726607 Share on other sites More sharing options...
timmah1 Posted December 30, 2008 Author Share Posted December 30, 2008 Thank you so much kenrbnsn! Link to comment https://forums.phpfreaks.com/topic/138809-solved-order-again/#findComment-726616 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.