首页 > 解决方案 > PHP 和 MYSQL:使用 mysql 数据插入输入选择并在更改选择时显示所有其他字段

问题描述

我有这种情况:

我的数据库表称为 reteconsolare 并具有以下字段:

ID Stato Citta Indirizzo Telefono 电子邮件 URL Mappa Descrizione (这不是真正的节目。我只用它来显示 Sito 和 Mappa 链接上的图像标题)

在这一刻一切正常,只是当“Stato”有更多“Citta”时,我希望在“Citta”列中出现一个输入选择,其中包含从 A 到 Z 排序的 Stato 的所有可用名称和更改选择时,还应显示正确的 Indirizzo、Telefono、Email、URL、Mappa。

以下是我重复这些值的实际代码。

例如。:

比利时 |
比利时布鲁塞尔| 沙勒罗瓦

 <?Php
  mysqli_set_charset($connessione, 'utf-8');

    $sql = "SELECT * FROM reteconsolare ORDER BY Stato, Citta";

    $query = $connessione->query($sql) ;
  while ($row = $query->fetch_assoc())  { 
        $stato=sprintf( $row["Stato"]); 
        $citta=sprintf( $row["Citta"]);
        $indirizzo=sprintf( $row["Indirizzo"]);
        $descrizione=sprintf( $row["Descrizione"]);
        $email=sprintf( $row["Email"]);
        $telefono=sprintf( $row["Telefono"]);
        $mappa=sprintf( $row["Mappa"]);
        $link=sprintf( $row["URL"]);
        $id=sprintf( $row["ID"]);
         ?>  


         <tr>
    <td width="20%"><ts><?Php echo($stato) ?></ts></td>
    <td width="20%"><ts><?Php echo($citta) ?></ts></td>
    <td width="30%"><ts><?Php echo($indirizzo) ?></ts></td>
    <td width="10%"><ts><?Php echo($telefono) ?></ts></td>
    <td width="10%"><ts><?Php echo emailize($email) ?></ts></td>

    <td width="5%" align="center"><?Php echo(' <a target=_blank href='); echo($link);  echo('>'); ?><img src="img/www.png" width="20" height="20" title="Link al sito del <?php echo ($descrizione);?>"></a></td>
    <td width="5%" align="center"><?Php echo(' <a target=_blank href='); echo($mappa);  echo('>'); ?><img src="img/Maps.png" width="20" height="20" title="Mappa del <?php echo ($descrizione);?>"></a></td>

      </tr>
   <?php } while ($row_news = $query->fetch_assoc());?>

<?php
    function emailize($text)
{
    $regex = '/(\S+@\S+\.\S+)/';
    $replace = '<a href="mailto:$1">$1</a>';

    return preg_replace($regex, $replace, $text);
}?>

我还搜索了另一种方法:不重复 Stato 的名称,并用其数据包裹每个城市。我试图玩mysql的group by ...但没有结果。无论如何,我不太喜欢这个解决方案。我认为我可能还需要知道 Ajax 来解决这个问题,但目前我无法学习它......在此先感谢。

标签: phpmysqlajaxhtml-select

解决方案


您可以做一件事:存储“Stato”array检查该值是否已存在array。对于即:

<?Php
  mysqli_set_charset($connessione, 'utf-8');

    $sql = "SELECT * FROM reteconsolare ORDER BY Stato, Citta";

    $query = $connessione->query($sql) ;
    $Stato = array() ;
  while ($row = $query->fetch_assoc())  { 
        if(!in_array(sprintf($row["Stato"]),$Stato)){
        $stato=sprintf( $row["Stato"]); 
        $Stato = sprintf( $row["Stato"]);
        $citta=sprintf( $row["Citta"]);
        $indirizzo=sprintf( $row["Indirizzo"]);
        $descrizione=sprintf( $row["Descrizione"]);
        $email=sprintf( $row["Email"]);
        $telefono=sprintf( $row["Telefono"]);
        $mappa=sprintf( $row["Mappa"]);
        $link=sprintf( $row["URL"]);
        $id=sprintf( $row["ID"]);
         ?>  


         <tr>
    <td width="20%"><ts><?Php echo($stato) ?></ts></td>
    <td width="20%"><ts><?Php echo($citta) ?></ts></td>
    <td width="30%"><ts><?Php echo($indirizzo) ?></ts></td>
    <td width="10%"><ts><?Php echo($telefono) ?></ts></td>
    <td width="10%"><ts><?Php echo emailize($email) ?></ts></td>

    <td width="5%" align="center"><?Php echo(' <a target=_blank href='); echo($link);  echo('>'); ?><img src="img/www.png" width="20" height="20" title="Link al sito del <?php echo ($descrizione);?>"></a></td>
    <td width="5%" align="center"><?Php echo(' <a target=_blank href='); echo($mappa);  echo('>'); ?><img src="img/Maps.png" width="20" height="20" title="Mappa del <?php echo ($descrizione);?>"></a></td>

      </tr>
   <?php }} while ($row_news = $query->fetch_assoc());?>

<?php
    function emailize($text)
{
    $regex = '/(\S+@\S+\.\S+)/';
    $replace = '<a href="mailto:$1">$1</a>';

    return preg_replace($regex, $replace, $text);
}?>

希望这对你有用。


推荐阅读