Jump to content

benanamen

Members
  • Posts

    2,134
  • Joined

  • Last visited

  • Days Won

    42

Everything posted by benanamen

  1. That is not any kind of login code.
  2. Ok, got it. When it comes to doing something in twig I don't know what should be obvious yet. You have already shown me several twig things that were not obvious to me. I thought ?{{ {'id': get.id}|url_encode }} was some sort of dynamic twig thing to get the whole url replacing $_SERVER['SCRIPT_NAME']. I pretty much have the jist of using TWIG with mysql now thanks to you and kicken. It was only 4 days ago that I even really looked at twig. So here is the (hopefully) final version of the template and pre-template code. I am sure there is probably some fine tuning that can be done somewhere. Any further comments or improvements welcome. Current Template <form class="form-horizontal" action="{{ form_action }}?{{ { 'p': get.p, 'id': get.id }|url_encode }}" method="post"> <fieldset> <legend> {% if get.action == 'add' %}Add{% endif %}{% if get.action == 'edit' %}Edit{% endif %} Membership Types </legend> <!-- Select Basic --> <div class="form-group {{ error.membership_category_id ? 'has-error' : '' }}"> <label class="col-md-4 control-label" for="membership_category_id">Membership Category <span style="color: #FF0000;">*</span></label> <div class="col-md-4"> <select id="membership_category_id" name="membership_category_id" class="form-control"> <option value="" style="display:none">Select Category</option> {% for option in category %} <option value="{{ option.membership_category_id }}" {% if form_data.membership_category_id is defined and option.membership_category_id == form_data.membership_category_id %}selected{% endif %}>{{ option.membership_category }}</option> {% endfor %} </select> </div> </div> <!-- Text input--> <div class="form-group {{ error.membership_type ? 'has-error' : '' }}"> <label class="col-md-4 control-label" for="membership_type">Membership Type <span style="color: #FF0000;">*</span></label> <div class="col-md-4"> <input id="membership_type" name="membership_type" type="text" placeholder="Membership Type" class="form-control input-md" value="{{ form_data.membership_type ?? '' }}"> </div> </div> <!-- Text input--> <div class="form-group {{ error.membership_type_description ? 'has-error' : '' }}"> <label class="col-md-4 control-label" for="membership_type_description">Membership Type Description <span style="color: #FF0000;">*</span></label> <div class="col-md-4"> <input id="membership_type_description" name="membership_type_description" type="text" placeholder="Membership Type Description" class="form-control input-md" value="{{ form_data.membership_type_description ?? '' }}"> </div> </div> <div class="form-group"> <label class="col-md-4 control-label"></label> <div class="col-md-4"> <label class="checkbox-inline" for="is_promo"> <input type="checkbox" name="is_promo" id="is_promo" value="1" {% if form_data.is_promo %}checked{% endif %} > Promotional </label> <label class="checkbox-inline" for="is_active"> <input type="checkbox" name="is_active" id="is_active" value="1" {% if form_data.is_promo %}checked{% endif %} > Active </label> </div> </div> <div class="form-group"> <div class="col-md-offset-4 col-md-4"> {% if get.id is not null %}<input type="hidden" name="id" value="{{ get.id }}">{% endif %} <input type="submit" name="submit" value="Submit" class="btn btn-primary"> </div> </div> </fieldset> </form> <?php $error = []; if ($_SERVER['REQUEST_METHOD'] == 'POST') // update or add record { //------------------------------------------------------------------------------------ // Trim Data, Check Missing Fields //------------------------------------------------------------------------------------ include './includes/validate_membership_types.php'; //------------------------------------------------------------------------------------ // Check for errors //------------------------------------------------------------------------------------ if ($error) { show_form_errors($error); } //------------------------------------------------------------------------------------ // Update Data //------------------------------------------------------------------------------------ elseif (isset($_GET['id'])) { $sql = "UPDATE membership_types SET membership_category_id =?, membership_type =?, membership_type_description =?, is_promo =?, is_active =? WHERE membership_type_id=?"; $stmt = $pdo->prepare($sql); $stmt->execute(array( $_POST['membership_category_id'], $_POST['membership_type'], $_POST['membership_type_description'], $_POST['is_promo'], $_POST['is_active'], $_POST['id'] )); die(header("Location: {$_SERVER['SCRIPT_NAME']}?p=list_membership_types&edit")); } //------------------------------------------------------------------------------------ // Insert Data //------------------------------------------------------------------------------------ else { $sql = "INSERT INTO membership_types (membership_category_id, membership_type, membership_type_description, is_promo, is_active) VALUES (?, ?, ?, ?, ?)"; $stmt = $pdo->prepare($sql); $stmt->execute(array( $_POST['membership_category_id'], $_POST['membership_type'], $_POST['membership_type_description'], $_POST['is_promo'], $_POST['is_active'] )); die(header("Location: {$_SERVER['SCRIPT_NAME']}?p=list_membership_types&insert")); } $form_data = $_POST; } //------------------------------------------------------------------------------------ // Initial form for editing record //------------------------------------------------------------------------------------ elseif (isset($_GET['id'])) { $sql = "SELECT membership_category_id, membership_type, membership_type_description, is_promo, is_active FROM membership_types WHERE membership_type_id=?"; $stmt = $pdo->prepare($sql); $stmt->execute(array( $_GET['id'] )); $form_data = $stmt->fetch(PDO::FETCH_ASSOC); } //------------------------------------------------------------------------------------ // Initial form for adding record //------------------------------------------------------------------------------------ else { $form_data = array(); } //------------------------------------------------------------------------------------ // Pass $form_data to template //------------------------------------------------------------------------------------ echo $twig->render('form_membership_types.twig', [ 'form_action' => $_SERVER['SCRIPT_NAME'], 'get' => $_GET, 'form_data' => $form_data, 'error' => $error, 'category' => $pdo->query('SELECT membership_category_id, membership_category FROM membership_category')->fetchAll(), ]); ?>
  3. Still not right. It needs the GET p page name value. On submit goes to http://localhost/mydev/index.php?id=1 This works: <form class="form-horizontal" action="{{ form_action }}?p={{ get.p }}{% if get.id %}&id={{ get.id }}{% endif %} " method="post"> Is there an improvement to that? FYI: 'form_action' => $_SERVER['SCRIPT_NAME'],
  4. <form method="post" action="{{ {'id': get.id}|url_encode }}"> @Jaques1, This does not work as is. This is a working url: http://localhost/mydev/index.php?p=edit_newdev&id=1 Doing what you posted results in <form class="form-horizontal" method="post" action="id=1"> To the URL http://localhost/mydev/id=1
  5. What would you suggest for a function for the missing required fields? The validation include is just simple ifs that add errors to the error array if there is a missing field. The validation currently is just for missing required fields and a function call converting certain empty fields to null.
  6. Thank you. Starting from a blank page I used the code from #37. The problem of overriding fields is fixed and now you can Insert and Update from a single twig template. Additionally, you can also insert and update from a single php file, something I didn't have before. The problem I now have is with the dropdown. The dropdown is used for both the insert and update. Currently the query to get that data is part of the update record code. A new record is not being updated, so how do I best go about handling it? I also see that you show a function to get the update data. I assume that is a better way to go about getting the data. I will come to that when I get everything else sorted out. ($membership_data = load_membership_data($_GET['id']);) Aside from what is mentioned, any other issues at this point? The part for the dropdown. Where would this go now? $options = array(); foreach ($stmt as $row2) { $options[] = [ 'selected' => isset($membership_category_id) && $membership_category_id == $row2['membership_category_id'], 'value' => $row2['membership_category_id'], 'label' => $row2['membership_category'] ]; } Current version of pre-template code <?php // TODO: Change arrays to new syntax [] $error = array(); if ($_SERVER['REQUEST_METHOD'] == 'POST') // update or add record { //------------------------------------------------------------------------------------ // Trim Data, Check Missing Fields //------------------------------------------------------------------------------------ include './includes/validate_membership_types.php'; //------------------------------------------------------------------------------------ // Check for errors //------------------------------------------------------------------------------------ if ($error) { show_form_errors($error); } //------------------------------------------------------------------------------------ // Update Data //------------------------------------------------------------------------------------ elseif (isset($_GET['id'])) { die('Record Updated'); //TODO: Temp Dev $sql = "UPDATE membership_types SET membership_category_id =?, membership_type =?, membership_type_description =?, is_promo =?, is_active =? WHERE membership_type_id=?"; $stmt = $pdo->prepare($sql); $stmt->execute(array( $_POST['membership_category_id'], $_POST['membership_type'], $_POST['membership_type_description'], $_POST['is_promo'], $_POST['is_active'], $_POST['id'] )); die(header("Location: {$_SERVER['SCRIPT_NAME']}?p=list_membership_types&edit")); } //------------------------------------------------------------------------------------ // Insert Data //------------------------------------------------------------------------------------ else { // @TODO add new record /*$sql = "INSERT INTO membership_types (membership_category_id, membership_type, membership_type_description, is_promo, is_active) VALUES (?, ?, ?, ?, ?)"; $stmt = $pdo->prepare($sql); $stmt->execute(array( $_POST['membership_category_id'], $_POST['membership_type'], $_POST['membership_type_description'], $_POST['is_promo'], $_POST['is_active'] )); die(header("Location: {$_SERVER['SCRIPT_NAME']}?p=list_membership_types&insert"));*/ die('Record Added'); // Temp Dev } $membership_data = $_POST; } //------------------------------------------------------------------------------------ // Initial form for editing record //------------------------------------------------------------------------------------ elseif (isset($_GET['id'])) { //$membership_data = load_membership_data($_GET['id']); $sql = "SELECT membership_category_id, membership_type, membership_type_description, is_promo, is_active FROM membership_types WHERE membership_type_id=?"; $stmt = $pdo->prepare($sql); $stmt->execute(array( $_GET['id'] )); $membership_data = $stmt->fetch(PDO::FETCH_ASSOC); } //------------------------------------------------------------------------------------ // Initial form for editing record //------------------------------------------------------------------------------------ else { $membership_data = array(); } //------------------------------------------------------------------------------------ // Pass $membership_data to template //------------------------------------------------------------------------------------ echo $twig->render('form_membership_types.twig', array( 'action' => 'Edit', 'form_action' => $_SERVER['SCRIPT_NAME'], 'get' => $_GET, 'data' => $membership_data, 'error' => $error )); ?>
  7. EXACTLY! For edit mode, we start off with a table list of records which also includes an edit button on each row. That button POST's the ID which is used to query for the edit data. That is the exact problem I am now seeing. I never noticed the problem before. So the edit button should GET instead of POST if I am understanding correctly. So in the function would I then be using if ($_SERVER['REQUEST_METHOD'] == 'POST') AND if ($_SERVER['REQUEST_METHOD'] == 'GET'){ ? From a clean slate, no code, what is the optimum way to use a single twig template for add and edit? That's really the bottom line. What I was doing is obviously not right.
  8. Yeah, I forgot about that. I was posting from my phone away from my DEV setup. It seems I should have done my twig testing inside the app. I would have seen that right away. Funny you mentioned that. That is something i noticed yesterday while I was testing. I checked the app and it has the same problem. Never noticed it before since the fields I had been dealing with were all required fields. Back to the drawing board!
  9. If you look at post #20, i do have it as 3 strictly separated cases there and that is also how the original non-twig template is. Only difference that will be now, is you showed me a cleaner option to not repeat all the if statements and by putting it to a function, will tremendously reduce the code in the app. So this updated function along with a ternary in the template field to = ' ' as in value="{{ membership_type ?? '' }}" should do it right? function data_source($form_fields) { $field_data = []; foreach ($form_fields as $form_field) { if (!empty($_POST[$form_field])) { $field_data[$form_field] = $_POST[$form_field]; } if (!empty($row[$form_field])) { $field_data[$form_field] = $row[$form_field]; } } return $field_data; }
  10. Where you have the empty set in the else, the original template had a ternary to set to empty in the form field instead so really the else should not be there in which case the logic would work as it has been. I had just not tested it in the app yet to notice that issue.
  11. The loop you showed in #24 would need to be used numerous times with various pages so I made it into a function as shown here. Any problems doing this? <?php function data_source($form_fields) { $field_data = []; foreach ($form_fields as $form_field) { if (!empty($_POST[$form_field])) { $field_data[$form_field] = $_POST[$form_field]; } elseif (!empty($row[$form_field])) { $field_data[$form_field] = $row[$form_field]; } else { $field_data[$form_field] = ''; } } return $field_data; } $_POST['membership_type'] = 'Trial'; $_POST['is_promo'] = 'Yes'; $form_fields = [ 'membership_category_id', 'membership_type', 'membership_type_description', 'is_promo', 'is_active', ]; $arr = data_source($form_fields) ; ?> And then send the array to the template..... echo $twig->render('template.twig', ['result' => $arr]);
  12. Very helpful. Thank you. Isn't it not necessary to create the empty array $membership_data = []; since the array is being created no matter what?
  13. I am. The app uses a single index file and includes each page from the $_GET value along with several other includes (db connection, functions). It was easier for testing to pull an edit "page" out of the app as a stand alone page while I learn TWIG.
  14. Not sure how you are meaning to do this. Code has been updated. Thanks. It is just there for testing twig. The app actually uses set_exception handler
  15. Anyone have any feedback If the previous post was coded correctly for twig?
  16. You posted in the wrong forum. That code is obsolete and has been completely removed from PHP. Yes, dont use obsolete code.
  17. I re-read the linked post. I dont know what you mean by pre-rendering html. If you are refering to post #17 that is not how the template file is. So we can be on the same page in getting this right, here is the current code. Please advise on what you see here. Thanks for your help on this. This works exactly as expected: edit_membership_types.php <?php echo "<pre>"; print_r($_POST); echo "</pre>"; //---------------------------------------------------------------------------------------- // Update Data //---------------------------------------------------------------------------------------- $error = array(); if (isset($_POST['update'])) { //------------------------------------------------------------------------------------ // Trim Data, Check Missing Fields //------------------------------------------------------------------------------------ include './includes/validate_membership_types.php'; //------------------------------------------------------------------------------------ // Check for errors //------------------------------------------------------------------------------------ if ($error) { //show_form_errors($error); } else { die; $sql = "UPDATE membership_types SET membership_category_id =?, membership_type =?, membership_type_description =?, is_promo =?, is_active =? WHERE membership_type_id=?"; $stmt = $pdo->prepare($sql); $stmt->execute(array( $_POST['membership_category_id'], $_POST['membership_type'], $_POST['membership_type_description'], $_POST['is_promo'], $_POST['is_active'], $_POST['id'] )); die(header("Location: {$_SERVER['SCRIPT_NAME']}?p=list_membership_types&edit")); } // End Else } // End POST //---------------------------------------------------------------------------------------- // Get Update Data //---------------------------------------------------------------------------------------- $hostdb = 'localhost'; $dbname = 'test'; $username = 'root'; $password = ''; $pdo = new PDO("mysql:host=localhost;dbname=$dbname", $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); require_once 'Twig/Autoloader.php'; Twig_Autoloader::register(); try { $_POST['id'] = 1; // Temp Dev $sql = "SELECT membership_category_id, membership_type, membership_type_description, is_promo, is_active FROM membership_types WHERE membership_type_id=?"; $stmt = $pdo->prepare($sql); $stmt->execute(array( $_POST['id'] )); $row = $stmt->fetch(PDO::FETCH_ASSOC); $membership_category_id = ''; $membership_type = ''; $membership_type_description =''; $is_promo = ''; $is_active = ''; if (!empty($row['membership_category_id'])) { $membership_category_id = $row['membership_category_id']; } if (!empty($_POST['membership_category_id'])) { $membership_category_id = $_POST['membership_category_id']; } //------------------------------------------------------------------------------------- if (!empty($row['membership_type'])) { $membership_type = $row['membership_type']; } if (!empty($_POST['membership_type'])) { $membership_type = $_POST['membership_type']; } //------------------------------------------------------------------------------------- if (!empty($row['membership_type_description'])) { $membership_type_description = $row['membership_type_description']; } if (!empty($_POST['membership_type_description'])) { $membership_type_description = $_POST['membership_type_description']; } //------------------------------------------------------------------------------------- if (!empty($row['is_promo'])) { $is_promo = $row['is_promo']; } if (!empty($_POST['is_promo'])) { $is_promo = $_POST['is_promo']; } //------------------------------------------------------------------------------------- if (!empty($row['is_active'])) { $is_active = $row['is_active']; } if (!empty($_POST['is_active'])) { $is_active = $_POST['is_active']; } //------------------------------------------------------------------------------------- $sql = "SELECT membership_category_id, membership_category FROM membership_category"; $stmt = $pdo->prepare($sql); $stmt->execute(); $options = array(); foreach ($stmt as $row2) { $options[] = [ 'selected' => isset($membership_category_id) && $membership_category_id == $row2['membership_category_id'], 'value' => $row2['membership_category_id'], 'label' => $row2['membership_category'] ]; } $loader = new Twig_Loader_Filesystem('./templates'); $twig = new Twig_Environment($loader, array( 'debug' => true )); $twig->addExtension(new Twig_Extension_Debug()); echo $twig->render( 'form_membership_types.php', [ 'action' => 'Edit', 'form_action' => $_SERVER['SCRIPT_NAME'], 'p' => $_GET['p'], 'id' => $_POST['id'], /*'membership_types' => $row, */ 'options' => $options, 'error' => $error, /*'row' => $row, */ /*'post' => $_POST, */ 'membership_category_id' => $membership_category_id, 'membership_type' => $membership_type, 'membership_type_description' => $membership_type_description, 'is_promo' => $is_promo, 'is_active' => $is_active ]); } catch (Exception $e) { die('ERROR: ' . $e->getMessage()); } ?> form_membership_types.php <pre> {#{ dump() }#} </pre> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> <!-- Latest compiled and minified JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> <form class="form-horizontal" action="{{ form_action }}?p={{ p }}" method="post"> <fieldset> <legend><p>{{ action }} Membership Types</p> </legend> <!-- Select Basic --> <div class="form-group {{ error.membership_category_id ? 'has-error' : '' }}"> <label class="col-md-4 control-label" for="membership_category_id">Membership Category <span style="color: #FF0000;">*</span></label> <div class="col-md-4"> <select id="membership_category_id" name="membership_category_id" class="form-control"> <option value="" style="display:none">Select Category</option> {% for opt in options %} <option value="{{ opt.value }}" {{ opt.selected?'selected="selected"':'' }}>{{ opt.label }}</option> {% endfor %} </select> </div> </div> <!-- Text input--> <div class="form-group {{ error.membership_type ? 'has-error' : '' }}"> <label class="col-md-4 control-label" for="membership_type">Membership Type <span style="color: #FF0000;">*</span></label> <div class="col-md-4"> <input id="membership_type" name="membership_type" type="text" placeholder="Membership Type" class="form-control input-md" value="{{ membership_type ?? '' }}"> </div> </div> <!-- Text input--> <div class="form-group {{ error.membership_type_description ? 'has-error' : '' }}"> <label class="col-md-4 control-label" for="membership_type_description">Membership Type Description <span style="color: #FF0000;">*</span></label> <div class="col-md-4"> <input id="membership_type_description" name="membership_type_description" type="text" placeholder="Membership Type Description" class="form-control input-md" value="{{ membership_type_description ?? '' }}"> </div> </div> <!-- Multiple Checkboxes (inline) --> <div class="form-group"> <label class="col-md-4 control-label" for="checkboxes"></label> <div class="col-md-4"> <label class="checkbox-inline" for="is_promo"> <input type="checkbox" name="is_promo" id="is_promo" value="1" {{ is_promo ? 'checked="checked"' : '' }} > Promotional </label> <label class="checkbox-inline" for="is_active"> <input type="checkbox" name="is_active" id="is_active" value="1" {{ is_active ? 'checked="checked"' : '' }} > Active </label> </div> </div> <div class="form-group"> <div class="col-md-offset-4 col-md-4"> <input type="hidden" name="update"> {% if id is not null %}<input type="hidden" name="id" value="{{ id }}">{% endif %} <input type="submit" name="submit" value="Submit" class="btn btn-primary"> </div> </div> </fieldset> </form>
  18. Never mind. Figured it out. Just needed to pass $membership_type_description to twig
  19. Not sure what you are talking about, but setting a default is not the answer. This single template is used for adding AND editing records. When adding a record, there is nothing to possibly default to. The record does not exist. Only when there is an error such as a missing field will the values of other fields be populated by the post data already entered so you don't have to re-type everything to enter data to the missing field. When editing, the values data initially comes from the DB. You are editing an existing record. On a missing field post error, the field values will be the post values which may or may not be the same as the original row data depending on if you edited the existing data. Its really not complicated when you look at the code. The problem is converting it to twig. Look at the Php setup that works perfectly for adding or editing using a single template. I need EXACTLY as below but in TWIG. if (!empty($row['membership_type_description'])) { $membership_type_description = $row['membership_type_description']; } if (!empty($_POST['membership_type_description'])) { $membership_type_description = $_POST['membership_type_description']; } <!-- Text input--> <div class="form-group <?= !empty($error['membership_type_description'] ) ? 'has-error' : '' ?>"> <label class="col-md-4 control-label" for="membership_type_description">Membership Type Description <span style="color: #FF0000;">*</span></label> <div class="col-md-4"> <input id="membership_type_description" name="membership_type_description" type="text" placeholder="Membership Type Description" class="form-control input-md" value="<?= !empty($membership_type_description) ? htmlspecialchars($membership_type_description, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') : '';?>"> </div> </div>
  20. That works. But in EDIT mode, value should be initially set to the DB value. On post, if there is an error, value should be set by POST. How would I TWIG that? (In ADD mode, value is empty unless there is an error POST) The way it was being handled is like so: if (!empty($row['membership_type_description'])) { $membership_type_description = $row['membership_type_description']; } if (!empty($_POST['membership_type_description'])) { $membership_type_description = $_POST['membership_type_description']; }
  21. Neither one outputs anything. * See the random text just above <!-- Multiple Checkboxes (inline) --> Here is the current files. edit_membership.php <?php echo "<pre>"; print_r($_POST); echo "</pre>"; //---------------------------------------------------------------------------------------- // Update Data //---------------------------------------------------------------------------------------- $error = array(); if (isset($_POST['update'])) { //------------------------------------------------------------------------------------ // Trim Data, Check Missing Fields //------------------------------------------------------------------------------------ include './includes/validate_membership_types.php'; //------------------------------------------------------------------------------------ // Check for errors //------------------------------------------------------------------------------------ if ($error) { //show_form_errors($error); } else { die; $sql = "UPDATE membership_types SET membership_category_id =?, membership_type =?, membership_type_description =?, is_promo =?, is_active =? WHERE membership_type_id=?"; $stmt = $pdo->prepare($sql); $stmt->execute(array( $_POST['membership_category_id'], $_POST['membership_type'], $_POST['membership_type_description'], $_POST['is_promo'], $_POST['is_active'], $_POST['id'] )); die(header("Location: {$_SERVER['SCRIPT_NAME']}?p=list_membership_types&edit")); } // End Else } // End POST //---------------------------------------------------------------------------------------- // Get Update Data //---------------------------------------------------------------------------------------- $hostdb = 'localhost'; $dbname = 'test'; $username = 'root'; $password = ''; $pdo = new PDO("mysql:host=localhost;dbname=$dbname", $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); require_once 'Twig/Autoloader.php'; Twig_Autoloader::register(); $_POST['id'] = 1; // Temp Dev $sql = "SELECT membership_category_id, membership_type, membership_type_description, is_promo, is_active FROM membership_types WHERE membership_type_id=?"; $stmt = $pdo->prepare($sql); $stmt->execute(array( $_POST['id'] )); $row = $stmt->fetch(PDO::FETCH_ASSOC); $sql = "SELECT membership_category_id, membership_category FROM membership_category"; $stmt = $pdo->prepare($sql); $stmt->execute(); $options = array(); foreach ($stmt as $row) { $options[] = [ 'selected' => isset($row['membership_category_id']) && $row['membership_category_id'] == $row['membership_category_id'], 'value' => $row['membership_category_id'], 'label' => $row['membership_category'] ]; } try { $loader = new Twig_Loader_Filesystem('./templates'); $twig = new Twig_Environment($loader, array( 'debug' => true )); $twig->addExtension(new Twig_Extension_Debug()); echo $twig->render( 'form_membership_types.php', [ 'action' => 'Edit', 'form_action' => $_SERVER['SCRIPT_NAME'], 'p' => $_GET['p'], 'id' => $_POST['id'], 'membership_types' => $row, 'options' => $options, 'error' => $error ]); } catch (Exception $e) { die('ERROR: ' . $e->getMessage()); } ?> Template: form_membership_types.php <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> <!-- Latest compiled and minified JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> <form class="form-horizontal" action="{{ form_action }}?p={{ p }}" method="post"> <fieldset> <legend><p>{{ action }} Membership Types</p> </legend> <!-- Select Basic --> <!--<div class="form-group <?= !empty($error['membership_category_id'] ) ? 'has-error' : '' ?>"> <label class="col-md-4 control-label" for="membership_category_id">Membership Category <span style="color: #FF0000;">*</span></label> <div class="col-md-4"> <select id="membership_category_id" name="membership_category_id" class="form-control"> <option value="" style="display:none">Select Category</option> {% for opt in options %} <option value="{{ opt.value }}" {{ opt.selected?'selected="selected"':'' }}>{{ opt.label }}</option> {% endfor %} </select> </div> </div>--> <!-- Text input--> <div class="form-group {{ error.membership_type ? 'has-error' : '' }}"> <label class="col-md-4 control-label" for="membership_type">Membership Type <span style="color: #FF0000;">*</span></label> <div class="col-md-4"> <input id="membership_type" name="membership_type" type="text" placeholder="Membership Type" class="form-control input-md" value="{{ membership_types.membership_type }}"> </div> </div> <!-- Text input--> <div class="form-group {{ error.membership_type_description ? 'has-error' : '' }}"> <label class="col-md-4 control-label" for="membership_type_description">Membership Type Description <span style="color: #FF0000;">*</span></label> <div class="col-md-4"> <input id="membership_type_description" name="membership_type_description" type="text" placeholder="Membership Type Description" class="form-control input-md" value="{% if membership_types.membership_type_description is not null %} {{ membership_types.membership_type_description }} {% endif %}"> </div> </div> {% if membership_type_description is not null %}HEllo{{ membership_type_description }}{% endif %} value="{{ post.membership_type ?? '' }}" <!-- Multiple Checkboxes (inline) --> <div class="form-group"> <label class="col-md-4 control-label" for="checkboxes"></label> <div class="col-md-4"> <label class="checkbox-inline" for="is_promo"> <input type="checkbox" name="is_promo" id="is_promo" value="1" {{ membership_types.is_promo ? 'checked="checked"' : '' }} > Promotional </label> <label class="checkbox-inline" for="is_active"> <input type="checkbox" name="is_active" id="is_active" value="1" {{ membership_types.is_active ? 'checked="checked"' : '' }} > Active </label> </div> </div> <div class="form-group"> <div class="col-md-offset-4 col-md-4"> <input type="hidden" name="update"> {% if id is not null %}<input type="hidden" name="id" value="{{ id }}">{% endif %} <input type="submit" name="submit" value="Submit" class="btn btn-primary"> </div> </div> </fieldset> </form>
  22. Ok, error handling is good to go now. One last problem with that.. On error (i.e: missing field), the other fields should retain the post values so as to not have to re-type the info again on every error. How to do the TWIG equivalent of value="<?= !empty($_POST['membership_type']) ? $_POST['membership_type'] : '';?>"
  23. Thanks @Jaques1. I have almost completed a TWIG conversion for one edit action. I will be able to do all the rest once I get this one done right. I am not sure how transform the server validation part. Here are all the parts as they currently are. The only parts left to convert to TWIG is the validation and the drop down in the template. * Connection info is actually a separate file. Only here for testing. * This same template is used for ADD and EDIT edit_membership_types.php <?php //---------------------------------------------------------------------------------------- // Update Data //---------------------------------------------------------------------------------------- if (isset($_POST['update'])) { //------------------------------------------------------------------------------------ // Trim Data, Check Missing Fields //------------------------------------------------------------------------------------ include './includes/validate_membership_types.php'; //------------------------------------------------------------------------------------ // Check for errors //------------------------------------------------------------------------------------ if ($error) { show_form_errors($error); } else { $sql = "UPDATE membership_types SET membership_category_id =?, membership_type =?, membership_type_description =?, is_promo =?, is_active =? WHERE membership_type_id=?"; $stmt = $pdo->prepare($sql); $stmt->execute(array( $_POST['membership_category_id'], $_POST['membership_type'], $_POST['membership_type_description'], $_POST['is_promo'], $_POST['is_active'], $_POST['id'] )); die(header("Location: {$_SERVER['SCRIPT_NAME']}?p=list_membership_types&edit")); } // End Else } // End POST //---------------------------------------------------------------------------------------- // Get Update Data //---------------------------------------------------------------------------------------- $hostdb = 'localhost'; $dbname = 'test'; $username = 'root'; $password = ''; $pdo = new PDO("mysql:host=localhost;dbname=$dbname", $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); require_once 'Twig/Autoloader.php'; Twig_Autoloader::register(); $_POST['id'] = 1;// Temp Dev $sql = "SELECT membership_category_id, membership_type, membership_type_description, is_promo, is_active FROM membership_types WHERE membership_type_id=?"; $stmt = $pdo->prepare($sql); $stmt->execute(array( $_POST['id'] )); $row = $stmt->fetch(PDO::FETCH_ASSOC); try { $loader = new Twig_Loader_Filesystem('./templates'); $twig = new Twig_Environment($loader, array('debug' => true)); $twig->addExtension(new Twig_Extension_Debug()); echo $twig->render('form_membership_types.php', ['action' => 'Edit', 'form_action' => $_SERVER['SCRIPT_NAME'], 'p' => $_GET['p'], 'id' => $_POST['id'], 'membership_types' => $row]); } catch (Exception $e) { die('ERROR: ' . $e->getMessage()); } ?> Template form_membership_types.php <form class="form-horizontal" action="{{ form_action }}?p={{ p }}" method="post"> <fieldset> <legend><p>{{ action }} Membership Types</p> </legend> <!-- Select Basic --> <div class="form-group <?= !empty($error['membership_category_id'] ) ? 'has-error' : '' ?>"> <label class="col-md-4 control-label" for="membership_category_id">Membership Category <span style="color: #FF0000;">*</span></label> <div class="col-md-4"> <select id="membership_category_id" name="membership_category_id" class="form-control"> <option value="" style="display:none">Select Category</option> <?php $sql = "SELECT membership_category_id, membership_category FROM membership_category"; $stmt = $pdo->prepare($sql); $stmt->execute(); $result = $stmt->fetchAll(); foreach ($result as $row) { $selected = ''; if ((isset($membership_category_id)) && $membership_category_id == $row['membership_category_id']) { $selected = "selected=\"selected\""; } echo "<option value=\"{$row['membership_category_id']}\" $selected>{$row['membership_category']}</option>\n"; } ?> </select> </div> </div> <!-- Text input--> <div class="form-group <?= !empty($error['membership_type'] ) ? 'has-error' : '' ?>"> <label class="col-md-4 control-label" for="membership_type">Membership Type <span style="color: #FF0000;">*</span></label> <div class="col-md-4"> <input id="membership_type" name="membership_type" type="text" placeholder="Membership Type" class="form-control input-md" value="{{ membership_types.membership_type }}"> </div> </div> <!-- Text input--> <div class="form-group <?= !empty($error['membership_type_description'] ) ? 'has-error' : '' ?>"> <label class="col-md-4 control-label" for="membership_type_description">Membership Type Description <span style="color: #FF0000;">*</span></label> <div class="col-md-4"> <input id="membership_type_description" name="membership_type_description" type="text" placeholder="Membership Type Description" class="form-control input-md" value="{{ membership_types.membership_type_description }}"> </div> </div> <!-- Multiple Checkboxes (inline) --> <div class="form-group"> <label class="col-md-4 control-label" for="checkboxes"></label> <div class="col-md-4"> <label class="checkbox-inline" for="is_promo"> <input type="checkbox" name="is_promo" id="is_promo" value="1" {{ membership_types.is_promo ? 'checked="checked"' : '' }} > Promotional </label> <label class="checkbox-inline" for="is_active"> <input type="checkbox" name="is_active" id="is_active" value="1" {{ membership_types.is_active ? 'checked="checked"' : '' }} > Active </label> </div> </div> <div class="form-group"> <div class="col-md-offset-4 col-md-4"> <input type="hidden" name="update"> {% if id is not null %}<input type="hidden" name="id" value="{{ id }}">{% endif %} <input type="submit" name="submit" value="Submit" class="btn btn-primary"> </div> </div> </fieldset> </form> validate_membership_types.php - The part I am currently at. //---------------------------------------------------------------------------------------- // Trim $_POST Array //---------------------------------------------------------------------------------------- $_POST = array_map('trim', $_POST); //---------------------------------------------------------------------------------------- // Validate Form Input //---------------------------------------------------------------------------------------- $error = array(); if (empty($_POST['membership_category_id'])) { $error['membership_category_id'] = 'Membership Category Id Required.'; } if (empty($_POST['membership_type'])) { $error['membership_type'] = 'Membership Type Required.'; } if (empty($_POST['membership_type_description'])) { $error['membership_type_description'] = 'Membership Type Description Required.'; }
  24. Problem with TWIG ternary with TWIG value {{ id }} # This works, output is record id 1 {{ id ? '{{ id }}' : '' }} # This just outputs {{ id }} {{ id ? 'Some text' : '' }} # This Works, output Some text I want the TWIG ternary of if isset id, output id else nothing. The actual line I am attempting {{ id ? '<input type="hidden" name="id" value="{{ id }}">' : '' }}
×
×
  • 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.