文本对齐(if_elseif_else)

本文档展示了如何使用PHP结合Smarty模板引擎,根据条件($dq)动态生成左侧、右侧或居中对齐的内容。通过加载配置文件'if.conf'获取背景颜色和显示状态,并在HTML中应用。内容从数据库查询获取,利用foreach遍历并展示在表格中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

if.conf

  #全局变量
bgcolor = #00ffff
display = true

[mycolor]
bgcolor = #cccc00


[yourcolor]
bgcolor = #123456


if_else.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>if_else</title>
</head>

<body>
<form action="if_else.php" method="post">
<input type="text" name="text" />
<input type="radio" name='dq' value="left" />left
<input type="radio" name='dq' value="right" />right
<input type="radio" name='dq' value="center" />center<br />
<input type="submit" value="提交" />

</form>
</body>
</html>

if_else.php


<?php
       
      include 'libs/Smarty.class.php';
      
    
      $link = mysql_connect('localhost','root','123') or die('数据库连接失败');
      mysql_select_db('ecshop_test',$link);
      mysql_query("set names utf8");

      $sql = mysql_query("select * from brand where id like '".$_POST['text']."'");
      $result = mysql_fetch_row($sql);
      
      
      $smarty = new Smarty();
      $smarty->template_dir="demo/templates";
      $smarty->compile_dir="demo/templates_c";
      $smarty->config_dir="demo/config";
      $smarty->left_delimiter="<{";
      $smarty->right_delimiter="}>";
      $smarty->assign('text',$result);
      $smarty->assign('dq',$_POST['dq']);
      $smarty->display('if_else.tpl');

if_else.tpl


<{config_load file='if.conf' section='mycolor'}>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>if_else</title>
</head>

<body bgcolor=<{ #bgcolor# }>>
<{ if #display# }>
<{ if $dq=='left'}>
<div style=" float:left;">
<table>
<tr>
<{ foreach from=$text item=value }>
<td><{ $value }></td>
<{ /foreach }>
</tr>
</table>
</div>
<{ elseif $dq=='right'}>
<div style=" float:right;">

<table>
<tr>
<{ foreach from=$text item=value }>
<td><{ $value }></td>
<{ /foreach }>
</tr>
</table>
</div>
<{else}>
<center><div>
<table>
<tr>
<{ foreach from=$text item=value }>
<td><{ $value }></td>
<{ /foreach }>
</tr>
</table>
</div></center>
<{/if}>

<{ else }>
没有显示
<{/if}>
</body>
</html>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值