Jump to content

benanamen

Members
  • Posts

    2,134
  • Joined

  • Last visited

  • Days Won

    42

Everything posted by benanamen

  1. 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>
  2. 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'] : '';?>"
  3. 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.'; }
  4. 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 }}">' : '' }}
  5. Thanks. In some of my existing Php templates there is one or more dynamic dropdowns that get their data from the DB. What would be the proper setup for those dropdowns? The query for the dropdown currently resides in the Php template as so.... What is the proper way to TWIG this? ($person_id is the POST value.) This would be a second query for the template page. <div class="form-group <?= !empty($error['person_id'] ) ? 'has-error' : '' ?>"> <label class="col-md-4 control-label" for="person_id">People</label> <div class="col-md-4"> <select id="person_id" name="person_id" class="form-control"> <option value="" style="display:none">Select Person</option> <?php $sql = "SELECT person_id, first_name FROM people"; $stmt = $pdo->prepare($sql); $stmt->execute(); $result = $stmt->fetchAll(); foreach ($result as $row) { $selected = ''; if ((isset($person_id)) && $person_id == $row['person_id']) { $selected = "selected=\"selected\""; } echo "<option value=\"{$row['person_id']}\" $selected>{$row['first_name']}</option>\n"; } ?> </select> </div> </div>
  6. benanamen

    TWIG

    Trying to learn TWIG. I can do a basic template but having a problem with Mysql results. {{ dump() }} displays the mysql array but no data shows in the TWIG for loop. No error, just nothing. Expected result is person_id and first_name rows from Mysql. Dump Array seems awfully big for two rows. I suspect the problem is here. Cant find any info on the net for TWIG and Mysql. (Unbelievably) What I have tried: <?php $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); $sql = "SELECT person_id, first_name FROM people"; $stmt = $pdo->prepare($sql); $stmt->execute(); $result = $stmt->fetchAll(); require_once 'Twig/Autoloader.php'; Twig_Autoloader::register(); try { $loader = new Twig_Loader_Filesystem('./templates'); $twig = new Twig_Environment($loader, array( 'debug' => true )); $twig->addExtension(new Twig_Extension_Debug()); echo $twig->render('people.twig', $result); } catch (Exception $e) { die('ERROR: ' . $e->getMessage()); } ?> people.twig <pre> {{ dump() }} </pre> <ul> {% for row in result %} <li>{{ row.person_id }} {{ row.first_name }}</li> {% endfor %} </ul> Results array(2) { [0]=> array(4) { ["person_id"]=> string(1) "1" [0]=> string(1) "1" ["first_name"]=> string(3) "Bob" [1]=> string(3) "Bob" } [1]=> array(4) { ["person_id"]=> string(1) "2" [0]=> string(1) "2" ["first_name"]=> string(3) "Sam" [1]=> string(3) "Sam" } }
  7. Forget the problem you think you need to solve. What exactly do you have going on overall? Your DB column names and data give no indication what is going on there. You say you have two forms with "different views" whatever that is. No one is going to be able to help you based on the non information you have provided.
  8. I have no idea what you're saying, but I am pretty sure everything you are doing is wrong. Anytime I have seen an OP/Noob using UNION their database is wrong. I am also confident this is an XY Problem. http://xyproblem.info/ The XY problem is asking about your attempted solution rather than your actual problem. This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.
  9. I wont get into the mess that is your code. You are using obsolete code that has been completely removed from Php. You need to use PDO. https://phpdelusions.net/pdo
  10. Thank @Jaques1, Outside the root is perfect. Just didn't know how twig is generally handled. I guess a template is a template in regards to where to store it. My editor (Webuilder) also supports .twig plus it clearly identifies the file is not only a template, but a twig template. One other thing, does auto escaping default to on? Couldn't quite grasp it from the docs.
  11. LOL! Move the opening <p> before the include. And no need to echo | week. That is less than basic Html.
  12. After reading many comments from @Jaques1 on using Twig and especially the quoted post by him I decided to learn Twig which is actually very easy to use. My question is, since anyone can view the template/twig source, is there any security issues with that? Also, what is the proper file extension for a twig template? I have seen .twig, .htm, .html. .phtml, .tpl and more.
  13. Per the manual: In PHP 5.6 onwards, "UTF-8" is the default value and its value is used as the default character encoding for htmlentities(), html_entity_decode() and htmlspecialchars() So isn't this unnecessary for output in >=5.6? htmlspecialchars($some_var, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8')
  14. It's a waste of time to even get into it. It is VERY bad code. Toss it and don't ever post it again. You might as well ask everyone to hack you. And that's just for starters.
  15. set_exception_handler is your friend here. @Jaques1 schooled me on this one. http://php.net/manual/en/function.set-exception-handler.php
  16. GoDaddy shared hosting is the worst. I double my rate when I have to work on those accounts.
  17. You may need to use LOAD DATA LOCAL INFILE http://dev.mysql.com/doc/refman/5.7/en/load-data.html
  18. @jaques1, just how many times and places do you actually need to set the character encoding? So you have header('Content-Type: text/html;charset=utf-8'); <meta charset="utf-8"> html_escape($test_input, APP_HTML_ENCODING) $dsn = "mysql:host=$dbhost;dbname=$dbname;charset=$charset"; And also have mentioned the server can set the character encoding. Seems redundant to have to explicitly set it in so many places. Not to mention Mysql as well, but I assume that is different.
  19. Your problem is your query. You need to join the tables. That is the whole reason it is so slow. The WHERE clause is killing it. This kind of thing should be done in a JOIN WHERE building_products.id = product_id Also, name is a reserved word. Change it to something like color_name. Unless height, width, length, and quantity are something other than numbers, you shouldn't be using varchar for the column type.
  20. Pictures are for hanging on the wall. Post your actual code. With that said, you are using obsolete code that has been completely removed from Php. You need to use PDO. https://phpdelusions.net/pdo
  21. Where can this Portfolio be viewed?
  22. Solidly makes the case. I personally have not done anything multi-lingual which is why I think in 100% sql and I am always the sole programmer. One point that does make Barand's answer is if you're querying from the console to get the same result. Valid point.
×
×
  • 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.