如何使这段代码更具可读性
这是php代码的一部分,它使用contentArray,它是一个JSON,并向用户生成UI,它生成html标签,也生成js代码......它有效,但我认为代码很难阅读和维护,有什么想法吗???谢谢。
for($i = 0; $i < count($contentArray); $i++){
if($i %2 == 0){
echo ("<li class='even_row'>");
}else{
echo ("<li class='odd_row'>");
}
$content = $contentArray[$i];
echo("<textarea class='userdata' id='user_data_textarea_".$content->{'m_sId'}."'>");
echo($content->{'m_sDataContent'});
echo("</textarea>");
echo("</li>");
echo("<script type='text/javascript'>");
echo("$('#user_data_textarea_".$content->{'m_sId'}."').bind('keydown', function(e){");
echo(" TypingHandler.handleTypingInUserDataTextArea(".$content->{'m_sId'}.", e);");
echo(" });");
echo("</script>");
}
This is a part fo php code, which use the contentArray, which is a JSON, and generate the UI to the user, it generate html tags, also, it generate js code too.... It works, but I think the code is pretty difficult to read and maintain, any ideas??? thank you.
for($i = 0; $i < count($contentArray); $i++){
if($i %2 == 0){
echo ("<li class='even_row'>");
}else{
echo ("<li class='odd_row'>");
}
$content = $contentArray[$i];
echo("<textarea class='userdata' id='user_data_textarea_".$content->{'m_sId'}."'>");
echo($content->{'m_sDataContent'});
echo("</textarea>");
echo("</li>");
echo("<script type='text/javascript'>");
echo("$('#user_data_textarea_".$content->{'m_sId'}."').bind('keydown', function(e){");
echo(" TypingHandler.handleTypingInUserDataTextArea(".$content->{'m_sId'}.", e);");
echo(" });");
echo("</script>");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
首先,对于奇数和偶数样式,不需要类只需使用 css
这里是信息 然后在
php 中只在一行中回显您需要的内容
,然后将 jquery 放在远离 php 的 html 页面中,
我们可以通过使用 以选择器开头
first for your odd and even styling there is not need for a class just use css
here is info on that
then in php only echo what you need in one line
and lets put the jquery in the html page away from php
we get can get every item by using starts with selector
关于“for”循环的一个技巧是,您应该在循环之前计算 $contentArray 的计数。每次执行循环时,都必须调用该函数。
One tip on your "for" loop, you should calculate the count of $contentArray before the loop. Every time the loop executes, it has to call that function.
你可以尝试真正的 HTML:
You could try real HTML:
它应该看起来像这样,以便在 IDE 中具有更好的可读性。
It should look something like this, for better readability in the IDE.
您可以从 echo 语句中删除 ( ),它们不一定需要,并且可能有助于使其看起来更整洁......
You could remove the ( ) from the echo statements, they're not necessarily needed and might help make it look a little neater...
这对我来说实际上看起来很容易理解;我可以毫无困难地弄清楚你在做什么。我建议的唯一区别是使用 三元运算符对于行类:
...但这只是我的情况,有些人会发现这更令人困惑,而不是更少。我个人喜欢将所有内容放在一行中。
That actually looks pretty understandable to me; I could figure out what you're doing without difficulty. The only difference I would suggest would be the use of ternary operators for the row class:
...but that's just me, some would find that MORE confusing, rather than less. I personally like putting it all in one line.
就我个人而言,我喜欢使用 printf 在 php 中编写 html 代码。它可能看起来像:
Personnaly, I like to use printf to write html code in php. It could look like:
jQuery 代码应该已经在 HTML 中,使用一些主选择器而不是一一绑定元素,这对我来说没有意义。这应该可以澄清你的代码。
添加
一般情况:
即使用类选择器进行绑定,并稍后使用一些唯一标识符来完成这项工作。
jQuery code should be already in the HTML, using some main selector and not binding elements one by one, it not makes sense for me. That should clarify your code.
ADDED
A generic case:
That is, bind using a class selector and late use some unique identifier for doing the job.
将所有内容放入数组中,然后在循环末尾回显它们。
Put everything in arrays, then echo them at the END of your loop.
使用例如 smarty 分隔您的内容和代码。它在短期内需要一些基础设施投资,但从长远来看可以改善维护。
考虑到这些评论,让我们将 PHP 视为真正的模板语言。
Separate your content and code using for example smarty. It requires some infrastructure investment in the short term, but improves maintenance in the long run.
Reflecting the comments, let's then treat PHP as a real templating language.