Page 2 of 2

Re: Sort by alphabetical order

Posted: Fri Feb 18, 2011 7:46 pm
by Jonah Bron
Aha! Good catch, Info. Try this:

Code: Select all

<?php


$lines = file('c:\dwagent\clients.dwa');
$delimiter = "]";

function sortByValue($value1, $value2) {
    echo strcmp($value1[1], $value2[1]);
}

foreach ($lines as $line_num => $line) {

    $parameters = array();

    $splitcontents = explode($delimiter, $line);
    $counter = 0;

    foreach ( $splitcontents as $color ) {

        $counter++;
        if ($counter == 1){
            $parameters[] = array('Name', $color);
        } elseif ($counter == 3){
            $parameters[] = array('SQL', $color);
        } elseif ($counter == 5){
            $parameters[] = array('WEB', $color);
        }
    }

    usort($parameters, 'sortByValue');

    foreach ($parameters as $parameter) {
        echo '<b>' . $parameter[0] . ': </b> ' . $parameter[1] . ' <br />';
    }
}


?>

Re: Sort by alphabetical order

Posted: Tue Feb 22, 2011 5:25 pm
by AdrenalineSeed
Ok that took out the big space, lol. I don't see any extra lines in the file at the top.

I can't post the file but here is sort of what it looks like

Client1]\\path]server]db]host]service5
Client2]\\path2]server2]db2]host4]service

Re: Sort by alphabetical order

Posted: Tue Feb 22, 2011 6:34 pm
by VladSun

Code: Select all

$parameters = array();

if (($handle = fopen("c:\dwagent\clients.dwa", "r")) !== FALSE) 
{
	while (($csv = fgetcsv($handle, 1000, "]")) !== FALSE) 
	{
		$parameters[] = array
		(
			'Name' 	=> $csv[1],
			'SQL'	=> $csv[3],
			'WEB'	=> $csv[5],
		);
	}
}

function sortByName($value1, $value2) 
{
	return strcmp($value1['Name'], $value2['Name']);
}

usort($parameters, 'sortByName');

foreach ($parameters as $parameter) 
{
	echo $parameter['Name'] . ': ' . $parameter['SQL'] . ' : ' . $parameter['WEB'];
}
PS:

Code: Select all

echo strcmp($value1[1], $value2[1]);
:lol:

Re: Sort by alphabetical order

Posted: Wed Feb 23, 2011 11:07 am
by AdrenalineSeed
Vlad! It worked! Thanks, and thanks everyone for your help on this. A round on me :drunk: