如何完全自动化此代码?

时间:2014-08-15 09:22:02

标签: php jquery

<?php
if(isset($_GET['kaubanimetus1']))
{
?>
<script>
<?php 
$i = 1;
while ($i <= 10):
     ?> 
    $('.lisa<?php echo $i; ?>').click("keyup change on",function()
      {    
      //alert('tere');    
       var kood<?php echo $i; ?> =  $( "#code<?php echo $i; ?>" ).text();                          
       var kaup<?php echo $i; ?> =  $( "#kaup<?php echo $i; ?>" ).text(); 
       var hind<?php echo $i; ?> =  $( "#price<?php echo $i; ?>" ).text();
       var kogus<?php echo $i; ?> = $( "#amount<?php echo $i; ?>" ).text();

       document.getElementById("kaubanimetus1").value = kaup<?php echo $i; ?>; 
       document.getElementById("hind1").value = hind<?php echo $i; ?>; 
       document.getElementById("kogus1").value = kogus<?php echo $i; ?>; 
       document.getElementById("kaubakood1").value = kood<?php echo $i; ?>; 
       $( "#kaubanimetus1" ).focus(); 
      }); 
<?php
    $i++;
endwhile;
  ?>
</script>

<?php     
$nimetus = str_replace("-", " ", $_GET['kaubanimetus1']);

$select_toode = $sql->query("SELECT * FROM store WHERE (`nimetus` LIKE '%".$nimetus."%') AND firma = '{$_COOKIE['firmad']}'") or die(mysql_error());

 echo '<div class="table-responsive"><table class="table"><thead>
     <tr><td colspan="4"><span class="kaubanimetus1" style="cursor:pointer;">[X]</span></td></tr>
 <tr><th>Kauba kood</th><th>Kauba nimetus</th><th>Kaupa laos</th><th>Hind</th></tr></thead>';
     echo '<tbody>';
     $i = 1;
while($print_toode = $select_toode->fetch_assoc())
  {
    echo '
    <tr>
    <td><div style="cursor:pointer;"'; echo 'id="code'.$i.'" class="lisa'.$i.'"'; echo'>'.$print_toode['kaubakood'].'</div></td>
    <td><div style="cursor:pointer;"'; echo 'id="kaup'.$i.'" class="lisa'.$i.'"'; echo'>'.$print_toode['nimetus'].'</div></td>
    <td><div style="cursor:pointer;"'; echo 'id="amount'.$i.'" class="lisa'.$i.'"'; echo'>'.$print_toode['kogus'].'</div></td>
    <td><div style="cursor:pointer;"'; echo 'id="price'.$i.'" class="lisa'.$i.'"'; echo'>'.$print_toode['price1'].'</div></td>
    </tr>';
  $i++;
  }
     echo '</tbody>';
   echo '</table></div>';
}

现在我使用PHP代码再创建100次,但我有

if(isset($_GET['kaubanimetus2']))
{}
and
if(isset($_GET['kaubanimetus3']))
{} 

等。等达15次, 但我希望这可以自动更改。重复一遍,而不必创建if语句?

有人可以帮我恢复正常吗?因为我无法弄清楚

提前致谢

抱歉英语不好

2 个答案:

答案 0 :(得分:1)

使用for循环?

for($kindex=1;$kindex<=15;$kindex++) {
    if(isset($_GET['kaubanimetus'.$kindex]))
    { /* ... */ }
}

答案 1 :(得分:1)

herr的一些伪代码来演示可能有用的模型

$parameterList = array('kaubanimetus1','kaubanimetus2','kaubanimetus3');
for ($i = 0; $i <= count($parameterList); $i++) {
    if(isset($_GET[$parameterList[$i]))
    {
        $value = anything_that_use_the_name($parameterList[$i]);
    }
}