我在选择选项和价值时遇到问题

时间:2020-10-29 11:50:46

标签: php html

单击按钮后,我尝试保留所选选项中的值。 现在,我已经使用输入(范围,文本)完成了此操作,并且可以正常工作,但是我无法确定如何使用选择选项来完成此操作。

其他事物(您必须创建它们才能运行它)

c13ustawienia.php

<?php
  $serwer='localhost';
  $uzytk='root';
  $haslo='';
  $baza='komis';
?>

c13dane.txt

1993|Volkswagen|Passat|19000 
 
1973|Opel|Blitz|12000 
 
1997|Volkswagen|Passat|17000 
 
2010|Mercedes|M5|29000 
 
2001|Volkswagen|Passat|29000 
 
1990|Volkswagen|Passat|23000 
 
2018|Tesla|Super|129000 
 
2018|sla|Super|9000 
 
1992|Volkswagen|Passat|10000 
 
2006|Audi|B9|74000 
 
2009|Volkswagen|Passat|89000 
<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
  <meta charset="UTF-8"> 
 
  <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
 
  <title>Baza</title> 
 
  <style> 
 
    table {border-collapse: collapse;} 
 
    td,th {border: 1px blue solid;} 
 
    th {background-color: azure;} 
 
    .id {width: 20px; text-align: center;} 
 
    .mar {width: 90px;} 
 
    .mod {width: 70px;} 
 
    .rok {width: 40px; text-align: right;} 
 
    .cena {width: 50px; text-align: right;} 
 
    .zolty {background-color: yellow;} 
 
    .pomar {background-color:orange;} 
 
    [type=text] {width:60px;} 
 
    header {height: 60px; background-color:greenyellow;} 
 
    header>img {height: 75%; text-align: center;}     
 
    nav {height: 400px; width: 30%; background-color:khaki; 
 
         float: left;} 
 
    main {height: 400px; width: 70%; background-color:moccasin; 
 
          float: left;} 
 
    footer {height: 40px; background-color: powderblue; 
 
            clear: both; text-align: center; color:blue;} 
 
  </style> 
 
  <script> 
 
    function wartosc() { 
 
      min=document.getElementById('cmin'); 
 
      max=document.getElementById('cmax'); 
 
      wmin=document.getElementById('wmin'); 
 
      wmax=document.getElementById('wmax'); 
 
      minint=parseInt(min.value); 
 
      maxint=parseInt(max.value); 
 
      if(maxint<minint) 
 
        maxint=minint+1; 
 
      wmin.value=minint; 
 
      min.value=minint; 
 
      wmax.value=maxint; 
 
      max.value=maxint; 
 
    } 
 
  </script> 
 
</head> 
 
<body> 
 
  <?php 
 
  function tworz_baze() { 
 
    require('c13ustawienia.php'); 
 
    $link=mysqli_connect($serwer, $uzytk, $haslo); 
 
    mysqli_query($link, "DROP DATABASE $baza"); 
 
    mysqli_query($link, "CREATE DATABASE $baza"); 
 
    mysqli_query($link, "USE $baza"); 
 
    mysqli_query($link, "CREATE TABLE auta ( 
      ID int(8) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
      marka varchar(20), 
      model varchar(25), 
      rok int(4), 
      cena double)"); 
 
    return $link; 
 
  }  // tworz_baze() 
 
   
 
  function czytajdane($plik) { 
 
    $f=fopen($plik, 'r'); 
 
    while(!feof($f)) { 
 
      $linia=rtrim(fgets($f)); 
 
      if(strlen($linia)>5) 
 
        $tab[]=explode('|', $linia); 
 
    } 
 
    return $tab; 
 
  }  // czytajdane($plik) 
 
 
 
 
  function dobazy($link, $tablica) { 
 
    foreach ($tablica as $sam) { 
 
      list($rok, $mar, $mod, $cena)=$sam; 
 
      mysqli_query($link, "INSERT INTO auta VALUES 
 
         (NULL, '$mar', '$mod', $rok, $cena)"); 
 
    } 
 
  }  // dobazy($link, $tablica) 
 
 
 
 
  function pisz($li, $marka, $cenamin, $cenamax) { 
 
    echo "<h3>Wybrano:<br>marka: $marka<br> 
 
          zakres cen: $cenamin - $cenamax zł</h3>"; 
 
    echo "<table> 
 
      <tr><th>id</th><th>marka</th><th>model</th> 
 
      <th>rok</th><th>cena</th></tr>"; 
 
    $wyn=mysqli_query($li, "SELECT * FROM auta WHERE 
 
        marka='$marka' AND cena>=$cenamin AND cena<=$cenamax"); 
    $licznik=FALSE; 
    while($wiersz=mysqli_fetch_array($wyn)) { 
      list($id, $mar, $mod, $rok, $cena)=$wiersz; 
      $kolor = $licznik ? 'zolty' : 'pomar'; 
      echo "<tr class=\"$kolor\"><th class=\"id\">$id</td> 
 
                <td class=\"mar\">$mar</td> 
 
                <td class=\"mod\">$mod</td> 
 
                <td class=\"rok\">$rok</td> 
 
                <td class=\"cena\">$cena</td></tr>"; 
 
      $licznik=!$licznik; 
 
    } 
 
    echo '</table>'; 
 
    mysqli_close($li); 
 
  }  // pisz($li, $model, $cenamax) 
 
 
 
 
  function filtry() { 
 
    if(isset($_GET['cmin'])) 
 
      $tab['cmin']=$_GET['cmin']; 
 
    else 
 
      $tab['cmin']=0; 
 
    if(isset($_GET['cmax'])) 
 
      $tab['cmax']=$_GET['cmax']; 
 
    else 
 
      $tab['cmax']=CENAMAKS; 
 
    if(isset($_GET['marka'])) 
 
      $tab['marka']=$_GET['marka']; 
 
    else 
 
      $tab['marka']='Volkswagen'; 
 
    return $tab; 
 
  } // filtry() 
 
 
 
 
  function lista($link) { 
 
    $w=mysqli_query($link, "SELECT DISTINCT marka  
 
                            from auta ORDER BY marka"); 
 
    while($m=mysqli_fetch_array($w)) 
 
      echo '<option value="'.$m['marka'].'">' 
 
            .$m['marka'].'</option>'; 
 
          //  $x=$m['marka']; 
 
            // "<option value=\"$x\">.... 
 
  }  // lista($link) 
 
 
 
 
  ?> 
 
<header> 
 
  <img src="auto.png" alt="auto"> 
 
  <span>Komis samochodowy</span> 
 
</header> 
 
 
 
 
<nav> 
 
  <h3>Filtry:</h3> 
 
  <form action="c41.php" method="GET"> 
 
    Cena:<br> 
 
    od: <input type="range" name="cmin" id="cmin"  
 
          min="0" max="<?php echo CENAMAKS ?>"            value="<?php echo $tf['cenamin'];?>" 
 
 
          onchange="wartosc()">
          <br> 
 
    do :<input type="range" name="cmax" id="cmax"  
 
           min="0" max="<?php echo CENAMAKS ?>"  
 
           value="<?php echo $tf['cenamin'];?>" 
 onchange="wartosc()">
 <br> 
 
    <input type="text" name="wmin" id="wmin" disabled  
 
    value="<?php echo $tf['cenamin'];?>" 
> -  
 
    <input type="text" name="wmax" id="wmax" disabled 
 
    value="<?php echo $tf['cenamax'];?>" 
><br> 
 
    <select name="marka" id="marka">
 
 
      <?php lista($li); ?> 
    </select> 
 
    <input type="submit" value="Filtruj"> 
 
    <input type="reset" value="Czyść"> 
 
  </form> 
 
</nav> 
 
   
 
<main> 
 
  <?php pisz($li, $tf['marka'], $tf['cmin'], $tf['cmax']); ?> 
 
</main> 
 
 
 
 
<footer> 
 
  Adam Kowal &copy; 
 
</footer> 
 
</body> 
 
</html> 
 

要使输入正常工作,我给了它们功能可变的变量,并指向数据库的右键以获取所需的内容,但是我不知道如何使其与选择选项一起工作

可让我在输入中获得所需代码的代码框架:value="<?php echo $tf['cenamin'];?>"

1 个答案:

答案 0 :(得分:0)

使用以下命令更改代码: 首先,将一个新参数添加到“ lista”函数中,以便能够标记选定的值,例如

function lista($link, $selected = "default") {
   // function code here
}

第二,修改函数以尊重传递的值,并将其与从数据库中获得的值相匹配:

while($m=mysqli_fetch_array($w)) {
  $status = "";
  if ($selected == $m['marka']) $status = "selected";
  echo '<option '.$selected.' value="'.$m['marka'].'">' .$m['marka'].'</option>'; 
}

第三,将选定的值传递给代码中的函数,例如:

<?php lista($li, $_GET['marka']); ?>

NB!您不应该在任何生产环境中使用当前代码:它包含多个SQL注入,并且不是由最佳实践构建的(例如,将html与程序代码分离等)。