Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Empfohlene Antworten

Veröffentlicht

Moin zusammen,

 

ich habe angefangen mir PHP anzuschauen und wollte mir eine Kontaktbuch Homepage erstellen, dort habe ich einmal eine Seite "Kontakte" und "Kontakt hinzufügen".

Unter "Kontakt hinzufügen" soll der Namen und die dazugehörige Telefonnummer eingeben können, diese werden dann in einer .txt-Datei "contacts" im JSON-Format gespeichert.

Unter "Kontakte" sollen die Namen aus der .txt-Datei ausgelesen und angezeigt werden.

Allerdings werden mir weder Namen, noch eine Fehlermeldung angezeigt.. Kann mir jemand sagen wo mein Fehler liegt?

Vielen Dank im Voraus! 

 

<!-- Main -->
    <div class="main">
        <div class="nav_bar">
            <a href='index.php?page=start'><img src="img/home.svg" >Startseite</a>
            <a href='index.php?page=contacts'><img src="img/person.svg" >Kontakte</a>
            <a href='index.php?page=add'><img src="img/add_person.svg" >Kontakt hinzufügen</a>
            <a href='index.php?page=legal'><img src="img/policy.svg" >Impressum </a>
        </div>
        <div class="main_section">


<!-- PHP  -->
            <div class="welcome">
            <?php
			$contacts = [];
                $headline = 'Herzlich Willkommen!';
                $headtext = 'Willkommen in deinem Kontaktbuch';

                if ($_GET['page'] == 'contacts') {
                    $headline = 'Kontakte';
                    $headtext = 'Entdecke deine Kontakte.';
                }
                if ($_GET['page'] == 'add') {
                    $headline = 'Kontakt hinzufügen';
                    $headtext = 'Füge neue Kontakte hinzu.';
                }
                if ($_GET['page'] == 'legal') {
                    $headline = 'Impressum';
                    $headtext = 'Hier findest du alles über uns.';
                }

                echo '<h1>'.$headline.'</h1>';
                echo '<p>'.$headtext.'</p>';
    
              
/* add contacts */
                if ($_GET['page'] == 'add') {
                    
                    echo"
                    <form action='?page=add' method='POST'>
                            Gebe einen Namen ein:
                        <br>
                            <input placeholder='Name' name='name'>
                        <br>
                            Gebe eine Telefonnummer ein:
                        <br>
                            <input placeholder='Telefonnummer' name='number'>
                        <br>
                            <button type='submit'>Absenden</button>
                            <br>
                            <br>
                    </form>
                    ";
                    
                
                if (file_exists('contacts.txt')) {
                    $text = file_get_contents('contacts.txt', true);
                    $contacts = json_decode($text, true); 
                }

                if (isset($_POST['name']) && isset($_POST['number'])) {
                        $newcontact = [
                            'name' => $_POST['name'],
                            'number' => $_POST['number']
                        ];
                        array_push($contacts, $newcontact);
                        file_put_contents('contacts.txt', json_encode($contacts, JSON_PRETTY_PRINT));
                        echo 'Der Kontakt <b>' . $_POST['name']. '</b> wurde hinzugefügt!';
                    }
                }  

/* show contacts */
                if ($_GET['page'] == 'contacts') {
                    foreach ($contacts as $row) {
                        echo '<div>' . $row['name'] . '</div>';
                    }
                }
            ?>
            </div>
        </div>
    </div>

 

  • Autor

Ich habe den Code wie folgt angepasst, jetzt funktioniert es.

                if ($_GET['page'] == 'contacts') {
                    if (file_exists('contacts.txt')) {
                            $text = file_get_contents('contacts.txt', true);
                            $contacts = json_decode($text, true); 
                    }  
                foreach ($contacts as $row) {
                        echo '<div>' . $row['name'] . '</div>';
                    }
                }

 

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.