Jump to content

NotionCommotion

Members
  • Posts

    2,446
  • Joined

  • Last visited

  • Days Won

    10

Everything posted by NotionCommotion

  1. I would like to store the following string in a database, and have the client download it and use it to configure a Highchart chart. Ideally, I would like to download it as JSON, however, the 22nd line color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black' is not data but script and JSON is meant only for data. So I could only think of two options: Keep it as JSON but come up with some sort of encoding strategy where the script is turned into data, and then parse it client side. Use it generate JavaScript on the initial page load. Is one option better than the other? Are there other options? How would you recommend implementing? Thank you { "chart": { "plotBackgroundColor": null, "plotBorderWidth": null, "plotShadow": false, "type": "pie" }, "title": { "text": "Browser market shares January, 2015 to May, 2015" }, "tooltip": { "pointFormat": "{series.name}: <b>{point.percentage:.1f}%</b>" }, "plotOptions": { "pie": { "allowPointSelect": true, "cursor": "pointer", "dataLabels": { "enabled": true, "format": "<b>{point.name}</b>: {point.percentage:.1f} %", "style": { "color": color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black' } } } }, "series": [ { "name": "Brands", "colorByPoint": true, "data": [ { "name": "Microsoft Internet Explorer", "y": 56.33 }, { "name": "Chrome", "y": 24.03, "sliced": true, "selected": true }, { "name": "Firefox", "y": 10.38 }, { "name": "Safari", "y": 4.77 }, { "name": "Opera", "y": 0.91 }, { "name": "Proprietary or Undetectable", "y": 0.2 } ] } ] }
  2. Thanks Josh, Not today, but maybe one day, and if so, I will parse the text and duplicate the appropriate data in its own column. I didn't originally say so, but the data (i.e. {"name": "Microsoft Internet Explorer","y": 56.33}) will not be stored in the file as it comes from a different source. Instead of hard coding a value of 56.33, maybe it will store the primary key of a record? But then if that record got deleted, I wouldn't have any foreign key constraint. Instead, maybe I should exclude this data from the JSON and store it in a database? Or maybe keep it both places; the JSON for the legend name and order (and any other Highchart features which are only display oriented), and the database for foreign key constraints only?
  3. I am building a Highcharts configuration tool where the resultant options object is stored as JSON in a text database column (I will not be supporting functions in the options object, so JSON will work). I will have a page which will display various properties of a given chart such as the type of chart, its title, the legend names, etc. I am thinking of not storing any of the properties in a separate column of the table, but just using json_decode() to get the values. "If" I need to create a list of all Pie charts, Bar charts, etc, I will store the chart type as a separate column and duplicate the data. Any concerns? Thanks { "chart": { "plotBackgroundColor": null, "plotBorderWidth": null, "plotShadow": false, "type": "pie" }, "title": { "text": "Browser market shares January, 2015 to May, 2015" }, "tooltip": { "pointFormat": "{series.name}: <b>{point.percentage:.1f}%</b>" }, "plotOptions": { "pie": { "allowPointSelect": true, "cursor": "pointer", "dataLabels": { "enabled": true, "format": "<b>{point.name}</b>: {point.percentage:.1f} %", "style": { "color": "black" } } } }, "series": [ { "name": "Brands", "colorByPoint": true, "data": [ { "name": "Microsoft Internet Explorer", "y": 56.33 }, { "name": "Chrome", "y": 24.03, "sliced": true, "selected": true }, { "name": "Firefox", "y": 10.38 }, { "name": "Safari", "y": 4.77 }, { "name": "Opera", "y": 0.91 }, { "name": "Proprietary or Undetectable", "y": 0.2 } ] } ] }
  4. Create a file called index.php. In it, include something like: $task=empty($_GET['task'])?'display':$_GET['task']; switch($task) { case 'display': /* Get data from your database. Pass the data to your view. Use a proper template system for your view such as http://twig.sensiolabs.org/ to render a template. Your template should include links to your JS files. */ break; case 'update_something': //do something break; default: //Do the same as display
  5. Thanks Kicken, Yes, I knew that, just didn't say that! Makes perfect sense (now). Thank you!
  6. Thanks Kicken, Was rushing out the door, and apologize for my cryptic post. Yes, I was kind of asking whether DOM manipulation is similar to JavaScript. For instance, would the following create <div><span>foo</span><span>bar</span></div> or <div><span>bar</span><span>bar</span></div>? Based on your response, I would expect the later. $div = $doc->createElement('div'); $span = $doc->createElement('span'); $span ->value='foo'; $div ->appendChild($span ); $span ->value='bar'; $div ->appendChild($span );
  7. I am sure my use of the word "persistent" is wrong. Let me explain. Consider the following code. If I create a single element and then append it multiple times to another element, will each time it is appended be its own unique object, or will they all be the same object? Thanks $doc = new DOMDocument(); $doc->loadHTML($html); $xpath = new DOMXPath($doc); $widget1s=[]; foreach( $xpath->query( "//img[contains(concat(' ', normalize-space(@class), ' '), ' widget1 ')]") as $img) { if( $id=$img->getAttribute('data-id')) { if(!in_array($id,$widget1s)) { $widget1s[]=$id; } $elem = $doc->createElement('span'); $class=$doc->createAttribute('class'); $class->value='widget1 w1'.$id; $elem->appendChild($class); $elem_value = $doc->createElement('span'); $class=$doc->createAttribute('class'); $class->value='value'; $elem_value->appendChild($class); $elem->appendChild($elem_value); $elem_units = $doc->createElement('span'); $class=$doc->createAttribute('class'); $class->value='units'; $elem_units->appendChild($class); $elem->appendChild($elem_units); $data_id=$doc->createAttribute('data-id'); $data_id->value=$id; $elem->appendChild($data_id); $img->parentNode->replaceChild($elem,$img); } } $widgets=[]; foreach( $xpath->query( "//img[contains(concat(' ', normalize-space(@class), ' '), ' widget2 ')]") as $img) { if( $id=$img->getAttribute('data-id')) { if(!in_array($id,$widget2s)) { $widget2s[]=$id; } $elem = $doc->createElement('div'); $class=$doc->createAttribute('class'); $class->value='widget2 w2'.$id; $elem->appendChild($class); $data_id=$doc->createAttribute('data-id'); $data_id->value=$id; $elem->appendChild($data_id); $img->parentNode->replaceChild($elem,$img); } }
  8. Hi Kicken, I agree it is simpler just deleting everything and then adding the existing plus new records. And I am happy to do so unless there are reasons why I shouldn't. My application is not unique, and if it is generally a good idea to do so, then I should also do so. Guess my concern was the microsecond of extra database processing, but it probably will be offset by the extra SELECT query and PHP logic processing of other solutions. My only other concern is some sort of quantum race condition which will never happen but keep me up at night. Thanks
  9. Thanks Barand, Where can I read up on the expression context? Specifically the first part such as "*" and "//p"?
  10. I too need to better understand the query syntax. I believe [@id=yourTagIdHere] is fine. I've only currently used the class attribute, and used something like '//p[@class=yourClassHere]'. Maybe you don't need the p since the id should uniquely identify the element type? I believe // will search from the current node and children, but not sure about */.
  11. ??? <img itemprop="image" title="$page_image_title1" alt="$page_image_alt1" src="http://example.com/images/myhouse.jpg"> <img itemprop="image" title="$page_image_title1" alt="$page_image_alt2" src="http://example.com/images/myhouse.jpg"> <img itemprop="image" title="$page_image_title3" alt="$page_image_alt3" src="http://example.com/images/myhouse.jpg">
  12. My first script would delete the records even if they shouldn't be deleted, but then would insert them back. The following (untested) only deletes records which should be deleted, and only adds records which are being added. Which approach do you think is best? Also, please describe where the race condition occurs, and what might be a solution to eliminate it. Thanks $sql="SELECT c2 FROM m2m WHERE c1=?"; $stmt = $this->db->prepare($sql); $stmt->execute(array($pk)); $existing = $stmt->fetchAll(PDO::FETCH_COLUMN); $deleted=array_diff($existing,$c2s); $added=array_diff($c2s,$existing); if($deleted) { $sql="DELETE FROM m2m WHERE c2 IN (".rtrim(str_repeat('(?,?),',count($deleted)),',').") AND c1=?"; $stmt = $this->db->prepare($sql); $deleted[]=$pk; $stmt->execute($deleted); } if($added) { $data=[]; foreach($added as $item) { $data[]=$pk; $data[]=$item; } $sql="INSERT INTO m2m(c1,c2) VALUES ".rtrim(str_repeat('(?,?),',count($added)),','); echo($this->showQuery($sql,$data)); $stmt = $this->db->prepare($sql); $stmt->execute($data); }
  13. Do you really not know?
  14. Thanks Jacques, So, not the following? Instead use array_diff() (or similar) first to find the deleted and new records, and just delete and add as applicable? $stmt = $this->db->prepare("DELETE FROM m2m WHERE c1=?"); $stmt->execute([$c1]); $data=[]; foreach($c2s as $c2) { $data[]=$c1; $data[]=$c2; } $sql="INSERT INTO m2m(c1,c2) VALUES ".rtrim(str_repeat('(?,?),',count($c2s)),','); $stmt = $this->db->prepare($sql); $stmt->execute($data);
  15. I think this is a PHP question, but if can be solved only with SQL, please move it. In table m2m, I have the following records: c1 c2 1 1 1 2 1 3 2 1 2 5 The server just received a request to use c2 values 1, 3, and 4 for c1 value 1. As such, table m2m should be updated to the following: c1 c2 1 1 1 3 1 4 2 1 2 5 How is this best accomplished?
  16. To quote Requinix.... "implement an API" which bypasses the existing application.
  17. Thanks Jacques, Yea, definitely not very readable using a ternary, and taking way too much time messing around with them. EDIT. I kept the single {{ (row.id?' data-id="'~row.id~'"')|raw }} as is as it was really simple, but change the rest to {% if %}, and it works perfect. Thanks again.
  18. I wish Twig to escape all variables except for row.id and row.path, and not escape the quotes or anchor tabs around each. Is this possible? Thanks <tr{{ (row.id?' data-id="'~row.id~'"')|raw }}><td>{{ row.path?'<a href="'~row.path~'" target="_blank">'|raw~row.name~'</a>'|raw:row.name }}</td><td>{{ row.size }}</td><td>{{ row.date }}</td><td><img alt="Delete File" title="Delete File" src="/lib/stdimages/icon_16/delete.png" class="vtip delete" height="16" width="16"></td></tr>
  19. Let's say I just wanted to get the file extension from a path. I could use either http://php.net/manual/en/function.pathinfo.php or http://php.net/manual/en/splfileinfo.getextension.php. For this specific requirement, which one should I use. Generically, why would one chose one approach over the other?
  20. What happened to PHP 6? Is PHP 7 ready?
  21. Thanks kicken and requinix, Good points about dependency on the filesystem and whether it is a artifact. Not sure how to respond, and would like to provide more info. I am building a simple webpage builder which creates a page such as the below. The application allows the user to upload JS, CSS, and image files to directories /js/, /css/, and /images/ respectively, and this is the part where the date uploaded and file size is presented to the administrator user. The application also allows the user to input desired HTML for a given page which will be presented in #content, and to link JS and CSS resource files to that page which might be locally hosted (as described above) or which are externally hosted. Image files are only linked to the page via the pages HTML markup. So, I have table "pages" - id (pk, int) - name (varchar) - HTML (text) My dilemma is how do I link the local and external resources to the page? Do I create a table which includes the page ID and the resource path such as '/js/file_1.js' or 'http://bla.com/external_file_1.js'? Or do I attempt to normalize the local files and include their path in a separate file and deal with externally located resources differently? <!DOCTYPE html> <html> <head> <title>Page #1</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type='text/javascript' src='/js/file_1.js'></script> <script type='text/javascript' src='/js/file_2.js'></script> <script type='text/javascript' src='http://bla.com/external_file_1.js'></script> <link rel="stylesheet" type="text/css" href="/css/file_1.css" /> <link rel="stylesheet" type="text/css" href="/css/file_2.css" /> <link rel="stylesheet" type="text/css" href="http://bla.com/external_file_1.css" /> </head> <body> Some global HTML goes here... <div id="content"> Possible user provided HTML goes here... <img src='/images/image_1.jpg' alt="x" /> Possible user provided HTML goes here... <img src='/images/image_2.png' alt="x" /> Possible user provided HTML goes here... <img src='/images/image_1.jpg' alt="x" /> Possible user provided HTML goes here... <img src='/images/image_3.jpg' alt="x" /> Possible user provided HTML goes here... </div> Some more global HTML goes here... </body> </html>
  22. I have some files stored in a directory and not a database, and I display the date uploaded as date ("F d Y H:i:s.", filemtime ('/path/to/file.ext')), and do something similar using filesize() for size. I have some other files stored in a directory and their name and surrogate key is also stored in a DB. Do you think I should also store the date uploaded/modified and size in the database instead of deriving it from the file? How should the date be stored so that it will act the same (i.e. daylight savings time, etc) as the first mentioned files where date is based on the Linux operating system? PS. The only reason I am storing the later files in a database is they are linked to another entities using a many-to-many table. Using the other entities PK and the filename instead of a surrogate file PK for the many-to-many table is a bad idea, right?
  23. I can envision how it will work, and think it is perfect. I might have some questions regarding doctype and the like, but will if necessary create a separate post. Thanks
  24. Creating a simple Drupal module which allows the site's administrator to add multiple HTML strings which will be stored in a DB and later displayed on the site. Special elements identified by a given class (I used class "p", but in practice will use something more descriptive) will be replaced with some other content (TBD either clientside using JavaScript of serverside using PHP), and that content will be based on the data-id value. There is also a record corresponding to each data-id value, and I wish to prevent that record from being deleted should an element with that given data-id value exist in any of the saved HTML strings. So, I wish to validate the user's HTML, and wish to determine (i.e. count) whether an element with a given data-id attribute exists in any of the HTML strings. Concerns?
  25. In regards to loading it with anything in PHP that supports HTML, you mean something like simplexml_load_string()? Below is the output of a slightly modified version of Barand's script (changed <p> to <xp>). I suppose I can then set a new error handler to "catch" the warning, and then restoring it to the previous error handler afterwards. Warning: simplexml_load_string(): Entity: line 1: parser error : Opening and ending tag mismatch: xp line 1 and p in /var/www/application/lib/testing/parse.php on line 20 Warning: simplexml_load_string(): <html><xp>Hello</p> in /var/www/application/lib/testing/parse.php on line 20 Warning: simplexml_load_string(): ^ in /var/www/application/lib/testing/parse.php on line 20 Fatal error: Call to a member function xpath() on a non-object in /var/www/application/lib/testing/parse.php on line 24 I've never used tidy() before. How do you envision this working? Another option maybe is an api into https://validator.w3.org/docs/api.html? Don't know how it will work yet. Thanks
×
×
  • 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.