Sort by alphabetical order

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

AdrenalineSeed
Forum Newbie
Posts: 24
Joined: Sat Oct 09, 2010 5:54 pm

Sort by alphabetical order

Post by AdrenalineSeed »

Hello,

I have this script working to print specific contents of a tab delimited file to the screen.

So it reads a line, delimits it, and collects 3 pieces of information then goes to the next line.

Is it possible to sort the entire outcome by Name (the If($counter == 1) bit of info)?

That way when it prints to the screen, it will have the name, sql, and web info shown in alphabetical order.

I was thinking that it could be added to an array, though how do you know what size the array is going to be, and when you sort the array how to do you tell it to only sort by Name?

Code: Select all

<?php

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


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

$delimiter = "]";
$splitcontents = explode($delimiter, $line);
$counter = "";
?>

<br><br>


<?php
foreach ( $splitcontents as $color )
{

$counter = $counter+1;
If ($counter == 1){
echo "<b>Name: </b> $color <br>";
}
If ($counter == 3){
echo "<b>SQL: </b> $color <br>";
}
If ($counter == 5){
echo "<b>WEB: </b> $color <br>";
}
}

}

?>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Sort by alphabetical order

Post by Jonah Bron »

How about this?

Code: Select all

$parameters = array();

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

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

usort($parameters, 'sortByValue');

foreach ($parameters as $parameter) {
    echo '<b>' . $parameter[0] . ': </b> ' . $parameter[1] . ' <br />';
}
AdrenalineSeed
Forum Newbie
Posts: 24
Joined: Sat Oct 09, 2010 5:54 pm

Re: Sort by alphabetical order

Post by AdrenalineSeed »

Ok here is what I have, though there is no output.

I am confused as to putting a function name into a function as a argument without any argument, then it returns those two empty argumentss into the usort function as one argument. 8O

Code: Select all

<?php

$parameters = array();

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

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

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

$delimiter = "]";
$splitcontents = explode($delimiter, $line);
$counter = "";
?>

<br><br>

<?php

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 />';

}

?>

The text file has lines that look like this

clientname]\\somepath\]someserver]somedatabase]someserver]someservice
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Sort by alphabetical order

Post by Jonah Bron »

Put the $parameters array inside of the first (at the beginning) of the first loop.
AdrenalineSeed
Forum Newbie
Posts: 24
Joined: Sat Oct 09, 2010 5:54 pm

Re: Sort by alphabetical order

Post by AdrenalineSeed »

OK here is what I have. Same blank output. Why would I want to define the array for every line in the text file?

Code: Select all

<?php


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

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

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

$parameters = array();

$delimiter = "]";
$splitcontents = explode($delimiter, $line);
$counter = "";
?>

<br><br>

<?php

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 />';

}

?>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Sort by alphabetical order

Post by Jonah Bron »

Oh wait, put it back on the outside. I didn't realize you had put the last loop on the outside of the first loop.

Change this:

Code: Select all

$counter = "";
to

Code: Select all

$counter = 0;
AdrenalineSeed
Forum Newbie
Posts: 24
Joined: Sat Oct 09, 2010 5:54 pm

Re: Sort by alphabetical order

Post by AdrenalineSeed »

No change, I thought that was odd too, but it was how it was done in an example and it worked. I write vbscript, so php confuses me a lot.

I tried just to see if I could get some output


echo $parameters['Name',0];

and I get a 500 error.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Sort by alphabetical order

Post by Jonah Bron »

Put this right before the last loop:

Code: Select all

print_r($parameters);
AdrenalineSeed
Forum Newbie
Posts: 24
Joined: Sat Oct 09, 2010 5:54 pm

Re: Sort by alphabetical order

Post by AdrenalineSeed »

Ok with this code I get output again. But its not in alphabetical order by Name, its in the same order as the lines in the text file.

Each line has the client name and their server names. I am just trying to sort by alphabetical.

I see the problem. I am still printing to the screen each time I run a loop, I can't know the order until I have finished running all the loops. The problem is it overwrites the array each time it does a loop, because its using $color as the location, and $color is the same on each loop. I am not sure how to make a different $color for each loop...

Code: Select all

<?php


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

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

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

$parameters = array();

$delimiter = "]";
$splitcontents = explode($delimiter, $line);
$counter = 0;
?>

<br><br>

<?php

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 />';

}

}


?>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Sort by alphabetical order

Post by Jonah Bron »

Oh, I think I see the problem now. Change this:

Code: Select all

echo strcmp($value1, $value2);
To

Code: Select all

echo strcmp($value1[1], $value2[1]);
AdrenalineSeed
Forum Newbie
Posts: 24
Joined: Sat Oct 09, 2010 5:54 pm

Re: Sort by alphabetical order

Post by AdrenalineSeed »

Then I get this kind of output.




11WEB: dwhost4
SQL: X
Name: client2


11WEB: dwhost4
SQL: X
Name: client3


11WEB: dwhost2
SQL: X
Name: client1
AdrenalineSeed
Forum Newbie
Posts: 24
Joined: Sat Oct 09, 2010 5:54 pm

Re: Sort by alphabetical order

Post by AdrenalineSeed »

OK here is what I have, this should work, because now it has something to sort by, $counter2. But I dont undertand what sortByValue is doing, or how to modify it correctly.

Wait I still want to sort by name, not by $counter2, but $counter2 keeps information from getting overwritten on each loop.

Code: Select all

<?php


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

function sortByValue($value1, $value2, $value3) {
    echo strcmp($value1, $value2, $value3);
}
$counter2 = 0;

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

$parameters = array();

$delimiter = "]";
$splitcontents = explode($delimiter, $line);
$counter = 0;
?>

<br><br>

<?php

foreach ( $splitcontents as $color ) {
    $counter++;
    if ($counter == 1){
        $parameters[] = array($counter2, 'Name', $color);
    } elseif ($counter == 3){
        $parameters[] = array($counter2, 'SQL', $color);
    } elseif ($counter == 5){
        $parameters[] = array($counter2, 'WEB', $color);
    }
} // end of loop by delimiter within a line

$counter2++;


} //end of loop by line

usort($parameters, 'sortByValue');

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

?>
AdrenalineSeed
Forum Newbie
Posts: 24
Joined: Sat Oct 09, 2010 5:54 pm

Re: Sort by alphabetical order

Post by AdrenalineSeed »

Edit: oh its not in alphabetical order, its in some really random order, like showing 2 web server names for 1 client and such. I am thinking it might just be easier to create a database, put it in there, then extract it with a select statement just to sort it correctly.

Aha! Was tricked the whole time. I moved the print back out of the loops from the original code and got no content again, scrolled waaaay down at the bottom of a blank page, there they were, in alphabetical order!

Here is the working code, don't know why its putting a huge blank page before it prints though.

Code: Select all

<?php


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

function sortByValue($value1, $value2) {
    echo strcmp($value1, $value2);
}
$counter2 = 0;

$parameters = array();

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


$delimiter = "]";
$splitcontents = explode($delimiter, $line);
$counter = 0;
?>

<br><br>

<?php


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

?>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Sort by alphabetical order

Post by Jonah Bron »

Post clients.dwa (if it's not confidential information).
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Sort by alphabetical order

Post by McInfo »

AdrenalineSeed wrote:[...]why [it's] putting a huge blank page before it prints[...]

Code: Select all

<?php
foreach ($lines as $line_num => $line) {
    ?><br><br><?php
}
Post Reply