Page 1 of 2
Sort by alphabetical order
Posted: Fri Feb 18, 2011 1:57 pm
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>";
}
}
}
?>
Re: Sort by alphabetical order
Posted: Fri Feb 18, 2011 2:39 pm
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 />';
}
Re: Sort by alphabetical order
Posted: Fri Feb 18, 2011 3:23 pm
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.
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
Re: Sort by alphabetical order
Posted: Fri Feb 18, 2011 3:38 pm
by Jonah Bron
Put the $parameters array inside of the first (at the beginning) of the first loop.
Re: Sort by alphabetical order
Posted: Fri Feb 18, 2011 3:50 pm
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 />';
}
?>
Re: Sort by alphabetical order
Posted: Fri Feb 18, 2011 4:27 pm
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:
to
Re: Sort by alphabetical order
Posted: Fri Feb 18, 2011 4:32 pm
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.
Re: Sort by alphabetical order
Posted: Fri Feb 18, 2011 4:59 pm
by Jonah Bron
Put this right before the last loop:
Re: Sort by alphabetical order
Posted: Fri Feb 18, 2011 5:01 pm
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 />';
}
}
?>
Re: Sort by alphabetical order
Posted: Fri Feb 18, 2011 5:10 pm
by Jonah Bron
Oh, I think I see the problem now. Change this:
To
Code: Select all
echo strcmp($value1[1], $value2[1]);
Re: Sort by alphabetical order
Posted: Fri Feb 18, 2011 5:17 pm
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
Re: Sort by alphabetical order
Posted: Fri Feb 18, 2011 5:25 pm
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 />';
}
?>
Re: Sort by alphabetical order
Posted: Fri Feb 18, 2011 5:39 pm
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 />';
}
?>
Re: Sort by alphabetical order
Posted: Fri Feb 18, 2011 6:32 pm
by Jonah Bron
Post clients.dwa (if it's not confidential information).
Re: Sort by alphabetical order
Posted: Fri Feb 18, 2011 6:56 pm
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
}