简体   繁体   English

在CodeIgniter中将PHP与HTML结合的最佳方法?

[英]Best way to combine PHP with HTML in CodeIgniter?

I've took over a PHP app which code is quite a mess so before making any major changes I've decided to rewrite it to MVC (CodeIgniter). 我已经接管了一个PHP应用程序,该应用程序的代码相当混乱,因此在进行任何重大更改之前,我决定将其重写为MVC(CodeIgniter)。 As for pure html sections I use $this->load->view(file); 至于纯html部分,我使用$this->load->view(file); technique, but I'm not sure how to cope with something like this: 技术,但我不确定该如何处理:

  echo "<tr>";
  echo "<td class=\"border szczegolyTd\">";
  echo "Kamp.: ".$kampania[$kamp]['idProject'];
  echo "<br />";
  echo "<b>".$kampania[$kamp]['name']."</b><br />";
  echo "<div class='szczegolyDiv'><a class=\"pokaz szczegoly\" href=\"?pokaz=".$kampania[$kamp]['idProject']."\">";
  echo "SZCZEGÓŁY";
  echo "</a></div>";
  if (isset($kampania[$kamp]['timestamp'][$iloLeftCustomers-1])) echo "<br />Dane od: <br /><b>".$kampania[$kamp]['timestamp'][$iloLeftCustomers-1]."</b>";
  echo "<br />do: <br /><b>".$kampania[$kamp]['timestamp'][0]."</b>";
    //echo "<br />".$ilOstatnichRozmow;
  polacz();
  $querySpr = mysql_fetch_assoc(mysql_query("SELECT COUNT(*) AS ile FROM lista WHERE user=".$_SESSION['loginid']." AND kampania=".$kampania[$kamp]['idProject'].""));
  rozlacz();
  if ($querySpr['ile']==0) {
    echo "<div id=\"".$kampania[$kamp]['idProject']."\" class=\"tabDodaj\">DODAJ DO OBSERWOWANYCH</div>";
  }else{echo "<div class='komunikatMasz'>Masz tę kampanię na liście.</div>";}

  echo "</td>";

I'm a beginner in CodeIgniter (and MVC in general), so I don't know all its features and which to choose. 我是CodeIgniter(通常是MVC)的初学者,所以我不知道它的所有功能以及选择哪个功能。 Perhaps it's not possible to seperate these 2 technologies completely - so it's more elegant to mainly use html and escape php scripts with <? ?> 也许不可能完全分离这两种技术-因此以<? ?>为主使用htmlphp脚本更为优雅<? ?> <? ?> or to use php and echo html parts? <? ?>或使用phpecho html部分?

You can always write html in the template file and insert php in it to loop or echo. 您总是可以在模板文件中编写html,然后在其中插入php以循环或回显。

echo "<tr>";
echo "<td class=\"border szczegolyTd\">";
echo "Kamp.: ".$kampania[$kamp]['idProject'];
echo "<br />";

Could be: 可能:

<tr>
<td class="border szczegolyTd">
Kamp.: <?php echo $kampania[$kamp]['idProject']; ?>
<br />

Putting code in those PHP tags wil actually fire the call in place. 将代码放在这些PHP标记中实际上会触发该调用。 Mind you, to keep the html clean of bussines code make sure you only use echo, foreach, for or if in it. 提醒您,要确保html不含商务代码,请确保仅在其中使用echo,foreach和for。

In CodeIgniter you could render a view file and insert into a "partial" view and just echo it in the main template using TRUE for the 3rd parameter. 在CodeIgniter中,您可以渲染视图文件并将其插入“部分”视图中,并在第三个参数中使用TRUE将其回显到主模板中。 This only buffers the output instead if immediatly outputting it. 仅在立即输出时才缓冲输出。

$data['content'] = $this->load->view('ding/templatefile',$vars,TRUE); 

if you are really new, please follow this tutorial its nice && informative. 如果您真的是新手,请按照本教程的&&信息丰富的原则进行操作。

yes it is possible, the best use is short tag + equal sign <strong><?=$variable?></strong> and it echoes whatever is in $variable , whenever you use foreach() use either 是的,这是可能的,最好的用法是短标签+等号<strong><?=$variable?></strong>并且在使用foreach()时,它会回显$variable任何内容

foreach($elements as $element) {

    $string = "";
    $string .= "new line" . $element->id . "<br>";
    $string .= "line 2" . $element->name;

    echo $string;

}

or just echo lines as you wish 或随心所欲地回声

foreach($elements as $element) {

    echo "new line" . $element->id . "<br>";
    echo "line 2" . $element->name;

}

but remember, before going in to foreach loop, please check if you have valid $variable to go thru. 但是请记住,在进入foreach循环之前,请检查是否有有效的$ variable可以通过。 ( empty() , isset() , $variable != FALSE ...) empty()isset()$variable != FALSE ...)

note: if you need condition while echoing basic text use 注意:如果在回显基本文本时需要条件,请使用

<strong><?=(condition) ? 'TRUE' : 'FALSE' ?></strong>

example

<strong><?=(isset($variable)) ? $variable : '' ?></strong>

I would advise you go read up on the flow of how a MVC works, but this should point you in the right direction. 我建议您继续阅读MVC的工作流程,但这应该为您指明正确的方向。

In the controller you instantiate the model of the data like: 在控制器中,您可以实例化数据模型,例如:

$this->load->model("lista");

Then, 然后,

$data = $this->lista->getCountList($_SESSION['loginid'], $kampania[$kamp]['idProject']); 

where in the lista model you have the function: 在lista模型中您具有功能的位置:

getCountLista($loginId, $projectId)

which does this part: 这部分内容:

return mysql_fetch_assoc(mysql_query("SELECT COUNT(*) AS ile FROM lista WHERE user=".$loginId." AND kampania=".$projectId.""));

Then you pass the returned data to the view 然后,您将返回的数据传递给视图

$this->load->view(file, array("listData" => $data));

and in the view: 并在视图中:

$listaData[$kamp]['idProject']

It creates an variable in the view foreach item in the array passed in the view. 它在视图中为视图中传递的数组中的每个项目创建一个变量。

I commend you to read documentation about MVC. 我建议您阅读有关MVC的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM