?
Solved

Reordering lines in text file with PHP

Posted on 2008-10-23
4
Medium Priority
?
389 Views
Last Modified: 2012-05-05
Here is an example of the contents of a text file I am using:
------------menu.txt------------------
Home
Specs
Specials
Fishing
Cost
Contact
Pictures
------------------------------------
I need to change the order of these lines using PHP. For example, if someone clicks a 'Move Up' button next to the word "Specials" on a page, it will swap locations with "Specs". If they click a 'Move Down' button, it will swap places within the file with "Fishing".

I would also like to ensure that errors down appear if the item is at the top but they press 'Move up' (and the same for the last item moving down).
0
Comment
Question by:seandolan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 9

Expert Comment

by:Shahzad Fateh Ali
ID: 22794675
Hi,

Here is the php code, try writing javascript for it and it will start working,

// post reordered menu list and write into the file
 
if(isset($_REQUEST['update'])){
	$menu = $_REQUEST['menu'];
	$fp = fopen('menu.txt','w+');
	foreach($menu as $m){
		@fputs($fp,$m);
	}
fclose($fp);
}
 
//read from file and display html
 
$fp = fopen('menu.txt','r');
$menu = array();
while(!feof($fp)){
 
	$menu[] = fgets($fp);
}

Open in new window

0
 
LVL 3

Author Comment

by:seandolan
ID: 22801793
I don't understand how that code moves a line up or down or how it knows what to reorder. Can you please explain it so that I can use this code.
0
 
LVL 9

Accepted Solution

by:
Shahzad Fateh Ali earned 500 total points
ID: 22801925
Here you are.
Cheers

<?
// post reordered menu list and write into the file
 
if(isset($_REQUEST['update'])){
        $menu = explode(',',$_REQUEST['menu']);
        $fp = fopen('menu.txt','w+');
        foreach($menu as $m){
                @fputcsv($fp,array($m),' ',' ');
        }
fclose($fp);
}
 
//read from file and display html
 
$fp = fopen('menu.txt','r');
$menu = array();
while(!feof($fp)){
 
 		$t = trim(fgets($fp));
        if($t != '') $menu[] = $t;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Reording</title>
<script language="javascript" type="text/javascript">
var menu = ["<?=implode('","',$menu);?>"];
 
	function reorder(index,move){
		var frm = document.getElementById('myForm');
		var inputmenu = document.getElementById('menu');
		
		switch(move){
			case "up":
				if(index == 0) {alert("You cannot move up"); return false;}
				else{
					var tmp = menu[index-1];
					menu[index-1] = menu[index];
					menu[index] = tmp;
					inputmenu.value = menu.toString()
					frm.submit();
				}
				break;
			
			case "down":
				if(index == menu.length-1) {alert("You cannot move down"); return false;}
				else{
					var tmp = menu[index+1];
					menu[index+1] = menu[index];
					menu[index] = tmp;
					inputmenu.value = menu.toString();
					frm.submit();
				}
				break;
		}
	}
</script>
</head>
<body>
<form id="myForm" method="post">
<input type="hidden" id="menu" name="menu" />
<input type="hidden" id="update" name="update" value="1" />
<? $i=0;
	foreach($menu as $value){
?>
<?=$value;?><input type="button" value="Up" onclick="reorder(<?=$i;?>,'up')" /><input type="button" value="Down" onclick="reorder(<?=$i;?>,'down')" /><br />
<?	$i++;}?>
</form>
</body>
</html>

Open in new window

0
 
LVL 3

Author Closing Comment

by:seandolan
ID: 31509548
Thanks! Great answer!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question