Jump to content

shaung

Members
  • Posts

    26
  • Joined

  • Last visited

Everything posted by shaung

  1. OK, thanks. I still can't get the values from the database into the array then into the dropdown. Someone suggested I do this: function _options($arr){ $selected = isset($arr['selected']) ? " selected='selected'" : ''; return "<option value='{$arr['id']}'$selected>{$arr['name']}</option>"; } $current_options = $institutions; $options = array_map('_options',$current_options); echo implode("\n",$options); ...but I can't get that to work either. I have like one day a week that I can look at this and I have been stuck on it for three weeks. This sucks to no end. Here is all the pertinent code using the above method. $_SESSION['sid'] = $sid; $edit = false; /*------------------------------------- *Begin adding your stuff here --------------------------------------*/ /*------------------------------------- */ if($_SESSION['edit']) { $sql = "SELECT * FROM fac_detail INNER JOIN institution ON fac_detail.institution_id = institution.id " . "INNER JOIN degree ON fac_detail.degree_id = degree.id WHERE employee_id = . '$_POST[hidden]'"; } else { $sql = "SELECT * FROM fac_detail INNER JOIN institution ON fac_detail.institution_id = institution.id " . "INNER JOIN degree ON fac_detail.degree_id = degree.id"; } try { $sth = $dbh->prepare("SELECT name FROM institution"); $sth->execute(); /* Fetch all of the values of the first column */ $institutions = $sth->fetch(); } catch(PDOException $e) { die($e->getMessage()); } //try //{ //$sth = $dbh->prepare("SELECT degree_type FROM degree"); //$sth->execute(); /* Fetch all of the values of the column */ //$degrees = $sth->fetch(); //} //catch(PDOException $e) { //die($e->getMessage()); //} try { if(isset($_POST['update'])) { $updatedQuery = "UPDATE fac_detail SET first_name=:fname, last_name=:lname, height=:height, cap=:cap,colors=:color WHERE employee_id=:id"; $stmt = $dbh->prepare($updatedQuery); // loop over each record in $_POST['record'] // getting the employee_id and each fields value foreach($_POST['record'] as $employee_id => $field) { $stmt->bindParam(':fname', $field['firstName'], PDO::PARAM_STR); $stmt->bindParam(':lname', $field['lastName'], PDO::PARAM_STR); $stmt->bindParam(':height', $field['height'], PDO::PARAM_STR); $stmt->bindParam(':cap', $field['cap'], PDO::PARAM_STR); $stmt->bindParam(':color', $field['color'], PDO::PARAM_STR); $stmt->bindParam(':id', $employee_id, PDO::PARAM_INT); $stmt->execute(); } } //$sql = "SELECT * FROM fac_detail"; $stmt = $dbh->prepare($sql); $stmt->execute(); $arrValues = $stmt->fetchAll(PDO::FETCH_ASSOC); $row = $stmt->fetch(); } catch(PDOException $e) { die($e->getMessage()); } //Heredoc syntax for echoing out HTML echo <<<HTML <form method= post> <table id="recordTable"> <thead> <tr> <th>First Name</th> <th>Last Name</th> <th>Height</th> <th>Cap</th> <th>Color</th> <th>Degree</th> <th>Institution</th> <th>Edit Record</th> </tr> </thead> HTML; //Array with sid as key if($edit) { $readstate = "readonly"; } else { $readstate = ""; } echo "<tbody>"; foreach ($arrValues as $row) { $id = $row['employee_id']; echo '<tr> <td><input type="text" name="record['.$id.'][firstName]" value="'.$row['first_name'].'" . $readState . /></ td> <td><input type="text" name="record['.$id.'][lastName]" value="'.$row['last_name'].'" . $readState . /></ td> <td><input type="text" name="record['.$id.'][height]" value="'.$row['height'].'" . $readState . /></ td> <td><input type="text" name="record['.$id.'][cap]" value="'.$row['cap'].'" . $readState . /></ td> <td><input type="text" name="record['.$id.'][color]" value="'.$row['colors'].'" . $readState . /></ td> <td><select name="record['.$id.'][degree]"> </td> <option> "Filler" </option> </select> <td><select name="record['.$id.'][school]"> </td>'; if($edit){ foreach($institutions as $val){ echo "<option value='\$val\'>".$val."</option>"; } } echo '</select> <td><input type="submit" name="update" value="Update" /></td> </tr>'; } echo "</tbody>"; echo "</table>"; echo "</form>"; ?> Everything above and below the code is done with a template and works fine. Everything works on this page except the dropdowns. Any hints as to how I can get this to work would be greatly appreciated. When the page loads it shows a table full of form elements filled with data and an edit button for each row. I want them to click the edit button then show the table again with only the single record they clicked with the dropdowns filled.
  2. Hi, I want to put a column from mysql into an array. Should I use fetch() or fetchAll() ? try { $sth = $dbh->prepare("SELECT name FROM institution"); $sth->execute(); /* Fetch all of the institution names from table*/ $institutions = $sth->fetchAll(); } catch(PDOException $e) { die($e->getMessage()); } I then want to iterate through the array and populate a dropdown using the array. foreach ($arrValues as $row) { $id = $row['employee_id']; echo '<tr> <td><input type="text" name="record['.$id.'][firstName]" value="'.$row['first_name'].'" . $readState . /></ td> <td><input type="text" name="record['.$id.'][lastName]" value="'.$row['last_name'].'" . $readState . /></ td> <td><input type="text" name="record['.$id.'][height]" value="'.$row['height'].'" . $readState . /></ td> <td><input type="text" name="record['.$id.'][cap]" value="'.$row['cap'].'" . $readState . /></ td> <td><input type="text" name="record['.$id.'][color]" value="'.$row['colors'].'" . $readState . /></ td> <td><select name="record['.$id.'][degree]"> </td> <option> "Filler" </option> </select> <td><select name="record['.$id.'][school]"> </td> ' if($edit){ foreach($institutions as $option){ echo "<option value='{$option}' </option>"; } } #edit =!$edit; echo '</select> <td><input type="submit" name="update" value="Update" /></td> </tr>'; If the edit variable == true I want to fill the dropdowns. The error says, "Unexpected if" Can anyone explain this?
  3. $dbh is defined in a file that is included in the page. It works perfectly within the page as seen below. $sql = "SELECT * FROM fac_detail INNER JOIN institution ON fac_detail.institution_id = institution.id " . "INNER JOIN degree ON fac_detail.degree_id = degree.id"; $stmt = $dbh->prepare($sql); $stmt->execute(); $arrValues = $stmt->fetchAll(PDO::FETCH_ASSOC); $row = $stmt->fetch(); I then show the returned data in a datatable, works great. The datatable also contains two dropdown lists that I need to fill with database calls to two other tables. I need to run three sql statements to completely fill the table. I wish to reuse code if possible. Right now Im trying to figure out the logic flow to fill the table and dropdowns. If user clicks edit button associated with a row, row becomes editable and dropdowns fill with choosable options. I want to make calls to a function that returns the data to put in the dropdowns. I have tried running the pdo code using if statements but it doesn't work. It seems to go out of scope. Is it possible to have a function that I can pass sql statements to that returns the $row and $arrValues?
  4. Hello, I keep getting a 'Fatal error: Call to a member function prepare() on a non-object' error. My goal is to have a function where I can pass in sql statements and get back the $row and $arrValues. I have included the function file in the file that I make the call in and the include works. Is it possible to do something like this in a separate file, passing in a sql statement? function runStatement($dbh, $sql){ $stmt = $dbh->prepare($sql); $stmt->execute(); return $stmt; } and use it something like this $sql = "SELECT * FROM databaseTable"; $stmt = runStatement($dbh, $sql); $arrValues = $stmt->fetchAll(PDO::FETCH_ASSOC); $row = $stmt->fetch(); Then I could run $arrValues and $row in a loop to show their values as needed. I have been trying figure this out for two days now. My end goal is if bool = true, run sql statement A, else run sql statement B. Anyone?
  5. OK, and one more question. I am trying to choose sql queries in an if statement, but mysql doesn't like it. Alternatively, I am putting the PDO stuff into a function and passing the query in, but that isn't working either. Is there a way to do this without having to write separate PDO code for each query? I want to do something like this: try { if(trueOrfalse) { $sql = "SELECT * FROM myTable"; } else{ $sql = "SELECT * FROM someOtherTable"; } $stmt = $dbh->prepare($sql); $stmt->execute(); $arrValues = $stmt->fetchAll(PDO::FETCH_ASSOC); $row = $stmt->fetch(); } catch(PDOException $e) { die($e->getMessage()); } But I keep getting a syntax error message. Apparently sql doesn't like it. Thanks.
  6. I want to pull two columns, one from each table, in one sql statement. They have no relation to each other. It is to populate two dropdown lists. How can I make this happen? I keep getting errors. Thanks
  7. Simple question about scope. I want to run different sql statements depending on a boolean value in a variable. The value would flip back and forth depending what the user is doing. What/where is the best way to initialize this variable (initially false). The page might reload (on a submit or whatever), but I want the variable to remember it's value. Thanks.
  8. OK, I'm starting to see what is going on now. It's just like one long string. So I should be able to flip the 'readonly' to write by putting it in a variable and just setting it to an empty string as needed... thanks a lot. I have programmed for years off and on, just haven't done it in about 3 years and have never done web programming/scripting before. Appreciate everyone's help.
  9. Hello, I have these form fields in HEREDOC syntax but I need to add more PHP which isn't possible in heredoc. I wish to remove the heredoc for this section, but I suck at echoing form fields. Could someone help me with this? foreach ($arrValues as $row) { $id = $row['employee_id']; echo <<<FORM_FIELDS <tr> <td><input type="text" name="record[$id][firstName]" value="{$row['first_name']}" readonly /></td> <td><input type="text" name="record[$id][lastName]" value="{$row['last_name']}" readonly /></td> <td><input type="text" name="record[$id][height]" value="{$row['height']}" readonly /></td> <td><input type="text" name="record[$id][cap]" value="{$row['cap']}" readonly /></ td> <td><input type="text" name="record[$id][color]" value="{$row['colors']}" readonly /></td> <td><select name="record[$id][degree]" </td> <option> "Filler" </option> </select> <td><select name="record[$id][school]" </td> <option>{$row['name']}</option> </select> <td><input type="submit" name="update" value="Update" /></td> </tr> FORM_FIELDS; } What I need to do is populate the dropdown option boxes from the DB but I cannot use a loop within the heredoc blocks. Apparently PHP loops are not possible within heredoc. I would prefer to just echo everything out, but with my limited PHP skills, I keep making a mess of it. Can anyone help me? Thanks!
  10. Let me be a bit clearer what I'm trying to do. I have a database full of faculty info. When a user logs in, they are directed to a page depending which group they belong to. If they are a faculty member, they are shown their individual record as a single row in a table. If they are in the 'bookstore' group, they see all of the faculty records in a table. Each row shows the users information and an 'edit' button. When the edit button is clicked, I want a webpage to popup that contains editable form fields. They can edit their information as needed and it saves to the database. I have everything working except the popup. IT WON'T POPUP!! That's where I am at. Once I have the popup working all I have to do is input validation and I'm finished. I have been stuck on the popup part for at least three days now. Hope that helps.
  11. I fixed the "Failed to load resource: the server responded with a status of 404 (Not Found)" error. The lightview css and js were not in the right directory. Now the js console shows this error: Uncaught ReferenceError: Prototype is not defined. The error is in the lightview js file, but it is being skipped so it should still work. My real problem is below "No where in your previous posts do you mention or show any code where you have PHP code within JavaScript code." I don't have php within JS. I am calling the js function from within a PHP if statement. if(isset($_POST['update'])) //popup here sayHello(); } When I click the update button, this error shows in the browser: Fatal error: Call to undefined function sayHello() I am still unable to call a simple function.
  12. The js page is loading correctly when I view source. When I look at the js console, I see this error: Failed to load resource: the server responded with a status of 404 (Not Found) Is it because I am calling the function from within php code? If so how do I get around it? I essentially want an iframe to popup with some data in it. The data that pops up is based on a hidden field in a row that the button is on, which is passed to the popup page. I have everything working but the popup, which I cannot get to work for the life of me. Today and tomorrow I finally have time to work on this. Once I get the popup working all I have to do is some input validation and I'm done. Thanks
  13. Nothing shows on the page when I click the button. I am using netbeans. I'm going to rewrite some of the page.
  14. Here is how I am adding the js file (using our work template) //add additional ccs and js files (optional) $hccTemplate->addJSFile("lightview.js", "/lightview/js/"); $hccTemplate->addCSSFile("lightview.css", "/lightview/css/"); $hccTemplate->addCSSFile("jquery.dataTables.css", "https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/"); $hccTemplate->addJSFile("jquery.dataTables.min.js", "https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/"); $hccTemplate->addJSFile("dataTables.js", "required/"); $hccTemplate->addJSFile("showAddForm.js", "required/"); The js file I created is called showAddForm.js. The datatables work beautifully, so I know that the template is including the js files correctly. When I comment out the bottom line and add this: <script src="required/showAddForm.js"></script> <button onclick="showAddForm();">Click me</button> it still doesn't work. The code is in a php file, but not between php tags. Everything else in the file works but I just cannot for the life of me get my js to work. I'm wondering if I have it in the wrong location or something. "Click me" button shows, when I click it the page seems to reload, but nothing happens. Thanks for your help.
  15. Hello, I am new to JS and PHP. I cannot for the life of me get any js function to run, no matter how simple it is. I have the function in a seperate js file. The function pops up an alert that says hello. Of course I have bigger and better plans for what I want to do, but I have to get a basic js function to work before I can do that. What is the minimum I need to do to run a function from a button click? Here are some snippets. in my .js file: function sayHello() { alert('Hello'); } I am including the .js file using the web template from our dept at work, so I know it is correct. in the code I am calling it like this: <button onclick="sayHello();">Click Me</button> Nothing I do will make any javascript work. I know I am missing something very basic. Please.... thx
  16. So populate every dropdown when loading the table? I was just worried that doing that would take a long time load. That certainly would be easier to implement, Will post code here when I get it working. Thanks..
  17. Hi, I have a 2d array of form elements in a table, as seen here. Among those elements are dropdown lists. One shows college names and the other shows what degree that person has. These are initially populated from a database with the single value pertaining to that particular record. If they click the dropdown, I wish to populate it with all the options from that table, from which they can choose one. Example: User logs in and his/her record shows as an editable table row that has two dropdowns. One dropdown shows the college they attended, and the other shows the degree they earned. If they click the 'degree' dropdown, it populates with all the degree options contained in that particular database table - from which they can choose whatever they want. I understand the database part and how to fill the dropdown with options, but I am unsure of how to know which dropdown was clicked, in that particular row, so I can fill it with options. When a faculty member uses the page, he/she will only see their single record BUT when an administrator uses it they will see all the records, which they may have to edit as necessary. How do I know which dropdown has been clicked so I can populate it? Here is the code where I have the dropdowns: foreach ($arrValues as $row){ $id = $row['employee_id']; echo <<<FORM_FIELDS <tr> <td><input type="text" name="record[$id][firstName]" value="{$row['first_name']}" /></td> <td><input type="text" name="record[$id][lastName]" value="{$row['last_name']}" /></td> <td><input type="text" name="record[$id][height]" value="{$row['height']}" /></td> <td><input type="text" name="record[$id][cap]" value="{$row['cap']}" /></ td> <td><input type="text" name="record[$id][color]" value="{$row['colors']}" /></td> //shows degree type <td><select name="record[$id][degree]" </td> <option>{$row['type']}</option> </select> //shows school attended <td><select name="record[$id][school]" </td> <option>{$row['name']}</option> </select>' <td><input type="submit" name="update" value="Update" /></td> </tr> FORM_FIELDS; } Thanks
  18. I figured it out. I use the 'name' field from the table that I'm joining fac_details to. I changed this: <td><select name="record[$id][school]" </td> <option>{$row['institution_id']}</option> to this: <td><select name="record[$id][school]" </td> <option>{$row['name']}</option> and it worked. Thanks!
  19. Here are the tables of the Database: fac_detail, institution, degree fac_detail detail_id mediumint employee_id varchar(11) first_name varchar(25) last_name varchar(50) institution_id mediumint height varchar(5) cap varchar(15) degree varchar(4) colors varchar(25) institution id mediumint name varchar(255) degree degree_type varchar(4) I wish to pull out the name for my particular row from the institution table using an inner join I am not sure which {$row['??????'] would hold the name of the institution (school) after doing the join. Would it be something like {$row['institution.name'] ?? Thanks loads, Shaun
  20. I have one more question. I am now attempting to populate a dropdown using an inner join. Here is the sql statement: $sql = "SELECT * FROM fac_detail INNER JOIN institution ON fac_detail.institution_id = institution.id"; The 'institution' table has( id mediumInt and name varchar(255) ). The 'name' are the colleges the records are associated with...eg. MIT, UW, USC, Milton College, UCLA....etc. I am trying to show the institution associated with each record in a dropdown. Just the one that matches the record for that row. Here is my code: //the headers for the two new columns added (degree and institution) <thead> <tr> <th>First Name</th> <th>Last Name</th> <th>Height</th> <th>Cap</th> <th>Color</th> <th>Degree</th> <th>Institution</th> <th>Edit Record</th> </tr> </thead> ... //attempting to show the result of the inner join in the dropdown <td><select name="record[$id][school]" </td> <option>{$row['institution_id']}</option> My dropdown for the institution is showing the ID number rather than the name of the institution from the other table. I think the problem is in the bolded part of the code. If I can figure out how to show the school name (institution) in the dropdown, I can figure out everything else I need to do from there. Thanks again, Shaun
  21. So heredoc syntax allows HTML to run within PHP without all the echo and escape char hell? OK, thanks, thats good to know. With my limited knowledge of web programming and PHP this saves me a lot of headache. I work in a college IT dept. This app is for our campus bookstore to order graduation gowns for faculty members based on where they went to college. Because of your insight, I now have to make very few pages for this web app. The index page directs users to this page based on group membership. If a faculty member logs in, it directs them to the page, then shows his/her single record based on employee ID number - and they can edit their info as necessary. If they belong to the school bookstore group (who orders gowns based on the info in each record), they see the full table and can edit, delete or add new records. I had been making separate pages for each task, but now I can implement them on one page in a nice pretty datatable with rows editable in line without having to use javascript. I could find no examples online that do this, so hopefully this helps others wishing to do the same. Im lovin' PHP now! Your example works like a champ. I learned a lot from your help. Thanks immeasurably!
  22. Sorry I haven't had a lot of time to work on this. What do you mean by this part (bolded) $id = $row['employee_id']; echo <<<FORM_FIELDS <tr> <td><input type="text" name="record[$id][firstName]" value="{$row['first_name']}" /></td> <td><input type="text" name="record[$id][lastName]" value="{$row['last_name']}" /></td> <td><input type="text" name="record[$id][height]" value="{$row['height']}" /></td> <td><input type="text" name="record[$id][cap]" value="{$row['cap']}" /></ d> <td><input type="text" name="record[$id]" value="{$row['colors']}" /></td> <td><input type="submit" name="update" value="Update" /></td> </tr> FORM_FIELDS; I tried putting the "<form action=facultyPage.php method= post>" part in the loop and it made a really whacked out looking 'table'. Not sure what you mean here. Sorry if I am a bit lost here. I understand your code otherwise. Thanks
  23. Hmmmmm, I just realized something. It is only updating the last record in the table. Any other record that I attempt to update, nothing happens. The last record in the table, which is the one that I added for testing purposes, if I edit it, the edit updates. So the Submit button for the last record is the only one that works. What is going on here? What would cause that?
  24. Thank you for your reply. The end user will be editing a single record at a time, but having the option to update multiple in one shot may also be handy. I will tinker with it a bit and let you know how it worked. THanks. If anyone else has anything to add, would appreciate. it. Thanks, Shaun
×
×
  • 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.