【问题标题】:PHP and MYSQL: Insert input select with mysql data and show all other fields when change selectionPHP 和 MYSQL:使用 mysql 数据插入输入选择并在更改选择时显示所有其他字段
【发布时间】:2018-09-24 09:03:51
【问题描述】:

我有这种情况:

我的数据库的表叫做 reteconsolare 并且有这些字段:

身份证 斯塔托 西塔 印迪里佐 电话 电子邮件 网址 马帕 Descrizione(这不是真正的展示。我仅用于在 Sito 和 Mappa 链接上显示图像的标题)

此时一切正常,只是当“Stato”有更多“Citta”时,我希望在“Citta”列中出现一个输入选择,其中包含从 A 订购的 Stato 的所有可用名称到 Z 并在更改选择时,还应显示正确的 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 来解决这个问题,但目前我无法学习它......提前谢谢。

【问题讨论】:

  • 你可以查看我的答案是否适合你

标签: php mysql ajax html-select


【解决方案1】:

您可以做一件事:将 "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);
}?>

希望这对你有用。

【讨论】:

  • 谢谢。我复制了您修改过的代码,但没有任何改变......它仍然在列中显示 2 次相同的 Stato,然后将相应的数据分开。
  • @RobertoN 你能在这里粘贴两个 stato 名称吗
  • 您好,例如:奥地利有唯一的城市维也纳,而我有比利时,目前有 2 个城市 (Citta),即布鲁塞尔和卡莱罗伊。我希望当同一个州(Stato)出现 1 次以上时,亲属城市会显示在选项选择中,或者显示与亲属数据相互包裹,而不重复州名......
  • @RobertoN 在这种情况下 PHP 是不够的,你还必须使用 jQuery。
  • 我在想类似的事情......谢谢
【解决方案2】:

检查while中的语法:

 while ($row = $query->fetch_assoc())  { 
...
} while ($row_news = $query->fetch_assoc());

正确的语法:

while(condition) {
...
}

do {
...
} while(condition)

【讨论】:

    猜你喜欢
    • 2011-10-16
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 2012-07-02
    • 2015-07-30
    • 1970-01-01
    相关资源
    最近更新 更多