-1

I did a form which adds record to database and it shows in HTML table. One of the column in form and database is "country". I would like to insert a right flag icon beside the "country" column and I'd like to make it from form. I added example photo

example photo

Could you help me with any idea?

Form

<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style2.css">
</head>
<body>

<?php

if (isset($_GET["date"]) ){
    $date = $_GET["date"];
    $country = $_GET["country"];
    $city = $_GET["city"];
    $place = $_GET["place"];


    if( empty ( $date ) || empty ($country) || empty ($city) || empty ($place)){
        echo "Wypelnij wszystkie pola";
    } else {

            $conn = new mysqli("localhost", "root", "", "tour");
            $odp = $conn->query("INSERT INTO trasy (date, country, city, place)
        VALUES ('$date', '$country', '$city', '$place')");
        header("Location: index5.php");


        if ($odp) {
            echo "Dodano koncert";
        }else{
            echo "Nie udalo sie dodac";
        }
        $conn->close();
    }

}

?>
    <div id="login">
    <form method="GET" action="index5.php">
    <table id="customers">
    <input name="date" type="date" placeholder="Date..."><br>
    <input name="country" type="text" placeholder="Country..."><br>
    <input name="city" type="text" placeholder="City..."><br>
    <input name="place" type="text" placeholder="Place..."><br>


    <input type="submit" value="OK">
    </table>

    </form>
    </div>

</body>
</html>

HTML table

<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style3.css">
</head>
<body>
<table id="customers">
          <tr>
        <th>Date</th>
        <th>Country</th>
        <th>City</th>
        <th>Place</th>
        <th>Info</th>
        </tr>
<?php 
    $conn = new mysqli("localhost", "root", "", "tour") 
    or die ("Błąd");
    $wynik = $conn->query("SELECT * FROM trasy ORDER BY date DESC");

    if ($wynik->num_rows > 0){

        while ( $wiersz = $wynik->fetch_assoc() ){
            echo "<tr>";
            echo "<td>" . $wiersz["date"] . "</td>";
            echo "<td>" . $wiersz["country"] . "</td>";
            echo "<td>" . $wiersz["city"] . "</td>";
            echo "<td>" . $wiersz["place"] . "</td>";
            echo "<td>" . $wiersz["info"] . "</td>";

            echo "</tr>";
        }
        echo "</table>";
    } else {
        echo "Nic tu nie ma";
    }
    $conn->close();

?>
</table>


</body>
</html>
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

1 Answers1

0

I will address only your question but keep in mind you have all sort of wrong things with your code starting with security.

I will just point you in direction what I think is best for you to go.

First of all upload all flags in one directory on server. You can use img or you can use CSS svg like this one for example.

Second, don't make people type there country in input field at all (that way you make sure values for same country will be the same), make that a drop-down with pre-populated all countries and assign a value to each.

Example of value for CSS icons that I provided link for: If user chooses Germany from drop-down you store in country field value: gr.

That way when you call your country you can do that like this:

echo '<td><span class="flag-icon flag-icon-' . $wiersz["country"] . '"></span></td>';

Which will produce for Germany:

<td><span class="flag-icon flag-icon-gr"></span></td>

You can do same thing just with HTML img tags. Just storing the flag value as img src.

I believe this is best way to go. Other that that you would need to have a whole lot of PHP switch statements.

EDIT: Additionally if you want to have country name and flag store both of them as value in select and on database input separate it and store it in two columns like this:

<select id="country">
  <option value="Croatia | hr">Croatia</option>
  <option value="Germany | gr">Germany</option>
<select>

$country  = mysqli_real_escape_string($_GET['country']);
$pieces = explode(" | ", $country);
$countryname = $pieces[0]; // piece1
$flag = $pieces[1]; // piece2   
ikiK
  • 6,328
  • 4
  • 20
  • 40