PHP MYSQL 编辑表单,试图将行内爆/爆炸到复选框?

发布于 2024-11-30 00:58:01 字数 706 浏览 2 评论 0原文

我有一个包含多个类别的表单。它们由复选框分隔,因此可以检查其帖子/故事所属的任意数量的类别。

这些值存储在数据库行内,在 INSERT 时用逗号分隔它们

GetSQLValueString(implode($_POST['r_category'],", "), "text"),

所以在数据库中我可以有一个如下所示的条目:

2,3,6,12

这些是类别号。

我现在正在构建一个编辑表单功能,用于编辑和更新这些记录。我找不到将值“爆炸”到编辑表单上的复选框中的方法?

因此,如果您登录并转到要编辑的条目,例如,我有分隔类别的复选框,因此很容易选中和取消选中添加和不添加...我已尝试了一切我能得到的查看此编辑表单时要发布并检查的值。

这是编辑表单页面上的一个类别复选框的示例,但当我有多个类别时,它不起作用,如上面的示例.. 2,3,6,12 ?? :

  <input type="checkbox" name="r_category[]" 
<?php if (!(strcmp("2", $row_Recordset2['r_category']))) {
              echo "checked=\"checked\"";} ?> value="2" />Cat 2

提前感谢忍者!!

I have a form that has multiple categories. They are separated by checkboxes, so one could check any number of categories that their post/story will be in.

The values are stored inside a db row separating them by commas while INSERT

GetSQLValueString(implode($_POST['r_category'],", "), "text"),

So in the database I could have an entry that looks like this:

2,3,6,12

Which those are category numbers.

I am now building an edit form feature to use to Edit and UPDATE these records. I can not find a way to "EXPLODE" the values into checkboxes on the edit form??

So if you log in and go to the ENTRY that you want to edit for example, I have Checkboxes separating the Categories, so it will be easy to check and uncheck to add and not add... I have tried everything I could to get the values to POST and be checked when viewing this Edit Form.

Here is an example of one of my Category Check boxes on my edit form page, but it is not working when I have multiple Categories, like the example above.. 2,3,6,12 ?? :

  <input type="checkbox" name="r_category[]" 
<?php if (!(strcmp("2", $row_Recordset2['r_category']))) {
              echo "checked=\"checked\"";} ?> value="2" />Cat 2

Thanks in advance NINJAS!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若有似无的小暗淡 2024-12-07 00:58:01

我找不到将值“爆炸”到编辑表单上的复选框的方法

这一行与答案非常接近,我真的想知道这是否是一个笑话帖子。

考虑到您的数据库结构,您可以使用 explode()< /code>函数根据分隔符将值分解为数组。例如:

$values = "1,2,3,4";
$array_of_values = explode(",", $values);

此后,$array_of_values 将包含一个由四个元素组成的数组,其值为 123 和 4

当决定是否将复选框显示为已选中时,可以使用 < code>in_array() 函数来进行该调用:

if (in_array("2", $array_of_values)) {
  echo 'checked="checked"';
}

除此之外,让我借此机会支持 @MarkB 在这个问题的评论中所说的话:这是一个在单个数据库字段中保存多个值的想法。它使搜索变得更加困难,使添加值变得更加困难......它只会使一切变得比需要的更加复杂。

处理此问题的最佳方法是使用一个多对多表来存储多行项目和类别,其中一行对应项目所属的每个类别。

换句话说,您当前拥有的位置:

Item | Category
---------------
123  | 1,2,3,4

您将拥有一个如下所示的表:

Item | Category
---------------
123  | 1
123  | 2
123  | 3
123  | 4

I can not find a way to "EXPLODE" the values into checkboxes on the edit form

This line is so close to the answer that I'm seriously wondering if this is a joke post.

Given your database structure as it is, you can use the explode() function to break the value into an array based on a delimiter. For example:

$values = "1,2,3,4";
$array_of_values = explode(",", $values);

After this, $array_of_values will contain an array of four elements with the values of 1, 2, 3, and 4 respectively.

When deciding whether or not to display the checkbox as checked, you can use the in_array() function to make that call:

if (in_array("2", $array_of_values)) {
  echo 'checked="checked"';
}

Beyond that, let me take this opportunity to second what @MarkB said in the comments on this question: it's a bad idea to hold multiple values in a single database field. It makes searching for things harder, it makes adding values harder... it just makes everything more complex than it needs to be.

The best way to handle this would be to have a many-to-many table which stores rows of items and categories, with one row for each category that an item belongs to.

In other words, where you currently have this:

Item | Category
---------------
123  | 1,2,3,4

You would instead have a table that looks like this:

Item | Category
---------------
123  | 1
123  | 2
123  | 3
123  | 4
爱,才寂寞 2024-12-07 00:58:01

您可以如下所示使用它,它可能会有所帮助

if (in_array("2", $row_Recordset2['r_category'])) {echo "Checked";} 

You can use it as shown below it could be helpful

if (in_array("2", $row_Recordset2['r_category'])) {echo "Checked";} 
护你周全 2024-12-07 00:58:01

in_array 条件对我来说从来不起作用。我设计了一个完全前端的解决方案,它像一个魅力一样工作,并根据数据库的更改自动更新。如果您有兴趣,请查看这里。
https://stackoverflow.com/a/26433841/1896348

That in_array conditional never did work for me. I devised a totally front-end solution instead that works like a charm and automatically updates itself based on changes to the database. If you're interested, check it out here.
https://stackoverflow.com/a/26433841/1896348

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文