![]() |
Two things: HTML entities and Undefined variables - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Two things: HTML entities and Undefined variables (/showthread.php?tid=37951) |
Two things: HTML entities and Undefined variables - El Forum - 01-25-2011 [eluser]edjon2000[/eluser] Hello all, Sorry to pick your brains yet again ![]() 1. HTML Entities: What is the best way to deal with HTML entities, I am currently designing all my views using the XHTML Strict doctype as follows: Code: <?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?> Code: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> First of all I have tried adding htmlspecialchars to my form validation rules which works great, up to a point, however if I then update an existing entry, I run into the double encoding problem, e.g. Code: Finance & Accounting Code: Finance && Accounting I tried using htmlspecialchars_decode(PHP 5.x.x) and that removes the appearance of Code: & At the moment, in my database I have data stored with HTML entities, so, should I store the records without encoding? and perhaps encode them on import to the view, and then decode them on update. Any advice on this would be greatly appreciated as I am sure you have come across this in the past (I did a search prior to posting this but found nothing directly relevant) and what partly related solutions I did find seemed to produce more errors, which brings me to my second question. 2. Undefined Variables Now this is a weird one, I have found a lot of interesting stuff on these forums but nothing that actually answered my question, which is:- If I am passing a variable from my controller to my associated view how can I do that in such a way as to prevent an undefined variable problem. To display my views I use a sort of pseudo_template idea as follows:- I have a common header and footer and a typical load from my controller is like this Code: <?php Now that all seems fine, but when it comes to building the view I get loads of undefined variable warnings in my IDE and I am one of those people that prefers to sort out the problem rather than cover it up, so any PHP notice error I have to correct. To use the above example, here is the associated view, I will have to post this in my next post Two things: HTML entities and Undefined variables - El Forum - 01-25-2011 [eluser]edjon2000[/eluser] Sorry about this here is the view. Header: Code: <?php Code: <div id="left-column"> Code: </div> Two things: HTML entities and Undefined variables - El Forum - 01-25-2011 [eluser]Fabdrol[/eluser] I'm not sure if I get your problem, but if I understant correctly it's the following: 1. User creates an entry with a title, body, maybe more 2. To be complient entities must be encoded when viewed in the browser Data in form entries doesn't need to be encoded to be complient with your doctype. So, what I would do is save the data 'raw' in the database (e.g., without encoding the entities). That way, you can always be sure that the entry is the way the author intended it. Then, when somebody views the data, you encode it in the view (or in route to the view). Example: Code: <h1><?= htmlspecialchars($title) ?></h1> When a user edits data, you don't encode it, but put it in the edit form in it's "raw" format. That way, your data remains true and the encoding should be no problem. Two things: HTML entities and Undefined variables - El Forum - 01-25-2011 [eluser]edjon2000[/eluser] Now, when it comes to variables on the view page things like Code: $data['vac_name'] Code: $vac_name I have read that all variables should be initialized but, how can you do that with a dynamic variable that is derived from the controller, if I define Code: $vac_name = ''; Any advice on this would also be greatly appreciated. Jon Two things: HTML entities and Undefined variables - El Forum - 01-25-2011 [eluser]edjon2000[/eluser] Sorry Fabdrol I was still composing the post which ended up as several posts, please could you reply again after looking at the additional information but thanks anyway I will certainly have a look at that (raw data in database and do the encoding on import) which would suggest decoding on export. Hmm sounds like a plan ![]() Jon Two things: HTML entities and Undefined variables - El Forum - 01-25-2011 [eluser]Fabdrol[/eluser] Well, if the data is not encoded in the DB, you don't need to decode it on export. You'll only encode it when viewing it in a view. In any other case, you don't do anything with the encoding.. I'll take a look at the rest of your post in a moment. Maybe you could summarize your specific problems, it's a bit confusing decoding it between all the code samples ;-) Fabian Two things: HTML entities and Undefined variables - El Forum - 01-25-2011 [eluser]edjon2000[/eluser] Actually, thinking about the double encoding problem since PHP 5.2.3 they added the double_encode parameter which is as follows:- Quote:When double_encode is turned off PHP will not encode existing html entities, the default is to convert everything.Perhaps that may solve the problem, it is difficult, my development setup uses PHP 5.3.3 running on ZendServerCE but my WebHost uses PHP 5.2.14, hmm this could be a problem. Jon Two things: HTML entities and Undefined variables - El Forum - 01-25-2011 [eluser]edjon2000[/eluser] Hi Fabian, thanks for sticking with me. Quote:I’ll take a look at the rest of your post in a moment. Maybe you could summarize your specific problems, it’s a bit confusing decoding it between all the code samplesIn essence, at the moment, information is stored in the database in encoded format i.e Code: & is stored as & Now as this project is brand new, and I have built it from the ground up, I have the freedom to play about with the database and change it for best practices although I am running out of time, My client would like to launch this soon, preferably in the next week or so, but he would prefer that it be "right" so to speak. Jon Two things: HTML entities and Undefined variables - El Forum - 01-25-2011 [eluser]edjon2000[/eluser] Fabian thanks, I do have a dev version of the site available at the following location please feel free to have a look at it. |