Jump to content

when php, mysql, html intersect


niche

Recommended Posts

Yesterday morning, I would've said I knew how php, mysql, html work together. Yesterday afternoon, it was obvious that I didn't have a clue or at least didn't know enough probably because I started with an assumption that all I had to do was get the html/css containing php variables in a mysql table, SELECT it, and place it as needed.Though I worked through my problem successfully (with indispensable help from the forum), I learned that storing HTML in a mysql table is odd to a lot of people.Why is that?What's the preferred approach?Please use use this script as a starting point for a reply if possible:

<p style="font-size:9px;margin:0px;"> <span style="background-color:yellow">Expires [color="#FF0000"]VARIABLE DATE GOES HERE[/color]. </span></p>

Link to comment
Share on other sites

Context.If you're storing user-styled user comments to a blog article (for example) then of course you'd want to put a big chunk of HTML in your database. How else are you going to do it?I suspect most of us work with CMS's that automate that kind of thing when we need it, so we never do that kind of programming ourselves.So without more context, the reaction may have been puzzlement. Our normal assumption is that you'd want to separate data from presentation, not realizing in this case that the presentation was in fact part of the data.

Link to comment
Share on other sites

I think the context would depend on how dynamic a site needs to be and how much variety a mind can hold (and quickly display) . The more dynamic a site and the more variety it has the more html/css needs to be stored in tables. Make sense?

Link to comment
Share on other sites

There's nothing inherently wrong about storing HTML fragments in a database. I've seen CMS applications that store HTML fragments in a database. If you are only ever updating things in a CMS, then it's not necessarily a bad idea to store everything in a database, but that means you have to always use the CMS to change anything.For the sites that I've been setting up lately, and I do not want to imply that my approach is the "correct" approach, I've been using templates. A few years ago I built my own template engine to parse my template files, so I use that for my HTML content for sites. A template is more or less a normal HTML file, it could be a complete file or it could be a fragment that gets included in another template, but it contains additional template tags that have meaning for the template engine. So I can write an HTML file that has if statements, or a loop instead of a table, or whatever else, but the HTML template file is separate from the PHP file that gathers all of the data and loads the template. I built my own template engine to do what I wanted it to do, but a popular open source template engine is Smarty if you want to research that and look into HTML templates.The point I'm trying to make is that there isn't one correct answer for every situation. If you want to store your stuff in a database, go nuts. Maybe in a year or two you'll find a situation where that approach doesn't work so well, and you'll adapt it. Maybe you'll go for an approach with templates, maybe not. Maybe you'll think up an entirely new way that no one has considered or formalized. The thing is to use what makes sense to you, and the longer you use it the more experience you'll gain, you'll learn what works and what doesn't, etc. Just feel free to experiment, if you don't understand through research why people dislike a certain approach, feel free to go for that approach and make it work like you think it should work, and see if it succeeds. I understand you're asking for suggestions from people more experienced than yourself, but I want to stress that self experimentation goes a long way to helping you either understand why a certain thing doesn't work (and also why others do), and also to make breakthroughs that other people haven't considered.If you're thinking of using the sprintf approach with your database strings, study examples 1 through 4 on the manual page, they show how it's possible to specify your values to substitute in one order, but have them appear in a different order in your string. If you use that syntax, you can change or internationalize your strings without changing your PHP code.http://www.php.net/manual/en/function.sprintf.php

Link to comment
Share on other sites

##CUT##For the sites that I've been setting up lately, and I do not want to imply that my approach is the "correct" approach,
i don't think you need to downplay your tons of experience, certainly to beginners like niche & myself, we can benefit from your many man-hours* spent, by "just following" the methods suggested (for now) - i doubt we're (yet) able to come up with a more efficient solution in our own way.* = am assuming it's directly related to your post count here on this forum. :)
I've been using templates. A few years ago I built my own template engine to parse my template files, so I use that for my HTML content for sites.
makes sense to me, i certainly subscribe to the Separation of Elements, Presentation & Behaviour.infact, one might further split "Elements" to Structure & Content - the DOM being the Structure, and Content being the pure text. i'd still go with <b>-s & <i>-s if one is permitted to bring up that old hoary chestnut again... :)
A template is more or less a normal HTML file, it could be a complete file or it could be a fragment that gets included in another template, but it contains additional template tags that have meaning for the template engine. So I can write an HTML file that has if statements, or a loop instead of a table, or whatever else, but the HTML template file is separate from the PHP file that gathers all of the data and loads the template. I built my own template engine to do what I wanted it to do, but a popular open source template engine is Smarty if you want to research that and look into HTML templates.##CUT##
would that be where methods like element.cloneNode(true) comes in ?
Link to comment
Share on other sites

It's not a Javascript template, it's HTML code plus extra stuff that gets processed by PHP. For example, here's a template for a login page:

{*type=file property-header.tpl}<ul class="breadcrumbs">  {*type=dataset_start breadcrumb}  <li><a href="{*type=dataset_item url}">{*type=dataset_item title}</a></li>  {*type=dataset_end}</ul><div class="property-container">  <div class="property-name">{*name}</div>  <hr>  <div class="property-address-info">    {*address} {*city}, {*state} {*zip}<br>    {*phone} {*type=if_start fax}| {*fax}{*type=if_end}  </div>  <br><br>    <div class="property-images">    <div style="text-align: center;">      <ul class="listing-nav">        <li><a href="#request-info-link" onclick="show_element('request-info', 275, true);">Check Availability</a></li>        <li><a href="show-map.php?id={*id}" onclick="window.open(this.href, '_blank', 'width=600,height=600,resizable=yes'); return false;">View Map</a></li>        <li><a href="java script:window.print();">Print</a></li>      </ul>    </div>    <br>    {*type=if_start num_images value=0}      <!-- no images -->    {*type=if_else}      {*type=if_start num_images value=1}    <img src="{*image}" alt="{*name}">      {*type=if_else}    <div id="myGallery">      {*type=dataset_start images}      <div class="imageElement">        <h3>{*type=dataset_item name}</h3><!-- title -->        <p></p><!-- description -->        <a href="{*type=dataset_item image}" target="_blank" title="open image" class="open"></a>        <img src="{*type=dataset_item image}" class="full">        <img src="{*type=dataset_item thumb}" class="thumbnail">      </div>      {*type=dataset_end}    </div>      {*type=if_end}    <div style="text-align: center;">      <ul class="listing-nav">        <li><a href="#request-info-link" onclick="show_element('request-info', 275, true);">Check Availability</a></li>        <li><a href="show-map.php?id={*id}" onclick="window.open(this.href, '_blank', 'width=600,height=600,resizable=yes'); return false;">View Map</a></li>        <li><a href="java script:window.print();">Print</a></li>      </ul>    </div>    <br>    {*type=if_end}  </div>  {*type=if_start description}  <h3>Description</h3>  <div id="property-description">{*description}</div>  {*type=if_end}  {*type=if_start features}  <h3>Features</h3>  <div id="property-features">{*features}</div>  {*type=if_end}  {*type=if_start num_floorplans value=0}    <!-- no floorplans -->  {*type=if_else}  <h3>Floorplans</h3>  <div class="property-floorplans-container">    <div id="property-floorplans-container-div">    <!-- <table id="property-floorplans">      <tbody><tr> -->      {*type=dataset_start floorplans}      <div class="floorplan-container">        <a href="{*type=dataset_item image}" target="_blank"><img src="{*type=dataset_item thumb}" class="thumbnail"></a>        <br>        <b>{*type=dataset_item name}</b><br>        {*type=dataset_item description}      </div>      {*type=dataset_end}      <!-- </tr></tbody>    </table> -->    </div>  </div>  <script type="text/javascript">  var el = document.getElementById('property-floorplans-container-div');  el.style.width = ({*num_floorplans} * 230) + 'px';  </script>  {*type=if_end}  {*type=if_start specials}  <h3>Specials</h3>  <div id="property-specials">{*specials}</div>  {*type=if_end}    {*type=if_start directions}  <h3>Directions</h3>  <div id="property-directions">{*directions}</div>  {*type=if_end}  {*type=if_start office_hours}  <h3>Office Hours</h3>  <div id="property-hours">{*office_hours}</div>  {*type=if_end}  <h3><a id="request-info-link" href="java script:void(0);" onclick="show_element('request-info', 275);">Check Availability</a></h3>  <div id="request-info">    <form method="post" action="properties.php" id="request-info-form">      <input type="hidden" name="page_mode" value="request-info">      <input type="hidden" name="id" value="{*id}">      <div class="form_left">First Name</div>      <div class="form_right">{*type=textfield contact_fname}</div>      <div class="form_left">Last Name</div>      <div class="form_right">{*type=textfield contact_lname}</div>      <div class="form_left">Phone</div>      <div class="form_right">{*type=textfield contact_phone}</div>      <div class="form_left">Email</div>      <div class="form_right">{*type=textfield contact_email}</div>      <div class="form_left">Move Date</div>      <div class="form_right">{*type=textfield contact_date id=datepicker_input}</div>      <div class="form_left">Questions/<br>Comments</div>      <div class="form_right">{*type=textbox real_comments style=width:90%;height:60px;}</div>      <textarea name="comments" id="hide_comments"></textarea>      <script type="text/javascript">document.getElementById('hide_comments').style.display = "none";</script>      <div class="form_right"><input type="submit" value="Submit"></div>      <div id="request-info-feedback"></div>    </form>  </div>  <h3>Map</h3>  <div id="map-container">    <div id="map">Loading Map...</div>    <a href="show-map.php?id={*id}" onclick="window.open(this.href, '_blank', 'width=600,height=600,resizable=yes'); return false;">View Larger</a> |     <a href="{*directions_link}" target="_blank">Directions</a>  </div>  {*logo}</div><script type="text/javascript">function setup() {  var gallery_el = $('myGallery');  if (gallery_el != undefined)  {    var myGallery = new gallery($('myGallery'), {      timed: true,      delay: 10000,      thumbHeight: 90,      thumbWidth: 120,      defaultTransition: "fadeslideleft",      textShowCarousel: 'All Pictures',      showInfopane: false    });  }  $('request-info').setStyle('height', 275);  $('request-info-form').addEvent('submit', function(e) {    e.stop();    this.set('send', {onComplete: function(response) {      var retval = JSON.decode(response);      if (!retval.success)      {        alert(retval.message);      }      else      {        alert('Thank you, your message has been sent.');        $('request-info-form').reset();        show_element('request-info', 0);      }    }});    this.send();  });    var dp = new DatePicker([$('datepicker_input')], {    pickerClass: 'datepicker_vista',    minDate: '<?php echo date('m-d-Y');?>',    timePicker: false  });  var geocoder = new google.maps.Geocoder();  geocoder.geocode( { 'address': '{*map_address}'}, function(results, status) {    if (status == google.maps.GeocoderStatus.OK) {      var opts = {        center: results[0].geometry.location,        mapTypeId: google.maps.MapTypeId.HYBRID,        zoom: 16,        sensor: false      };      var map = new google.maps.Map(document.getElementById("map"), opts);        var marker = new google.maps.Marker({        map: map,        position: results[0].geometry.location,        title: '{*map_address}',        visible: true      });    } else {      alert("Map could not be shown for the following reason: " + status);    }  });}window.addEvent('domready', setup);function show_element(elId, height, force){  var el = $(elId);  if (el.style.height != '0px' && force !== true)    height = 0; // toggle close  var myElementsEffects = new Fx.Elements([el]);  myElementsEffects.start({    '0': {      'height': height    }  });}</script>{*type=file footer.tpl}

The point is that PHP gathers the content and passes it to the template, and the template figures out where to put everything. The PHP file which feeds that template doesn't have anything to do with where things go, it's only concerned with gathering the data and sending it to the template.

Link to comment
Share on other sites

i don't think you need to downplay your tons of experience
I appreciate the vote of confidence, but just so you know, I'm in no way sure that my ways are better than anyone else's. There are specific problems I've looked for specific solutions to, but a lot of programmers are successful at similar things using very different approaches.A wise man once said "you don't know anything until you know that you know nothing."
Link to comment
Share on other sites

It's not a Javascript template, it's HTML code plus extra stuff that gets processed by PHP. For example, here's a template for a login page:
{*type=file header.tpl}##CUT##what are those 'things' ?do 'curlies' have some meaning in HTML, or are those tags to be parsed by PHP ?custom-made delimiters ??
I appreciate the vote of confidence, but just so you know, I'm in no way sure that [b]my ways are better than [u]anyone else's.[/u] [/b]There are specific problems I've looked for specific solutions to, but a lot of programmers are successful at similar things using very different approaches.A wise man once said "you don't know anything until you know that you know nothing."
that's a good quote, i agree that one must [u]always[/u] be learning.and i didn't say better than "anyone else's" - just (obviously) better than us beginners ! :)we [b]may[/b], somewhere down the line, figure out (or just stumble onto !!) something that you never thought of, but given our (beginners) current skill set, that's highly unlikely it will happen in the near future.i'm really impressed with the openness that mods/admins here are willing to share their knowledge and expertise in guiding absolute newbies on their path to being developers of their own.i guess it's similar to "happiness shared is happniess doubled, sorrow shared is sorrow halved" - and can thus be applied to be; "knowledge shared is (human) knowledge compounded".
Link to comment
Share on other sites

custom-made delimiters ??
Right, the template engine I made looks for those and replaces them with some HTML content. For the type=file, it just includes another template and parses that new template the same way. There are tags for replacing form fields, the if statements and dataset loops, and just a normal tag like this:{*name} gets replaced with a value sent from PHP, like a variable. So yeah, it's custom, but templates that things like Smarty or other engines use have some sort of similar syntax to tell the template engine to do some work on the template.On the PHP side, the code is very minimal and may look something like this for the login template:
<?phpinclude 'functions.php';include 'class.page.php';$page_content = array();$page_content['page_title'] = 'Log In';$page_content['errors'] = '';$page_content['username'] = form_var('username');if ($page_content['username'] != ''){  // process login info}$page = new Page();$page->set_template('login.tpl');$page->process($page_content);$page->output();?>

The thing to notice is that there is nothing in the PHP file about how anything appears, just getting the data and sending it to the template. The templates are allowed to have PHP code in them for more advanced things, but typically the template doesn't contain any PHP, just if statements or loops to display the content however it should show up.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...