从几年前开始,我就一直想让我的PHP代码片段变得更有用,以便也能使用某种IF语句。但我发现它太复杂了,所以今天我推动我最终寻求帮助。
所以我需要一些编码的帮助,如果数据是空的,请隐藏变量周围的文本,但这段代码中有一些扭曲,我无法解决。我已经尝试让我的代码片段尽可能的干净,所以希望它仍然能保持干净。
请容忍我,因为我的PHP知识非常有限。
这段代码是输出给定信息的javascript的一部分。
{
title: "This is it",
file: "thisisit.mp4",
image: "happy.jpg",
description: "some text right here",
},
我已经成功地将这部分javascript封装在PHP中,这样我就可以使用变量来代替,并将信息存储在外部的其他地方。
echo '
{
title: "'.$data[2].' - '.$data[3].' ('.$data[0].') <img src=\"'.$data[10].'\"/>",
file: "'.$data[6].'",
image: "'.$data[9].'",
description: "'.$data[4].' <br><a href='.$data[8].'>Read more</a> ",
},
';
上面的代码按照它应该的方式工作。如果填充了任何变量,则它将按原样显示,如果为空,则不会输出任何内容。但现在我希望在变量周围有一个模板文本或html,以便只有在变量中有数据时才显示。
以data8为例。数据将只包含一个用于读取更多内容的URL。只要有一个URL就可以了,但是如果没有URL,那么它只会显示一个没有URL的read more链接。因此,如果数据是空的,那么我不想显示任何与该数据变量相关的文本或链接。
我试过使用IF语句,但我不知道这是不是正确的方法,或者我只是不知道如何正确实现它。无论我做什么,都会以错误告终。
像这样的试错,仍然在相同的回声中
echo '
{
title: "'.$data[2].' - '.$data[3].' ('.$data[0].') <img class=\"jwTitleIcon\" src=\"'.$data[10].'\"/>",
file: "'.$data[6].'",
image: "'.$data[9].'",
description: "'.$data[4].' '.if($data[8]) else{.'<br><a href='.$data[8].'</a>} ",
},
';
或者像这样的试错,分离到另一个回声
echo '
{
title: "'.$data[2].' - '.$data[3].' ('.$data[0].') <img class=\"jwTitleIcon\" src=\"'.$data[10].'\"/>",
file: "'.$data[6].'",
image: "'.$data[9].'",
description: "'.$data[4].' ';
if(empty($data[8]) else { echo ' <br><a href='.$data[8].'>Read more</a> } ",
},
';
或者这个(在someone else's question上找到),它没有给出错误,但不知何故代码破坏了一些东西,因为html输出是空白的。可能会弄乱javascript,因为它非常敏感。
echo '
{
title: "'.$data[2].' - '.$data[3].' ('.$data[0].') <img class=\"jwTitleIcon\" src=\"'.$data[10].'\"/>",
file: "'.$data[6].'",
image: "'.$data[9].'",
description: "'.$data[4].' ';
echo !empty($data[8]) ? '<br><a href='.$data[8].'>Habba hej</a> ':' ",
},
';
感谢你的帮助。谢谢
发布于 2018-05-24 07:14:58
最好使用json_encode()
将PHP值转换为Javascript文本,而不是使用您自己的echo
语句。
在这种情况下,您可以使用第三运算符。
$data2 = array(
'title' => "$data[2] - $data[3] ($data[0]) <img class='jwTitleIcon' src='$data[10]'>",
'file' => data[6],
'image' => $data[9],
'description' => $data[4] . (!empty($data[8]) ? "<br><a href='$data[8]'>Habba hej</a>" : "")
);
echo json_encode($data2) . ",";
发布于 2018-05-24 07:17:54
正如Barmar所说,使用json_encode会更容易。
此函数将数组作为参数,并输出相应的json。
因此,基本上,您需要做的是创建一个数组,其中键是对象中的键。
我将用你的例子(描述)做一个例子,让你适应剩下的部分。
$output = []; // We start with an empty array
// Let's consider that there is a description
$output['description'] = $data[4];
if(!empty($data[8])) { // If there is a link at $data[8], append it to the description
$output['description'] .= '<br><a href='; // append the html
$output['description'] .= $data[8]; // append the link
$output['description'] .= '>Read more</a>'; // append the end of the html
// One line equivalent :
// $output['description'] .= '<br><a href=' . $data[8] . '>Read more</a>';
}
// Then you echo the json_encoded output
echo json_encode($output);
另一种选择是使用if-else语句:
$output = []; // We start with an empty array
// Let's consider that there is a description
if(!empty($data[8])) { // If there is a link at $data[8], set the description with a link
$output['description'] = $data[4] . '<br><a href=' . $data[8] . '>Read more</a>';
} else { // Else = there is no link at $data[8]
$output['description'] = $data[4];
}
// Then you echo the json_encoded output
echo json_encode($output);
最后一个选项是使用第三个条件,它基本上是一个“给出一个值”的条件。尽管我认为这在这种情况下不是最好的选择,因为我认为指令会变得相当长/难以阅读:
$output = []; // We start with an empty array
// Let's consider that there is a description
$output['description'] = $data[4] . (!empty($data[8]) ? ('<br><a href=' . $data[8] . '>Read more</a>') : ''); // We add the link if there is a link at $data[8]
// Then you echo the json_encoded output
echo json_encode($output);
我刚刚看到在您的代码片段的末尾有一个",“。因此,我认为您应该使用数组的数组,并在其上使用json_encode。例如:
function snippet(&$json, $data) // The reason to put &$sjon and not $json is because we need to have a parameter passed by reference
{
// HERE PLACE THE CODE OF YOUR SNIPPET
$output = [];
/* ... add the pieces of information into the output */
$json[] = $output; // We add the $output to our array
}
$datas = [/*... array of $data*/];
$json = []; // Create the empty array
foreach ($datas as $data) {
snippet($json, $data); // Call the snippet
}
echo json_encode($json);
https://stackoverflow.com/questions/50498689
复制相似问题