Jump to content

foreach special ORDER


timmah1

Recommended Posts

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

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 :).

 

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'];
}
}

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 :P

 

Hope this is clearer :).

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;	
      }
     
}
}

 

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'] );

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.