Jump to content

Alcain

New Members
  • Content Count

    4
  • Joined

  • Last visited

Community Reputation

0 Neutral

About Alcain

  • Rank
    Newbie
  1. Thank you, everyone, for the input. I found Sepodati's answer to be the easiest to implement. I have the code working perfectly now.
  2. THIS IS FOR HOMEWORK, so please do not give me the direct answer but maybe the logic behind it. I cannot seem to figure this out. The problem: I need to generate a dynamic table by column and not by row. The user enters a score for an exam (ie 100) on an HTML page. That value is passed and I need to create 100 different entries in a table. There is also a box that allows the user to select how many columns they want the 100 entries displayed in. So instead of having 100 rows going all the way down the page, a table with 5 columns (if they choose 5) will show while still having all 100 entries in it. What I currently have is (all the way to 1, in 1 big column) 100 -> 100% (A) 99 -> 99% (A) 98 -> 98% (A) 97 -> 97% (A) 96 -> 96% (A) 95 -> 95% (A) 94 -> 94% (A) 93 -> 93% (A) 92 -> 92% (A) 91 -> 91% (A) 90 -> 90% (A) 89 -> 89% (B) 88 -> 88% (B) 87 -> 87% (B) In essence, the table will be generated with 100 entries, still going down the page, but if the user selects 5 columns then the 100 entries needs to be broken down into 5 columns. So it should look like this 100 -> 100% (A) 93 -> 93% (A) continued on till 1 99 -> 99% (A) 92 -> 92% (A) 98 -> 98% (A) 91 -> 91% (A) 97 -> 97% (A) 90 -> 90% (A) 96 -> 96% (A) 89 -> 89% (B) 95 -> 95% (A) 88 -> 88% (B) 94 -> 94% (A) 87 -> 84% (B) $max_points is the score for the exam. $precision is if the user wants decimals or not $percent is the percent of the users score. for ($i = $max_points; $i > 0; $i--) { $p = ($i / $max_points) * 100; if ($precision == 0) $percent = round($p, 0); else $percent = number_format($p, 2, '.', ''); echo "<tr>"; echo "<td class='points'>$i ⇒</td>"; echo "<td class='pctGrade'>$percent%</td>"; if ($percent >= $a_min) echo "<td class='ltrGrade'>(A)</td>"; elseif ($percent >= $b_min) echo "<td class='ltrGrade'>(B)</td>"; elseif ($percent >= $c_min) echo "<td class='ltrGrade'>(C)</td>"; elseif ($percent >= $d_min) echo "<td class='ltrGrade'>(D)</td>"; else echo "<td class='ltrGrade'>(F)</td>"; echo "</tr>"; echo "</th>"; } echo "</table>";
  3. I probably should have specified more. This is not something that will be implemented for a business or personal use, it's for an assignment that I'm currently stumped on. I fully understand that we don't want users to manipulate data. So for the purposes of this assignment: 1. User enters data into a form (done) 2. That data is saved into a text file (done) 3. Another page allows the user to edit the text files that are in the current directory How I went about accomplishing this is there is a dropbox with the all the text files in the directory. The user chooses which file they want to edit, and a text area is then created and populated with the information from the text file. The user then can edit the information they want, and click a button to "submit" their edit. My problem that I can't quite get my head around is keeping the value of the select list for when they edit the text file. I realize this is not a practical or professional way to go about solving it, but like I said I am a beginner and this is how I thought about doing it. This is the entire code for the page, the problem comes when the text area is populated with the information from the file. I edit whatever I want to edit, and hit the "edit report" button. Even though I have a different file open, it will take the value of the current option in the select list which upon the first submit defaults back to the top selection. So even if I choose to edit say the 4th file, when I hit edit report, it saves it to the 1st file. <article> <h2>Software Bug Reports</h2> <h3 id="title">Edit Report</h3> <form action="?" method="post"> <fieldset id="bugReports"> Select the bug report you would like to edit: <br /> <select name="bugReport"> <?php // creating a dropbox for user to choose which report they wish to edit // read all entries in current directory if ($handle = opendir('.')) { // loop through all the files while (false !== ($file = readdir($handle))) { // strip everything that doesn't end in txt if ($file != "." && $file != ".." && strtolower(substr($file, strrpos($file, '.') + 1)) == 'txt') { // create options in the dropbox with a value of the file the user chose echo "<option value={$file}>".basename($file)."</option>"; echo "<br />"; } } // close directory closedir($handle); } ?> </select> <br /> <br /> </fieldset> <fieldset id="submitbutton"> <input type="submit" id="submitBtn" name="submit" value="Edit report" /> </fieldset> <br /><br /> <?php // if user chooses a report and clicks "edit report" if (isset($_POST['submit'])) { // create variable for requesting the file from the drop box $report = $_POST['bugReport']; // create variable to retrieve all contents from the file chosen by the dropbox $print = file_get_contents($report); // create a text area and print all the information from the file into it echo "<textarea id='updateText' name='updateText' rows='7' cols='60'>{$print}</textarea>"; // create a new update file button for editting echo "<input type='submit' name='updateFile' value='Update File'>"; } // if the user clicks on "update file" if (isset($_POST['updateFile'])) { // retrieve the file the user chose to edit $report = $_POST['bugReport']; // opens the file $newFile = fopen($report, "w+") or die("Unable to open file!"); // retrieves the updated text from the textarea $newText = $_REQUEST['updateText']; // prints the newly updated text from the textarea into the file fwrite($newFile, $newText . "\r\n"); echo "{$report} updated!"; } ?> </form> </article>
  4. <select name="bugReport"> <?php if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && strtolower(substr($file, strrpos($file, '.') + 1)) == 'txt') { echo "<option value={$file}>".basename($file)."</option>"; echo "<br />"; } } // close directory closedir($handle); } ?> </select> Alright so I am fairly new at PhP programming and I'm trying to figure out how to maintain my selection after submitting the form. The thought process here is to create a select list that is dynamically created. It will read through my current directory, and exclude all files not ending in txt. It will assign the value of the file to the option, so if the user chooses an option and hits submit, the file's contents will show. My problem is that when I choose a text file to show and hit submit, the contents of the file show correctly (code isn't provided for this part as it's not the issue), but the select option returns to the default. I want the select list to maintain the option that the user chose after the form is submitted. I've been browsing forums and messing around myself but just cannot figure this issue out.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.