I have this situation:
The table of my database is called reteconsolare and have these fields:
ID Stato Citta Indirizzo Telefono Email URL Mappa Descrizione (This is not really show. I use it only for showing the title of the images on Sito & Mappa links)
In this moment all works ok, just that When there are more "Citta" for "Stato" I wish that in the "Citta" colum would appear an input select with all the available name of the Citta for Stato ordering from A to Z and when changing the selection, should also show the correct Indirizzo, Telefono, Email, URL, Mappa.
Following is my actual code that repeat the values.
eg.:
Belgio | Bruxelles
Belgio | Charleroi
<?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);
}?>
I searched also an alternative way: not repeating the name of the Stato and Wrap every city with its data. I tried to play with mysql's group by... but without results. And anyway don't really like too much this solution. I think that i need to know maybe also Ajax to solve this problem, but at the moment i can't learn it...Thanks in advance.
Check syntax in while:
while ($row = $query->fetch_assoc()) {
...
} while ($row_news = $query->fetch_assoc());
Correct syntax:
while(condition) {
...
}
or
do {
...
} while(condition)
You can do one thing: store "Stato" in array
and check if the value already exist in array
or not. For i.e. :
<?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);
}?>
Hope this will work for you.