php - php将字符串拆分为html形式
问题描述
我以以下格式将字符串存储在 mysql 数据库中:
$row["actions"]= Add&&Remove&&Replace&&Change&&
例如,我需要将其中的每一个拼接成一个复选框 html 表单
while{
echo $row["actions"];
//this would be the string to cut up
}
for each before && as cut {
<form action="" method="post">
//name would be add
<input type="checkbox" name='$cut[0]' value="Bike"> I have a bike<br>
//name would be Remove
<input type="checkbox" name='$cut[1]' value="Bike"> I have a bike<br>
//name would be replace
<input type="checkbox" name='$cut[2]' value="Bike"> I have a bike<br>
<input type="submit" value="Submit">
</form>}
解决方案
为什么你不能只使用简单的检查,因为你有预定义的动作?
// If you have "Add" in your string, then print needed string
if (strpos( $row["actions"], 'Add') !== false) {
echo "<input type=\"checkbox\" name='Add' value=\"Bike\"> I have a bike<br>"
}
//... and so on`
如果你真的需要拆分和迭代你的值,那么看看explode
函数,它就是这样做的。文档。
目前还不清楚您要达到的目标。正如评论中已经指出的那样,您不应该:
- 在数据库的一行中存储多个值 \ 在一个变量中
- 有用于互斥操作的复选框 (?)。可能你想要这里的单选按钮。
另请注意,您的代码不是有效的 PHP 代码。你不能像这样混合 HTML 和 PHP。因此,您需要关闭 PHP 标记 like<?php $a=1; ?> <a href="google.com">HTML goes here</a>
或 doecho
或print
:echo "<a href=\"google.com\">Some HTML</a>";
推荐阅读
- sql-server - 在 order by 子句之前使用 case 语句
- mysql - 关键查询mysql中的条件分组
- java - 弹簧靴 | 如何将我的应用程序的上下文 URL 从“gerp-tasks-rest-0.0.1-SNAPSHOT-wildfly10”更改为“terptasks”| 野蝇
- java - 我们如何在 Spring Boot 中保存到表关系中
- node.js - 如何使用有效负载在nodejs中创建uuid?
- javascript - Konvajs - 相对放大到指针,但非相对缩小
- java - 如何在 OpenAPI 3 中正确定义可为空属性的示例?
- elasticsearch - 只能从另一个嵌套对象中获取一个嵌套对象的可能性
- ios - 如何在 iOS 中下载和打开文件?
- java - springboot 启动失败,Linux 服务器安装了 dynatrace 代理(APM)