Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

flip flop lines in a file

Status
Not open for further replies.

jason246day

Programmer
Jul 9, 2003
127
US
I have a file full of data entries that are displayed on a page, file is reversed before displaying so the last element will display first. I am trying to create a feature that will allow a user to ascend and descend an element in the list. Basically it will flip flop two sequential lines in a file. I think my logic is right, but for some reason it won't replace the values. Anyone have any ideas on this???

Code:
$old_stock=file("db/stock_shop.txt");
$stock_count = count($old_stock);

foreach($old_stock as $old_stock_line) {
     echo "$old_stock_line<br>";
}

for($z=1; $z < $stock_count-1; $z++){
     $first_entry=$old_stock[$z];
     $second_entry=$old_stock[$z+1];

     $exploded=explode("|", $first_entry);
     if($exploded[0]==$id){
          $old_stock[$z]=$second_entry;
	  $old_stock[$z+1]=$first_entry;
     }
}

echo "<br><br>";
foreach($old_stock as $old_stock_line) {
     echo "$old_stock_line<br>"; 
}
 
I'm not really too positive on what you're trying to do here.

Do you want to re-write all the lines to a file, with the possibility that one line may be switched with the one directly below it?

If the original file looks like:

[tt]
Milk|1.99|Dairy|White
Broccoli|3.49|Produce|Green
Cheese|3.29|Dairy|Yellow
Cereal|2.79|Dry|Brown
[/tt]

You want to have the capability of making the file look like this?

[tt]
Milk|1.99|Dairy|White
Cheese|3.29|Dairy|Yellow
Broccoli|3.49|Produce|Green
Cereal|2.79|Dry|Brown
[/tt]

Am I correct? Also, how would the user specify this?

*cLFlaVA
----------------------------
A polar bear walks into a bar and says, "Can I have a ... beer?"
The bartender asks, "What's with the big pause?
 
The functionality you are asking for could be easily implemented with a database, MySQL, postgreSQL etc.

I'm also not exactly clear what you want to achieve. It is not evident from your code.
 
clflava has the idea for the datafile. I will try to illustrate what will be shown in the browser

*each line will have a move up and move down option, except the top and bottom respectively*

Spawn # Description Last Updated
123 test 07/29/04
0987 here 07/29/04
6323 here you go 07/29/04
234 tesing 07/29/04
63 goes here 07/22/04

sample data file
ex: UniqueID|UserSpecifiedID|Description|Date
1|63|description goes here EDITED asdfsa|1090555376
16|234|tesing|1091133006
17|6323|here you go|1091133015
18|0987|here we go again|1091133022
19|123|another testing post|1091133033
 
I am trying to achieve a utility that will allow the user of the script to arrange the data entries any way they feel possible, through the browser. Imagine a table containing data. Each line in the table contains all the data stored for that entry. I want to have arrows off to the right side of each entry. If the up arrow is clicked for a particular entry, then that entry is swapped with the one on top of it. And vice versa for the down arrow. And the first entry would only have a down arrow available, because there is nothing above it, so it can't ascend any farther. And vice versa for the last entry.

Does this help get the idea across???
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top