timmah1 Posted December 24, 2008 Share Posted December 24, 2008 how do I order this in the way I want? 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"; } I need it to show like this "ult" "sngl" "mnth" "ssn" "po" "spl" Right now, it display's it in a random order. Thanks in advance Link to comment https://forums.phpfreaks.com/topic/138346-foreach-special-order/ Share on other sites More sharing options...
Mikedean Posted December 24, 2008 Share Posted December 24, 2008 How is the '$packages' array set? Would be easier to know that information too Link to comment https://forums.phpfreaks.com/topic/138346-foreach-special-order/#findComment-723392 Share on other sites More sharing options...
timmah1 Posted December 24, 2008 Author Share Posted December 24, 2008 $packages; $packages["$a[package]"] = $a['picks']; Link to comment https://forums.phpfreaks.com/topic/138346-foreach-special-order/#findComment-723393 Share on other sites More sharing options...
Mikedean Posted December 24, 2008 Share Posted December 24, 2008 Try this, it might sound like we're going the long way, but it's how I would do it. Assign the $packages variables like: $packages; $packages[<specified number for each (0,1,2,3 etc.)>] = arracy( ["$a[package]"] => $a['picks'] ); Then wrap this around the foreach loop and also change the foreach loop itself. for( $i = 0; $i < count( $packages ); $i++ ) { foreach( $packages[ $i ] as $key => $value) { //etc. } I'm assuming it uses a database, but in my eyes this seems like the best way to do it without changing the database. One thing to note is that you'd have to make sure that they are set to the correct number, so if it is grabbing from the database, you might need to do the following if( $a[package] == "ult" ) $packages[0] = arracy( ["$a[package]"] => $a['picks'] ); if( $a[package] == "sngl" ) $packages[0] = arracy( ["$a[package]"] => $a['picks'] ); And so on. I hope this helps . Link to comment https://forums.phpfreaks.com/topic/138346-foreach-special-order/#findComment-723397 Share on other sites More sharing options...
timmah1 Posted December 24, 2008 Author Share Posted December 24, 2008 That seems like it would, but I'm confused on it I don't understand the top array This is the entire code $packages; $packages[for each (0,1,2,3,4,5,6)] = array( ["$a[package]"] => $a['picks'] ); } for( $i = 0; $i < count( $packages ); $i++ ) { //foreach( $packages as $key => $value) { foreach( $packages[ $i ] as $key => $value) { if( $a[package] == "ult" ) $packages[0] = arracy( ["$a[package]"] => $a['picks'] ); if( $a[package] == "sngl" ) $packages[0] = arracy( ["$a[package]"] => $a['picks'] ); /* 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']; } } Link to comment https://forums.phpfreaks.com/topic/138346-foreach-special-order/#findComment-723399 Share on other sites More sharing options...
Mikedean Posted December 24, 2008 Share Posted December 24, 2008 Sorry, I've had a couple of drinks so I probably don't make the best of sense The top array looks like it is being set using database values, is this correct? If yes, then you'd probably need to use the following to set them correctly. $packages; if( $a[package] == "ult" ) $packages[0] = arracy( ["$a[package]"] => $a['picks'] ); if( $a[package] == "sngl" ) $packages[1] = arracy( ["$a[package]"] => $a['picks'] ); if( $a[package] == "mnth" ) $packages[2] = arracy( ["$a[package]"] => $a['picks'] ); if( $a[package] == "ssn" ) $packages[3] = arracy( ["$a[package]"] => $a['picks'] ); if( $a[package] == "po" ) $packages[4] = arracy( ["$a[package]"] => $a['picks'] ); if( $a[package] == "spl" ) $packages[5] = arracy( ["$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 />"; } } That's what I was trying to put across before Hope this is clearer . Link to comment https://forums.phpfreaks.com/topic/138346-foreach-special-order/#findComment-723403 Share on other sites More sharing options...
timmah1 Posted December 25, 2008 Author Share Posted December 25, 2008 I get an error of Parse error: syntax error, unexpected '[', expecting ')' in /home/vegas/public_html/core/admin/showPicks1.php on line 52 And this is line 52 $packages[0] = arracy( ["$a[package]"] => $a['picks'] ); Link to comment https://forums.phpfreaks.com/topic/138346-foreach-special-order/#findComment-723470 Share on other sites More sharing options...
timmah1 Posted December 25, 2008 Author Share Posted December 25, 2008 ok, I got that to work, but now I get an error Warning: Invalid argument supplied for foreach() in /home/vegas/public_html/core/admin/showPicks1.php on line 66 This is the line foreach( $packages[$i] as $key => $value) Link to comment https://forums.phpfreaks.com/topic/138346-foreach-special-order/#findComment-723471 Share on other sites More sharing options...
timmah1 Posted December 25, 2008 Author Share Posted December 25, 2008 I get it to work, but only if all 6 are in the database, if a package isn't in the database, I get the error Warning: Invalid argument supplied for foreach() in /home/vegas/public_html/core/admin/showPicks1.php on line 66 How can I revise this code to only show what's in the database and in that order? I mean, it works when all 6 fields are in the database, but if there are 4, I get that error twice Here is the semi-working code $packages; if( $a['package'] == "ult" ) $packages[0] = array( $a['package'] => $a['picks'] ); if( $a['package'] == "sngl" ) $packages[1] = array( $a['package'] => $a['picks'] ); if( $a['package'] == "mnth" ) $packages[2] = array( $a['package'] => $a['picks'] ); if( $a['package'] == "ssn" ) $packages[3] = array( $a['package'] => $a['picks'] ); if( $a['package'] == "po" ) $packages[4] = array( $a['package'] => $a['picks'] ); if( $a['package'] == "spl" ) $packages[5] = array( $a['package'] => $a['picks'] ); for( $i = 0; $i < count($packages); $i++ ) { foreach( $packages[$i] as $key => $value); { if($value == ""){ $key1 = ""; } 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; default: $key1 = "Nothing"; break; } } } Link to comment https://forums.phpfreaks.com/topic/138346-foreach-special-order/#findComment-723476 Share on other sites More sharing options...
Mikedean Posted December 25, 2008 Share Posted December 25, 2008 If you can modify the database, it might be easier to use that to set it. If you create a new field in that table named 'order' or something along those lines, data type to INT, with a length of 2 and then edit those items to put the order they should appear in. All you would need to do then is have the following in the while loop. $packages; $packages[ $a['order'] ] = array( $a['package'] => $a['picks'] ); Link to comment https://forums.phpfreaks.com/topic/138346-foreach-special-order/#findComment-723531 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.