我认为我越来越了解如何将我的网站转向更多PHP OOP方法。
我的每个用户都有不同的页面,因为它们对javascript生成的网格都有不同的需求。为了可维护性,我可以有一个页面,只是将php注入javascript,因为我(有点)显示如下?标准做法是将php变量保留在MySQL中并根据$ _SESSION ['user_id']将它们吐出来;到目前为止,我想知道自己是否走上正确的道路?
ORIGINAL GRID
var mygrid;
function doInitGrid(){
mygrid = new dhtmlXGridObject('gridbox');
var combo=mygrid.getCombo(4);
combo.put(2, 'Approve / Deny');
combo.put(1, 'Approve');
combo.put(0, 'Deny');
mygrid.setImagePath("../codebase/imgs/");
mygrid.setHeader("Submit Date, Manufacturer, Product Category, Progress, Approval Status",null,["text-align:center;","text-align:center;","text-align:center","text-align:center","text-align:center"]);
<!--mygrid.attachHeader("#text_filter,#text_filter,#text_filter");-->
mygrid.setInitWidthsP("10,16,44,18,12");
mygrid.setColAlign("center,left,left,center,center");
mygrid.enableAutoWidth(false);
mygrid.setColTypes("ro,ro,ro,button,coro");
mygrid.setColSorting("str,str,str,str,str,str");
mygrid.enableRowsHover(true,'grid_hover');
mygrid.setSkin("modern");
mygrid.init();
mygrid.setStyle("font-size:11px;","font-size:11px;","font-size:11px","font-size:11px","font-size:11px");
mygrid.loadXML("connector.php?hospid=<?php echo $hospid; ?>");
mygrid.setColumnIds("date,man_name,group,approval_progress, approval_status");
var dp = new dataProcessor ("connector.php?hospid=<?php echo $hospid; ?>");
dp.init(mygrid);
dhxWins = new dhtmlXWindows();
dhxWins.setImagePath("../codebase/imgs/");
mygrid.attachEvent("onRowSelect", function(id,ind){
if (ind == 0 || ind == 1 || ind == 2 || ind == 3){
dhxWins = new dhtmlXWindows();
var w1 = dhxWins.createWindow("w1", 60, 60,925, 575);
dhxWins.setSkin("dhx_web");
dhxWins.setImagePath("../codebase/imgs/");
w1.centerOnScreen();
w1.setText("");
w1.setModal(false);
theVar11 = (id);
w1.attachURL("pop_details.php?var1=" +theVar11);
return true;
};
});
MY IDEA FOR OOP GRID
var mygrid;
function doInitGrid(){
mygrid = new dhtmlXGridObject('gridbox');
var combo=mygrid.getCombo(4);
combo.put(2, 'Approve / Deny');
combo.put(1, 'Approve');
combo.put(0, 'Deny');
mygrid.setImagePath("../codebase/imgs/");
mygrid.setHeader(<?php $MySQL_results ?>);
<!--mygrid.attachHeader("#text_filter,#text_filter,#text_filter");-->
mygrid.setInitWidthsP("<?php $MySQL_results ?>");
mygrid.setColAlign("<?php $MySQL_results ?>");
mygrid.enableAutoWidth(false);
mygrid.setColTypes("<?php $MySQL_results ?>");
mygrid.setColSorting("<?php $MySQL_results ?>");
mygrid.enableRowsHover(true,'grid_hover');
mygrid.setSkin("modern");
mygrid.init();
mygrid.setStyle("<?php $MySQL_results ?>");
mygrid.loadXML("connector.php?hospid=<?php echo $hospid; ?>");
mygrid.setColumnIds("date,man_name,group,approval_progress, approval_status");
var dp = new dataProcessor ("connector.php?hospid=<?php echo $hospid; ?>");
dp.init(mygrid);
dhxWins = new dhtmlXWindows();
dhxWins.setImagePath("../codebase/imgs/");
mygrid.attachEvent("onRowSelect", function(id,ind){
if (ind == 0 || ind == 1 || ind == 2 || ind == 3){
dhxWins = new dhtmlXWindows();
var w1 = dhxWins.createWindow("w1", 60, 60,925, 575);
dhxWins.setSkin("dhx_web");
dhxWins.setImagePath("../codebase/imgs/");
w1.centerOnScreen();
w1.setText("");
w1.setModal(false);
theVar11 = (id);
w1.attachURL("pop_details.php?var1=" +theVar11);
return true;
};
});
答案 0 :(得分:3)
在我看来,(和其他人可能会有任何不同意见)你提议的是一个完全可行的选择。如果你不想在你的javascript中包含这么多的php,你可以使用php json_encode()
函数来创建一个初始的javascript对象,并使用它的属性而不是php回声。像这样的东西:
var gridOpts = <?php echo json_encode($MySQL_results);?>;
mygrid.setColTypes(gridOpts.colTypes);
答案 1 :(得分:2)
我看不懂那个代码,太乱了......
但是对于一般用途:
<script type="text/javascript">
var js_variable = <?php echo json_encode($php_variable); ?>;
</script>
此方法允许您将任何类型的PHP变量(除Resources之外)传递给JS。