我有一个脚本,我正在工作,有两个基本的输入标签。其中一个名为“seasure”,另一个名为“epset”,当您选择一个季节(如第2季)时,它将通过使用Java脚本将显示样式更改为block来显示“seasure”输入标记。然后,对“插曲输入”标签执行同样的操作,但这次它显示了一个带有单击事件链接的按钮。
我试图使它更容易添加到每个按钮的链接。目前我有它的设置,所以有30个'dls1e'部分在MySQL中,其中包含下载链接。
我现在面临的问题是,这是一个巨大的痛苦,必须作出大量的如果声明。
代码:
//Season Changer Script
if($seasonscript!='') {echo $seasonscript;} else {echo '<p>An error has occured. (Error: #01 - No season script is present for this show.)</p>';}
//Episodes Script
if($episodescript!='') {echo $episodescript;} else {echo '<p>An error has occured. (Error: #02 - No episode script is present for this show.)</p>';}
//720p Download Buttons
//if($l720p!='') {echo $l720p;}
?>
<form method="POST" action="" class="pure-form" style="color: #000;">
<fieldset class="pure-group">
<h2 style="color: #fff; display:inline-block">Season</h2>
<select style="display: inline-block; width: 50%;" id="selectseason" onchange="javascript:seasonChange()">
<?php if($seasoncount!='') {echo $seasoncount;} else {echo '<option>There are no seasons ready for this TV Show.</option>';} ?>
</select>
</fieldset>
<?php
if($episodescript = 'Season1') {
echo '<fieldset style="display: none;" id="season1episodes" class="pure-group">';
echo '<h2 style="color: #fff; display:inline-block">Episode</h2>';
echo '<select style="display: inline-block; width: 50%;" id="selectseason1episode" onload="javascript:season1episodesChange()" onchange="javascript:season1episodesChange()">'.$season1episodes.'</select>';
echo '<span id="s1buttons">';
if($dls1e1 != '') {echo '<button id="season1episode1" type="button" onclick="window.open(\'http://example.com?url='.$dls1e1.'&ref='.$imdbid.'\');" class="pure-button pure-input-1-2 pure-button-primary" style="display: none;">Download Episode 1</button>';}
if($dls1e2 != '') {echo '<button id="season1episode2" type="button" onclick="window.open(\'http://example.com?url='.$dls1e2.'&ref='.$imdbid.'\');" class="pure-button pure-input-1-2 pure-button-primary" style="display: none;">Download Episode 2</button>';}
if($dls1e3 != '') {echo '<button id="season1episode3" type="button" onclick="window.open(\'http://example.com?url='.$dls1e3.'&ref='.$imdbid.'\');" class="pure-button pure-input-1-2 pure-button-primary" style="display: none;">Download Episode 3</button>';}
if($dls1e4 != '') {echo '<button id="season1episode4" type="button" onclick="window.open(\'http://example.com?url='.$dls1e4.'&ref='.$imdbid.'\');" class="pure-button pure-input-1-2 pure-button-primary" style="display: none;">Download Episode 4</button>';}
if($dls1e5 != '') {echo '<button id="season1episode5" type="button" onclick="window.open(\'http://example.com?url='.$dls1e5.'&ref='.$imdbid.'\');" class="pure-button pure-input-1-2 pure-button-primary" style="display: none;">Download Episode 5</button>';}
if($dls1e6 != '') {echo '<button id="season1episode6" type="button" onclick="window.open(\'http://example.com?url='.$dls1e6.'&ref='.$imdbid.'\');" class="pure-button pure-input-1-2 pure-button-primary" style="display: none;">Download Episode 6</button>';}
if($dls1e7 != '') {echo '<button id="season1episode7" type="button" onclick="window.open(\'http://example.com?url='.$dls1e7.'&ref='.$imdbid.'\');" class="pure-button pure-input-1-2 pure-button-primary" style="display: none;">Download Episode 7</button>';}
if($dls1e8 != '') {echo '<button id="season1episode8" type="button" onclick="window.open(\'http://example.com?url='.$dls1e8.'&ref='.$imdbid.'\');" class="pure-button pure-input-1-2 pure-button-primary" style="display: none;">Download Episode 8</button>';}
if($dls1e9 != '') {echo '<button id="season1episode9" type="button" onclick="window.open(\'http://example.com?url='.$dls1e9.'&ref='.$imdbid.'\');" class="pure-button pure-input-1-2 pure-button-primary" style="display: none;">Download Episode 9</button>';}
if($dls1e10 != '') {echo '<button id="season1episode10" type="button" onclick="window.open(\'http://example.com?url='.$dls1e10.'&ref='.$imdbid.'\');" class="pure-button pure-input-1-2 pure-button-primary" style="display: none;">Download Episode 10</button>';}
if($dls1e11 != '') {echo '<button id="season1episode11" type="button" onclick="window.open(\'http://example.com?url='.$dls1e11.'&ref='.$imdbid.'\');" class="pure-button pure-input-1-2 pure-button-primary" style="display: none;">Download Episode 11</button>';}
if($dls1e12 != '') {echo '<button id="season1episode12" type="button" onclick="window.open(\'http://example.com?url='.$dls1e12.'&ref='.$imdbid.'\');" class="pure-button pure-input-1-2 pure-button-primary" style="display: none;">Download Episode 12</button>';}
if($dls1e13 != '') {echo '<button id="season1episode13" type="button" onclick="window.open(\'http://example.com?url='.$dls1e13.'&ref='.$imdbid.'\');" class="pure-button pure-input-1-2 pure-button-primary" style="display: none;">Download Episode 13</button>';}
if($dls1e14 != '') {echo '<button id="season1episode14" type="button" onclick="window.open(\'http://example.com?url='.$dls1e14.'&ref='.$imdbid.'\');" class="pure-button pure-input-1-2 pure-button-primary" style="display: none;">Download Episode 14</button>';}
if($dls1e15 != '') {echo '<button id="season1episode15" type="button" onclick="window.open(\'http://example.com?url='.$dls1e15.'&ref='.$imdbid.'\');" class="pure-button pure-input-1-2 pure-button-primary" style="display: none;">Download Episode 15</button>';}
if($dls1e16 != '') {echo '<button id="season1episode16" type="button" onclick="window.open(\'http://example.com?url='.$dls1e16.'&ref='.$imdbid.'\');" class="pure-button pure-input-1-2 pure-button-primary" style="display: none;">Download Episode 16</button>';}
if($dls1e17 != '') {echo '<button id="season1episode17" type="button" onclick="window.open(\'http://example.com?url='.$dls1e17.'&ref='.$imdbid.'\');" class="pure-button pure-input-1-2 pure-button-primary" style="display: none;">Download Episode 17</button>';}
if($dls1e18 != '') {echo '<button id="season1episode18" type="button" onclick="window.open(\'http://example.com?url='.$dls1e18.'&ref='.$imdbid.'\');" class="pure-button pure-input-1-2 pure-button-primary" style="display: none;">Download Episode 18</button>';}
if($dls1e19 != '') {echo '<button id="season1episode19" type="button" onclick="window.open(\'http://example.com?url='.$dls1e19.'&ref='.$imdbid.'\');" class="pure-button pure-input-1-2 pure-button-primary" style="display: none;">Download Episode 19</button>';}
echo '</span></fieldset>';
}
?>
</form>
从代码中可以看到,im必须有30个if语句,以检查dls1e中是否有下载链接,如果有,则输出一个按钮并向其附加一个链接。
有没有办法让它使用更少的if语句来检查,或者能够使用更少的MySQL列来将不同的链接应用到不同的按钮?
大约2-3个月前,我有一个脚本为我做了这件事,但不知道发生了什么,我找不到我从哪里得到它。
如果有人能帮我解决这个问题,我将不胜感激。
更新:
我删除了19部dls1e中的I had,并将第30部改名为s1集。如图所示:https://gyazo.com/aed3cd627f173f01c8871dd64ae7aa16
然后我在第1集中使用了explode():
$s1ep = explode("~", $s1episodes);
所以现在s1ep[0]是link 1(dls1e1),s1ep[1]是link 2(dls1e2),等等,我现在需要的只是一个循环,基本上做19 if语句正在做的事情。
最佳答案
最后我不明白你的问题是什么
但是要去掉19个(为什么你在你的文章里写了30个?)您可以将其替换为常规循环:
for ($i=1;$i<=19; $i++) {
if(${'dls1e'.$i} != '') {
echo '<button id="season1episode'.$i.'" type="button" onclick="window.open(\'http://example.com?url='.$dls1e.$i.'&ref='.$imdbid.'\');" class="pure-button pure-input-1-2 pure-button-primary" style="display: none;">Download Episode '.$i.'</button>';
}
}
更新
foreach ($s1ep as $key=>$value ) {
echo '<button id="season1episode'.($key+1).'" type="button" onclick="window.open(\'http://example.com?url='.$value.'&ref='.$imdbid.'\');" class="pure-button pure-input-1-2 pure-button-primary" style="display: none;">Download Episode '.($key+1).'</button>';
}