Jump to content

XAceX

Members
  • Posts

    35
  • Joined

  • Last visited

Posts posted by XAceX

  1. Ok so this is probably a dumb question but like I mentioned im kinda new at PHP. Ok so say I wanted to put a background color on a row in my php table. How would I do that? This is what I have:

    <html><body><?phpecho "<center><b>Hello and welcome to the Tech Support Page. Here you can find simple and thourough instructions on the many different tasks you encounter day to day. This will be the source of help for those who don't know what to do or for those who have done it before but can't quite really remember that one detail and need a guide to go back and check..</center></b><br>";	echo "<center><table border=1>";	echo "<tr>"; 	echo "<td><span style=\"font-weight: bold;\">File</span></td>";	echo "<td ><span style=\"font-weight: bold;\">Date</span></td>";	echo "</tr></center>";  		$current_dir="/applications/fgl/support";		//list all the files in current directory		if ($handle = opendir($current_dir)) {			while (false !== ($file = readdir($handle))) {				if ($file != "." && $file != "..") {									$full_file="$current_dir/$file";					$type=filetype($full_file);      					if($type=="file"){						echo "<tr bgcolor=white>";						echo "<td>$file</td>";						echo "</tr>";					}				}			}			closedir($handle);		}						?></body></html>

    Thats all I have so far. Now I know normally you would put the bg color code with the TD or TR like so:

    <tr bgcolor="red">or <td bgcolor="blue">

    however since there is an echo it's not letting me use it. All I want to do is make the row that has the word file and the word date have a green background. Can someone help me? :)

  2. Oh sweet! thank you so much. I cant wait to try it out. :)

    Yeah, so you just need to dynamically write out the FILE link then. So, you would just check what the current sort is, and display the other one.
    <?phpif ($sort == "alpha")  echo "<a href=\"files.php?sort=chron\">";else  echo "<a href=\"files.php?sort=alpha\">";?>File</a>

  3. WOW! Thanks!! This definitely helps a lot! I know how to put the files in an array that will put them in alphabetical order. Here is a screen cap of my list this will help me explain better.list.gifWhat I need is to make the word FILE into an active link. So if I click it once it sorts that list in alphabetical order. And if I click it again then it goes back. But you did give me a better idea of what im doing so thanks!!! :)

    You can use PHP for that. So basically, you just want to sort a list of things. I guess in order to have 2 options I'll assume alphabetical by name, or by date. So the first step is to set up the list. This can come from a database if the files are recorded in a database, or you can also read the list of files from a certain directory, or you can just manually hard-code a list of files. I'll just assume that it's manual, since that is the easiest to show. So assume you have a list of files in an array, like this:
    <?php$files = array();$new_file = array("name" => "file1.txt", "date" => "2005-12-1");$files[] = $new_file;$new_file = array("name" => "file2.txt", "date" => "2006-3-17");$files[] = $new_file;$new_file = array("name" => "file3.txt", "date" => "2004-6-3");$files[] = $new_file;?>

    So that gives an array of 3 files, each with a name and a date. There are other ways to get the list of files, but for example I'll just use that. So, you need a link on the page that will toggle the sorting between alpha and chrono. This will be tacked onto the end of the link, so the links will look like this:

    Sort the page by: <a href="files.php?sort=alpha">name</a> or <a href="files.php?sort=chron">date</a>

    So the PHP page gets a variable called sort, that should either be "alpha" or "date". It will be easiest to store the user's choice in the session, that way you don't need to edit all other links to include the sort order. So, the next thing to do is check in the session to see if the sort was already set, and if not, set a default. The default will be alpha.

    if (isset($_SESSION['sort']))  $sort = $_SESSION['sort'];else  $sort = "alpha";

    Now we have a value for sort, and we need to check if they clicked on a link to choose a new sort. If so, update the session too.

    if (isset($_GET['sort'])){  $sort = $_GET['sort'];  $_SESSION['sort'] = $sort;}

    So, now we have the list of files, and we know if they want it sorted by name or date. So now we sort the list. Since we are using a complex array, we need to use a custom sort function to compare either the names or the dates. So we define the function to compare names:

    function cmp_names($a, $b){  if ($a['name'] == $b['name'])	return 0;  // a and b are the same  if ($a['name'] < $b['name'])	return -1;  // a is before b  return 1; // a is after b}

    And the function to compare dates:

    function cmp_dates($a, $b){  if ($a['date'] == $b['date'])	return 0;  // a and b are the same  if ($a['date'] < $b['date'])	return -1;  // a is before b  return 1; // a is after b}

    Now we can check what they want to sort by, and use the usort function to sort the list of files by either name or date.

    if ($sort == "chron")  usort($files, "cmp_dates");else  usort($files, "cmp_names");

    So, at this point, the list of files is sorted by whatever the user wanted to sort by. So now when you write the list out it will be in the correct order.I left several things out, like starting the session, going from page to page, etc, but hopefully this gives you a conceptual idea of what you need to do. Ask if you have other questions.

  4. I'm pretty new to PHP up until now I only used HTML for any web stuff I work on. So I don't really have any code to post. But I do have a question. I run a tech support page at work and I'm supposed to use php what my boss wants me to do is find a way to organize this list of files that I post on there. What he wants is for me to post it as a list but have an active button that will sort it by alphabetical order if I click on it and if I click it again un-sort it back to the original state. I have no idea how to do that. Up until now I have just been reading up on PHP using W3 Schools. Can anyone help me. Do I even use php for that? do I use Java script? I dunno. :):)

×
×
  • Create New...