Jump to content

Need some help modifying my code


MildewMan1

Recommended Posts

Ok I got the original code for this off some website, so I'm not sure how everything in it works (i'll put a comment next to what I don't know about). I am having a couple of problems with my modified code. Here is my code, which will read and list how many ".rbs" files are in the folder and then put them in a table and a form.

<?php/** * @author Mark Haynes * @copyright 2007 */echo "<form action=\"nothing.php\" method=\"post\" target=\"_blank\"><table border='1' width='450'><tr><th>Artist</th><th>Select</th></tr>";  /*echo the beginning of the form and table*/getfiles(); //Get the .rbs filesfunction getfiles($dirname = "."){	$pattern = "(\.rbs$)"; //check for .rbs extension	if ($handle = opendir($dirname))	{		while (false !== ($value = readdir($handle))) //not 100% sure what this does, but I think as long as it has something to read, keep going		{			echo "<tr><th>"; //echo parts of table			if(eregi($pattern, $value))  //check to see if this is an rbs file				$value = basename($value, ".rbs"); //display only the name w/o the extension rbs			else		continue;  //otherwise skip it and go to the next				echo "$value"; //print what's in $value			echo "</th><th><input type=\"checkbox\" name=\"Selected\"/></th></tr><br />"; //add form checkboxes to the column two		}		closedir($handle);	}	echo "<input type=\"submit\" value=\"Submit\"/><input type=\"reset\" value=\"Reset\"/></form></table>"; //put the submit/reset buttons and end the table}?>

Ok the problems I am having are: 1. The table appears halfway down the page instead of at the top.2. Even though it does not list the other file types in the folder, it leaves a small space where it would have listed them. (this can be seen right at the very top before the first song...2 blanks)Here is the page example so you can see how it works: (remember you have to scroll down)The Code

Link to comment
Share on other sites

Give this a shot. I made a few minor changes, you can compare the original and mine to find them. I also added a few comments that start with # to indicate a couple changes.

<?php/*** @author Mark Haynes* @copyright 2007*/echo "<form action=\"nothing.php\" method=\"post\" target=\"_blank\"><table border='1' width='450'><tr><th>Artist</th><th>Select</th></tr>";  /*echo the beginning of the form and table*/getfiles(); //Get the .rbs filesfunction getfiles($dirname = "."){	$pattern = "(\.rbs$)"; //check for .rbs extension	if ($handle = opendir($dirname))	{		while (false !== ($value = readdir($handle))) //not 100% sure what this does, but I think as long as it has something to read, keep going		{			if(eregi($pattern, $value)) //check to see if this is an rbs file				$value = basename($value, ".rbs"); //display only the name w/o the extension rbs			else		continue;  //otherwise skip it and go to the next			echo "<tr><td>"; //echo parts of table			# the above line I moved from the top of the loop - only write it out if the file is valid			echo $value; //print what's in $value			echo "</td><td><input type=\"checkbox\" name=\"Selected\"/></td></tr>"; //add form checkboxes to the column two			# I removed a <br> at the end of this line, that was responsible for all the space on top.  you don't need a br between two table rows		}		closedir($handle);	}	echo "<tr><td colspan=\"2\"><input type=\"submit\" value=\"Submit\"/><input type=\"reset\" value=\"Reset\"/></td></tr></table></form>"; //put the submit/reset buttons and end the table}?>

Link to comment
Share on other sites

after reading the comments, that actually makes perfect sense. Guess I just didn't notice it. I'll try this out and let you know if it works. Thank you!Edit: That worked. Thanks a ton man. You wouldn't happen to have any ideas on how to alphabetize the list would you? I tried feeding the words into an array and use asort, but the actual feeding the words in part doesn't seem to work right.

Link to comment
Share on other sites

ok i've got the information into an array and i used the asort function, but it didn't do anything, so I tried something else. All of the values are in the same order. Any help on sorting this would be appreciated. :) Edit: woot I got it sorted. Here is the code I used.

<?php/** * @author Mark Haynes * @copyright 2007 */echo "<form action=\"songselection.php\" method=\"post\" target=\"_blank\"><table border='1' width='450'><tr><th>Artist</th><th>Select</th></tr>";/*echo the beginning of the form and table*/getfiles();//Get the .rbs filesfunction getfiles($dirname = "."){	$i = 0;	$j = 0;	$temp = 0;	$pattern = "(\.rbs$)";//check for .rbs extension	if ($handle = opendir($dirname))	{		while (false !== ($value = readdir($handle)))		{			if (eregi($pattern, $value)) //check to see if this is an rbs file				$basevalue = basename($value, ".rbs");//display only the name w/o the extension rbs			else				continue;//otherwise skip it and go to the next			$songs[$i] = $basevalue;			$fullname[$j] = $value;			$i = $i + 1;			$j = $i;		}		for ($i = 0, $size = count($songs); $i < $size; $i++)		{			for ($k = 0; $k <= $size - 1; $k++)			if (strcmp($songs[$i], $songs[$k]) < 0)			{				$temp = $songs[$i];				$songs[$i] = $songs[$k];				$songs[$k] = $temp;			}		}		for ($i = 0, $size = count($songs); $i < $size; $i++)		{			echo "<tr><td>";//echo parts of table			echo $songs[$i];//echo the array			echo "</td><td><input type=\"checkbox\" name=\"Selected\"/></td></tr>";//add form checkboxes to column two		}		closedir($handle);	}	echo "<tr><td colspan=\"2\"><input type=\"submit\" value=\"Submit\"/><input type=\"reset\" value=\"Reset\"/></td></tr></table></form>";//put the submit/reset buttons and end the table}?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...