Jump to content

Recommended Posts

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

$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

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

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

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

Link to comment
https://forums.phpfreaks.com/topic/138809-solved-order-again/#findComment-726021
Share on other sites

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

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

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

<?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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.