Jump to content

shaung

Members
  • Posts

    26
  • Joined

  • Last visited

shaung's Achievements

Newbie

Newbie (1/5)

0

Reputation

  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.
×
×
  • 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.