Jump to content

Out put order


timmah1

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/137719-out-put-order/
Share on other sites

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.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-719884
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-719887
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-719890
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-719893
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-719901
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-719907
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-719908
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-719915
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/137719-out-put-order/#findComment-720124
Share on other sites

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.