Jump to content

NotionCommotion

Members
  • Posts

    2,446
  • Joined

  • Last visited

  • Days Won

    10

Everything posted by NotionCommotion

  1. Still would opinions why storing data this way will cause issues other than lack of referential integrity and inability to properly query on a column.
  2. After giving a little more thought, I now feel my (in my head) slick idea of receiving a "map" from the client to the server property is not so slick. What if given my particular application the location of a property is not the same for all chart types? I think it is better for the server to just receive "updateTitle" and figure out how to do so. Maybe not, but that is how I feel at this instance.
  3. Thanks Jacques1, Not really graphically. They would select a chart type from a select menu and input a unique name so they can identify the chart in the future. They can then add/delete series to the chart. The application would only give the ability to change limited portions such as the main chart title and the individual series legends, and would do so on a per-property basis (i.e. the server would get a request to change a single property to a new value). To change the main title, wouldn't it be as simple as $obj=json_decode($json_from_db); $obj->title->text="new title";, and then encode the object and store it back in the database? If they want to do more than this, they would write their own JSON which would replace the JSON in the database.
  4. Below is an untested possible implementation. The intent is to change "Firefox" to "My New Browser". I don't think $prop acts as a reference to $obj, so will probably need to figure something slightly different out. function getFromDB($id) { $db[123]='{ "chart": { "type": "pie" }, "title": { "text": "Browser market shares January, 2015 to May, 2015" }, "series": [ { "name": "Brands", "colorByPoint": true, "data": [ {"name": "Microsoft Internet Explorer","y": 56.33}, {"name": "Chrome","y": 24.03}, {"name": "Firefox","y": 10.38}, {"name": "Safari","y": 4.77}, {"name": "Opera","y": 0.91}, {"name": "Proprietary or Undetectable","y": 0.2} ] } ] }'; return $db[$id]; } $_POST=['task'=>'updateObject', 'chartID'=123,'node'=>'series[0].data[2].name', value=> 'My New Browser']; //From client $json=getFromDB($_POST['id']); $obj=json_decode($json); $prop=$obj; //From client $_POST['elem']="series[0].data[2].name"; $elem=explode(".",$_POST['elem']); foreach($elem as $node) { if($pos = strpos($node, "[")){ $index=substr($node,-$pos+1,-1); $node=substr($node,0,$pos-1); $prop=$prop->{$node}[$index]; } else { $prop=$prop->{$node}; } } $prop=$_POST['value']; $json=json_encode($obj); //Save $json in database for id 123
  5. Hi ginerjm, I don't think you are going to get your head bit off, and most will recommend not storing data in JSON. For this case, however, I believe it makes sense for the following reasons: There is no referential integrity requirements. There will never be any requirement to query the individual data held within the JSON. 99.9% of the time, I need the data in JSON format so I could send it to the client, and 0.1% of the time I need it in a PHP array so I could manipulate it, and don't want the extra overhead. The data is only being used to populate a JavaScript application. I will also be allowing the user to edit the entire JSON text and will only be validating certain parts server-side. There are some properties which I don't wish to model, and using JSON allows me to do so. If right or wrong the data is being stored as a JSON string, do you have recommendations what should be used to target specific properties so they may be changed?
  6. I will be storing various Highcharts configuration files as JSON text in a database, and will be creating an API to allow the user to change it. One option is to use a POST such as {task: ”updateTitle”, chartID: 123, value: “My New Title”}. I know often it is recommended to decoupling the backend from the client, but the Highcharts configuration object is nothing secret, and think there might be a better way. Any thoughts about posting something like the following, and then having the server parse it? Or maybe use PHP notation instead of JavaScript? Or maybe send an object instead of the text map? Recommendations on how to implement? {task: "updateObject", chartID: 123, element: "title.text", value: "My New Title"} {task: "updateObject", chartID: 123, element: "series[0].data[2].name", value: "My New Browser"} {task: "deleteObjectElem", chartID: 123, element: "series[0].data[2]"} {task: "addObjectElem", chartID: 123, element: "series[0]", value: "{"name": "Another Browser","y": 12.33}"} Side note. If PHP supported PUT and PATCH, which method should be used for the above 4 server calls? { "chart": { "type": "pie" }, "title": { "text": "Browser market shares January, 2015 to May, 2015" }, "series": [ { "name": "Brands", "colorByPoint": true, "data": [ {"name": "Microsoft Internet Explorer","y": 56.33}, {"name": "Chrome","y": 24.03}, {"name": "Firefox","y": 10.38}, {"name": "Safari","y": 4.77}, {"name": "Opera","y": 0.91}, {"name": "Proprietary or Undetectable","y": 0.2} ] } ] }
  7. Thanks Barand, I too was thinking of my second approach, but wanted another opinion. And thank you too for the implementation script. I rarely use list(), but probably should do so more often.
  8. I've got the following two tables and wish to create the following array. What is the best way to create the array? Thanks Do the first query and loop through them using PHP, and then a prepared statement for the second? Inner join the two tables, loop through them using PHP, and set an array if not currently set, and if set, just add to the array? Inner join the two tables, and use GROUP_CONCAT and then explode()? While I think this approach is slick, I don't want to use it due to the truncation limitation. Something else? Companies 1 Ford 3 General Motors 5 Chrysler Cars 1 1 Mustang 2 1 Fusion 3 3 Corvette 4 3 Escalade 5 5 Charger 6 5 Challenger $cars=[ [ 'id'=>1, 'name'=>'Ford', 'cars'=>[ ['id'=>1, 'Mustang'], ['id'=>2, 'Fusion'] ] ], [ 'id'=>3, 'name'=>'General Motors', 'cars'=>[ ['id'=>3, 'Corvette'], ['id'=>4, 'Escalade'] ] ], [ 'id'=>5, 'name'=>'Chrysler', 'cars'=>[ ['id'=>5, 'Charger'], ['id'=>6, 'Challenger'] ] ] ];
  9. I agree implementing ever little feature of Highcharts isn't really necessary. I just didn't know if I would later discover some user need which would require JavaScript. For now, I will not attempt to do so. Thanks
  10. Your hopes have been met. I don't want the first scenario! Regarding color, I agree there are better strategies which don't require storing and running arbitrary JavaScript code. There are other times, however, when JavaScript is used in the configuration, and I may not be able to use some sort of preprocessing. Unless I can come up with a good way of deal ling with them, it seems like maybe I should not even attempt to support these other cases.
  11. On my second post, I added the cryptic "I just recalled that I plan on storing data in the string, so it needs to be well formed JSON no matter which option I take.", so this is not so easy. My reason is I wish to be able to change, for instance, just the title. Sure makes it easier if it is JSON. For color, I can keep it all server-side, so I don't have the need to store script. There is also some other cases, however, where JavaScript is used in the Highchart config, and it would be nice to be flexible enough to handle these cases when they become evident.
  12. I just recalled that I plan on storing data in the string, so it needs to be well formed JSON no matter which option I take. Here are my thoughts: Authorized user adds the initial JSON such as: '{"foo":"bar","script":"{{ javascript(); }} }'. PHP confirms the string is well formed and identifies the nodes which have the {{ }} deliminator. These node locations are stored in the JSON as meta data, and maybe the deliminator are stripped. Either:The JSON which includes the script node meta data are sent to the client via JSON, and the client takes appropriate action on the script nodes. Two copies of the string are stored: one which is JSON and the other which is used to write the JavaScript on the client. If the string needs to be edited, the deliminators are reinserted by the server allowing the authorized user to edit. Thoughts?
  13. 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 } ] } ] }
  14. 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?
  15. 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 } ] } ] }
  16. 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
  17. Thanks Kicken, Yes, I knew that, just didn't say that! Makes perfect sense (now). Thank you!
  18. 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 );
  19. 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); } }
  20. 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
  21. Thanks Barand, Where can I read up on the expression context? Specifically the first part such as "*" and "//p"?
  22. 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 */.
  23. ??? <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">
  24. 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); }
×
×
  • 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.