根据条件从其他变量组装一个变量

发布于 2024-09-29 18:41:12 字数 1088 浏览 2 评论 0原文

今天脑子完全不灵了。

因此,我在表中有几列基本上指定了用户是否提供了某条信息。

例如,我有一个表:

| ID | USER | crit1 | crit2 | crit3 | crit4 | etc. |

现在,该记录的任何“critX”字段都可以为 1 或 yes。我对数学和排列不太了解,但我猜如果有 4 列,你可能会有 16 种输出组合。在我的现实世界示例中,我有 16 个不同的标准,因此我无法考虑混乱的输出。我需要编写某种例程。

在我的示例中,将评估每个临界值,如果标准 == 1/yes,它将包含在另一个变量中,并为其分配更人性化的数据位。我目前正在从数据库中提取每个值,然后执行类似

### first I pull the values
$mydbarray[crit1] = $cr1;
$mydbarray[crit2] = $cr2;
$mydbarray[crit3] = $cr3;
(etc...)

### then I assign some human friendly text ONLY if the value == 1/yes
if($cr1==1) ($cr1 = "This info is present!";}
if($cr2==1) ($cr2 = "Number two is present!";}
if($cr3==1) ($cr3 = "Three was provided!";}

现在,我需要做的事情,仅当“IF”为 true 时才收集所有输出并将其组装成最终变量。

所以不知何故,我想要:

$finaloutput = $cr1, $cr2, $cr3;

显然这不是有效的或我想要的,但即使它确实有效,它最终也会包括所有 == 0/no 实例。

所以本质上我需要对这些变量进行条件分组,但我没有得到它。

我正在考虑投射一个数组并循环遍历它,但后来我对包含人类可理解的部分感到不知所措......

工作中的一些人提到了使用 bool 的 IF 语句,但我对此并不熟悉。

我认为这很容易,但我整个星期都在照顾孩子。所以我的脑子坏了。

提前致谢!!! 抢

Brain totally not working today.

So, I have a few columns in a table that basically designate whether a certain piece of information WAS or WAS NOT provided by the user.

For example, I have a table with:

| ID | USER | crit1 | crit2 | crit3 | crit4 | etc. |

Now, the record could have a 1 or yes for any of the "critX" fields. I dunno much about math and permutations, but I guess if there were 4 columns, you could have 16 combinations of output. In my real world example I have 16 different criteria, so I can't factor for the output of that mess. I need to write a routine of some sort.

In my example, each of those crit values is going to be evaluated and if the criterion == 1/yes, it will be included in another variable AND have a more human friendly bit of data assigned to it. I am currently pulling each value from the DB an doing something like

### first I pull the values
$mydbarray[crit1] = $cr1;
$mydbarray[crit2] = $cr2;
$mydbarray[crit3] = $cr3;
(etc...)

### then I assign some human friendly text ONLY if the value == 1/yes
if($cr1==1) ($cr1 = "This info is present!";}
if($cr2==1) ($cr2 = "Number two is present!";}
if($cr3==1) ($cr3 = "Three was provided!";}

Now, what I need to do, is collect all that output only if the "IF" fired on true and assemble into a final variable.

So somehow, I want:

$finaloutput = $cr1, $cr2, $cr3;

Obviously that's not valid or what I want, but even if it DID work, it would end up including all the == 0/no instances as well.

So essentially I need a conditional grouping of these variables and I am not getting it.

I was thinking of casting an array and looping through it, but then I was at a loss for including the human intelligible portions...

Some guy at work mentioned an IF statement using bool, but I am not wellversed there.

I would think this is easy, but I've been up all week with the baby. so my brain is broken.

Thanks in advance!!!
Rob

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

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

发布评论

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

评论(2

蓝礼 2024-10-06 18:41:12

首先,您编写的大部分内容都不是有效的 PHP。我假设您编写它只是为了说明这一点,而没有考虑语法。

操作方法如下:

您采用一个数组,在其中放置文本:

$texts = array();

对于每个条件,您检查是否提供了它们,但添加到之前创建的数组中:

if($cr1==1) {$texts[] = "This info is present!";}
if($cr2==1) {$texts[] = "Number two is present!";}
if($cr3==1) {$texts[] = "Three was provided!";}
...

最后,将所有文本与 implode

$finaloutput = implode(', ', $texts);

我更喜欢使用 implode 的方法,而不是仅附加到字符串的方法,因为如果我想要逗号分隔符,我不会在末尾得到额外的逗号分隔符.

祝你好运,阿林

First of all much of what you wrote is not valid PHP. I will assume you wrote it just to illustrate the point and didn't bother with the syntax.

Here is how to do it:

You take an array in which you will put your texts:

$texts = array();

For each of your criteria you check if they are provided, but add to the array created before:

if($cr1==1) {$texts[] = "This info is present!";}
if($cr2==1) {$texts[] = "Number two is present!";}
if($cr3==1) {$texts[] = "Three was provided!";}
...

In the end you concatenate all your texts with implode:

$finaloutput = implode(', ', $texts);

I prefer the method with implode to the one that just appends to a string because if I want a comma separator I don't get an extra one at the end.

Good luck, Alin

客…行舟 2024-10-06 18:41:12

试试这个:

$finaloutput = '';
if($cr1) $finaloutput .= "This info is present! ";
if($cr2) $finaloutput .= "Number two is present! ";
if($cr3) $finaloutput .= "Three was provided! ";
echo $finaloutput;

如果 if 语句中只有一项,则不需要在其中使用方括号。我不确定你是否想要在变量中使用逗号......

Try this:

$finaloutput = '';
if($cr1) $finaloutput .= "This info is present! ";
if($cr2) $finaloutput .= "Number two is present! ";
if($cr3) $finaloutput .= "Three was provided! ";
echo $finaloutput;

You dont need brackets in an if statement if there is only one item inside it. Im not sure if you want commas in the variable though...

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