Avatar billede Vivivroue Nybegynder
19. marts 2011 - 11:18 Der er 6 kommentarer og
1 løsning

PHP - Udskifte statisk HTML og statisk array med data fra databasen.

Jeg må endnu en gang ty til hjælp.........

Jeg har dette spørgsmål. Jeg forstår ikke helt hvad der menes?


Select, loop and output predefined data in form selectboxes and checkboxes
In make_reference.php you have a selectbox for towns and checkboxes for relations.
Currently the checkboxes for relations are static HTML and the town select box is built from
a static array that you loop through to output each option in the selectbox.
Your task is now to replace this static HTML and static array with selections of relevant data
from the database. The selection box for towns should be built from a database selection
and the checkboxes for relations should also be built from a database selection.
To do this you need to update your current script for building the town selectbox to do the
following: select all towns from the database and loop through the result array with while
and output the option-elements.
You also need to create a new script that replaces the current static checkboxes for
relations. The script should select all relations in the database and loop through the
selection and output an input of the type checkbox for each result in your selection.
Remember in both cases to test for successful query results before you loop and output any
data and throw an error from the mysql database if the query was not successful.
Also remember to make the selectbox and the checkboxes sticky (book p. 89).
Examples below are code samples from earlier exercise parts demonstrating the sticky
principle for selectboxes and checkboxes (of course the code should look different now that
you create this via while and looping through a database instead of with a static array and
foreach + static HTML with no loop):PHP and MySQL exercise I Dynamic Web Pages and Joomla!
Anett Korsgaard Nissen 2 of 3
Debugging and testing:
Test your scripts along the way everytime the code makes it possible - to debug your PHP.
The end result: There should be no real visual difference between before and after - other
than maybe less options in the selectbox and less relations as checkboxes depending on
what you inserted in the database.
Connection TIP: As you will be making a page with several scripts using mysql - you need
to include the connection file in the beginning of the page and close down the connection at
the end of the page instead of within each script.
When everything works try to open phpmyadmin and insert a new town and a new relation
and return to the make_reference.php page and refresh to see the new option/checkbox




Min make_reference.php ser således ud:



<?php
include ("html_fragments/header.html.php");
?>



<body>

    <div class="main">
    <!-- START LAYOUT HEADER -->
<div class="header">
            <a class="image" href="index.php" name="Home" title="Home"><img src="images/logo.gif" alt="home" /></a>
</div>
        <!-- START LAYOUT TOP LINKS -->
          <div class="toplinks">
            <div class="toplinks_cont">
        <?php include ("html_fragments/toplinks.html.php"); ?>
      </div>
      </div>
           
        <!-- START LAYOUT LINKS -->
              <div class="links">
            <div>
            <?php
include ("html_fragments/links.html.php");
?>
</div>
</div>
                 
      <!-- START LAYOUT CONTENT -->
      <div class="content">
      <div><?php

if(isset($_POST['submit'])){
   
    //test on correct datatype and formats and input data//
    //validate name//
    if((!empty($_POST['name'])) && (!is_numeric($_POST['name']))) {
        $name = $_POST['name'];
    }
    else{
    echo "You forgot to type a name (in letters)<br/>";
    }
    //validate lastname//
    if((!empty($_POST['lastname'])) && (!is_numeric($_POST['lastname']))) {
        $lastname = $_POST['lastname'];
    }
    else{
    echo "You forgot to type a lastname (in letters)<br/>";
    }
   
    //validate address//
    if(!empty($_POST['address'])) {
        $address = $_POST['address'];
    }
    else {
    echo "You forgot to type in your address<br/>";   
    }
   
    //validate zipcode//
        $errorMessage = "NULL";
        $zip = $_POST['zip'];
    if ($_POST['zip'] != $errorMessage && isset($_POST['zip'])){
        //continue form//
    }
    else {
        echo "You forgot to select your zipcode</br>";
        }
       
    //validate email//

    if(!empty($_POST['email'])) {
        $email = $_POST['email'];
    }
    else {
    echo "You forgot to type in your email<br/>";   
    }
   

//validate relation//

if (!isset($_POST['relation1']) && !isset($_POST['relation2']) && !isset($_POST['relation3']) && !isset($_POST['relation4']) && !isset($_POST['relation5']) && !isset($_POST['relation6'])) {
    echo "No relations checked</br>";   
}
else {
    //continue form//   
}

//validate day//
    $errorMessage = "NULL2";
        $day = $_POST['day'];
    if ($_POST['day'] != $errorMessage && isset($_POST['day'])){
        //continue form//
    }
    else {
        echo "You forgot to select a day</br>";
        }
//validate month//
    $errorMessage = "NULL3";
        $month = $_POST['month'];
    if ($_POST['month'] != $errorMessage && isset($_POST['month'])){
        //continue form//
    }
    else {
        echo "You forgot to select a month</br>";
        }

//validate year//
    $errorMessage = "NULL4";
        $year = $_POST['year'];
    if ($_POST['year'] != $errorMessage && isset($_POST['year'])){
        //continue form//
    }
    else {
        echo "You forgot to select a year</br>";
        }

// Validate description//
if (!empty($_POST['desc'])) {
    //continue form//
    $desc = $_POST['desc'];   
}

else {
    echo "Please write a description</br>";
}

    //validate complete form to submit//
    if(!empty($name) && !is_numeric($name) && !empty($lastname) && !is_numeric($lastname) && !empty($address) && $zip!=$errorMessage && preg_match("/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $_POST['email']) && isset($relation) && $day!=$errorMessage && $month!=$errorMessage && $year!=$errorMessage && !empty($desc) && isset($_POST['relation1']) || isset($_POST['relation2']) || isset($_POST['relation3']) || isset($_POST['relation4']) || isset($_POST['relation5']) || isset($_POST['relation6'])){
    echo "Thank you for submitting <br/>";
    }
    else{
    echo "Please fill in the form again<br/>";}
}

?>


</div>
            <div class="content_text"><?php /* put content inside this div - it ensures padding of content in content box */ ?>
                   
                    <h2>Please make a reference about me</h2>
                    <form name="referenceForm" id="referenceForm" method="post" action="#">
                        <!-- START INFORMATION -->
                        <p class="formtitle">Information about you</p>
                       
                        <fieldset>
                        <label>Name</label><input type="text" id="name" name="name" value="<?php if(isset($_POST['name'])){echo $_POST['name'];}?>" /><br />
                   
                            <label>Lastname</label><input type="text" id="lastname" name="lastname" value="<?php if(isset($_POST['lastname'])){echo $_POST['lastname'];}?>" /><br />
                           
                            <label>Address</label><input type="text" id="address" name="address" value="<?php if(isset($_POST['address'])){echo $_POST['address'];}?>" /><br />
                   
                            <label>Town</label>
                            <select name="zip" id="zip">
                                <option value="NULL" <?php echo (isset($_POST['zip']) && $_POST['zip'] == 'NULL') ? 'selected' : ''; ?>>Town</option>
                                <option value="7323" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7323') ? 'selected' : ''; ?>>7323 Give</option>
                                <option value="7330" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7330') ? 'selected' : ''; ?>>7330 Brande</option>
                                <option value="7361" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7361') ? 'selected' : ''; ?>>7361 Ejstrupholm</option>
                                <option value="7362" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7362') ? 'selected' : ''; ?>>7362 Hampen</option>
                                <option value="7400" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7400') ? 'selected' : ''; ?>>7400 Herning</option>
                               
                                <option value="7430" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7430') ? 'selected' : ''; ?>>7430 Ikast</option>
                                <option value="7441" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7441') ? 'selected' : ''; ?>>7441 Bording</option>
                                <option value="7442" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7442') ? 'selected' : ''; ?>>7442 Engesvang</option>
                                <option value="7451" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7451') ? 'selected' : ''; ?>>7451 Sunds</option>
                                <option value="7470" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7470') ? 'selected' : ''; ?>>7470 Karup</option>
                                <option value="7480" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7480') ? 'selected' : ''; ?>>7480 Vildbjerg</option>
                                <option value="7490" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7490') ? 'selected' : ''; ?>>7490 Aulum</option>
                                <option value="7500" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7500') ? 'selected' : ''; ?>>7500 Holstebro</option>
                                <option value="7540" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7540') ? 'selected' : ''; ?>>7540 Haderup</option>
                            </select><br />
                   
                            <label>Email</label><input type="text" id="email" name="email" value="<?php if(isset($_POST['email'])){echo $_POST['email'];}?>" />
                   
                      </fieldset>
                      <!-- END INFORMATION -->
                     
                      <!-- START RELATIONS -->
                      <p class="formtitle">Relations to me</p>
                     
                        <fieldset>
                          <label>Teacher</label><input class="box" type="checkbox" name="relation1"  value="teacher" <?php if (isset($_POST['relation1'])) { echo 'checked="checked"';}?>/><br />
                            <label>Student</label><input class="box" type="checkbox" name="relation2" value="student" <?php if (isset($_POST['relation2'])) { echo 'checked="checked"';}?>/><br />
                            <label>Boss</label><input class="box" type="checkbox" name="relation3" value="boss" <?php if (isset($_POST['relation3'])) { echo 'checked="checked"';}?>/><br />
                            <label>Collegue</label><input class="box" type="checkbox" name="relation4" value="collegue" <?php if (isset($_POST['relation4'])) { echo 'checked="checked"';}?>/><br />
                            <label>Friend</label><input class="box" type="checkbox" name="relation5" value="friend" <?php if (isset($_POST['relation5'])) { echo 'checked="checked"';}?>/><br />
                            <label>Family</label><input class="box" type="checkbox" name="relation6" value="family" <?php if (isset($_POST['relation6'])) { echo 'checked="checked"';}?>/><br /><br />
                           
                            <label>Known me since</label>
                            <select class="selectbox_small" name="day" id="day">
                                <option value="NULL2" <?php echo (isset($_POST['day']) && $_POST['day'] == 'NULL2') ? 'selected' : ''; ?>>Day</option>
                                <option value="1" <?php echo (isset($_POST['day']) && $_POST['day'] == '1') ? 'selected' : ''; ?>>1</option>
                                <option value="2" <?php echo (isset($_POST['day']) && $_POST['day'] == '2') ? 'selected' : ''; ?>>2</option>
                                <option value="3" <?php echo (isset($_POST['day']) && $_POST['day'] == '3') ? 'selected' : ''; ?>>3</option>
                                <option value="4" <?php echo (isset($_POST['day']) && $_POST['day'] == '4') ? 'selected' : ''; ?>>4</option>
                                <option value="5" <?php echo (isset($_POST['day']) && $_POST['day'] == '5') ? 'selected' : ''; ?>>5</option>
                                <option value="6" <?php echo (isset($_POST['day']) && $_POST['day'] == '6') ? 'selected' : ''; ?>>6</option>
                                <option value="7" <?php echo (isset($_POST['day']) && $_POST['day'] == '7') ? 'selected' : ''; ?>>7</option>
                                <option value="8" <?php echo (isset($_POST['day']) && $_POST['day'] == '8') ? 'selected' : ''; ?>>8</option>
                                <option value="9" <?php echo (isset($_POST['day']) && $_POST['day'] == '9') ? 'selected' : ''; ?>>9</option>
                                <option value="10" <?php echo (isset($_POST['day']) && $_POST['day'] == '10') ? 'selected' : ''; ?>>10</option>
                                <option value="11" <?php echo (isset($_POST['day']) && $_POST['day'] == '11') ? 'selected' : ''; ?>>11</option>
                                <option value="12" <?php echo (isset($_POST['day']) && $_POST['day'] == '12') ? 'selected' : ''; ?>>12</option>
                                <option value="13" <?php echo (isset($_POST['day']) && $_POST['day'] == '13') ? 'selected' : ''; ?>>13</option>
                                <option value="14" <?php echo (isset($_POST['day']) && $_POST['day'] == '14') ? 'selected' : ''; ?>>14</option>
                                <option value="15" <?php echo (isset($_POST['day']) && $_POST['day'] == '15') ? 'selected' : ''; ?>>15</option>
                                <option value="16" <?php echo (isset($_POST['day']) && $_POST['day'] == '16') ? 'selected' : ''; ?>>16</option>
                                <option value="17" <?php echo (isset($_POST['day']) && $_POST['day'] == '17') ? 'selected' : ''; ?>>17</option>
                                <option value="18" <?php echo (isset($_POST['day']) && $_POST['day'] == '18') ? 'selected' : ''; ?>>18</option>
                                <option value="19" <?php echo (isset($_POST['day']) && $_POST['day'] == '19') ? 'selected' : ''; ?>>19</option>
                                <option value="20" <?php echo (isset($_POST['day']) && $_POST['day'] == '20') ? 'selected' : ''; ?>>20</option>
                                <option value="21" <?php echo (isset($_POST['day']) && $_POST['day'] == '21') ? 'selected' : ''; ?>>21</option>
                                <option value="22" <?php echo (isset($_POST['day']) && $_POST['day'] == '22') ? 'selected' : ''; ?>>22</option>
                                <option value="23" <?php echo (isset($_POST['day']) && $_POST['day'] == '23') ? 'selected' : ''; ?>>23</option>
                                <option value="24" <?php echo (isset($_POST['day']) && $_POST['day'] == '24') ? 'selected' : ''; ?>>24</option>
                                <option value="25" <?php echo (isset($_POST['day']) && $_POST['day'] == '25') ? 'selected' : ''; ?>>25</option>
                                <option value="26" <?php echo (isset($_POST['day']) && $_POST['day'] == '26') ? 'selected' : ''; ?>>26</option>
                                <option value="27" <?php echo (isset($_POST['day']) && $_POST['day'] == '27') ? 'selected' : ''; ?>>27</option>
                                <option value="28" <?php echo (isset($_POST['day']) && $_POST['day'] == '28') ? 'selected' : ''; ?>>28</option>
                                <option value="29" <?php echo (isset($_POST['day']) && $_POST['day'] == '29') ? 'selected' : ''; ?>>29</option>
                                <option value="30" <?php echo (isset($_POST['day']) && $_POST['day'] == '30') ? 'selected' : ''; ?>>30</option>
                                <option value="31" <?php echo (isset($_POST['day']) && $_POST['day'] == '31') ? 'selected' : ''; ?>>31</option>
                            </select>
                            <select class="selectbox_small" name="month" id="month">
                                <option value="NULL3" <?php echo (isset($_POST['month']) && $_POST['month'] == 'NULL3') ? 'selected' : ''; ?>>Month</option>
                                <option value="1" <?php echo (isset($_POST['month']) && $_POST['month'] == '1') ? 'selected' : ''; ?>>January</option>
                                <option value="2" <?php echo (isset($_POST['month']) && $_POST['month'] == '2') ? 'selected' : ''; ?>>February</option>
                                <option value="3" <?php echo (isset($_POST['month']) && $_POST['month'] == '3') ? 'selected' : ''; ?>>March</option>
                                <option value="4" <?php echo (isset($_POST['month']) && $_POST['month'] == '4') ? 'selected' : ''; ?>>April</option>
                                <option value="5" <?php echo (isset($_POST['month']) && $_POST['month'] == '5') ? 'selected' : ''; ?>>May</option>
                                <option value="6" <?php echo (isset($_POST['month']) && $_POST['month'] == '6') ? 'selected' : ''; ?>>June</option>
                                <option value="7" <?php echo (isset($_POST['month']) && $_POST['month'] == '7') ? 'selected' : ''; ?>>July</option>
                                <option value="8" <?php echo (isset($_POST['month']) && $_POST['month'] == '8') ? 'selected' : ''; ?>>August</option>
                                <option value="9" <?php echo (isset($_POST['month']) && $_POST['month'] == '9') ? 'selected' : ''; ?>>September</option>
                                <option value="10" <?php echo (isset($_POST['month']) && $_POST['month'] == '10') ? 'selected' : ''; ?>>October</option>
                                <option value="11" <?php echo (isset($_POST['month']) && $_POST['month'] == '11') ? 'selected' : ''; ?>>November</option>
                                <option value="12" <?php echo (isset($_POST['month']) && $_POST['month'] == '12') ? 'selected' : ''; ?>>December</option>
                            </select>
                            <select class="selectbox_small" name="year" id="year">
                                <option value="NULL4" <?php echo (isset($_POST['year']) && $_POST['year'] == 'NULL4') ? 'selected' : ''; ?>>Year</option>
                                <option value="1980"<?php echo (isset($_POST['year']) && $_POST['year'] == '1980') ? 'selected' : ''; ?>>1980</option>
                                <option value="1981" <?php echo (isset($_POST['year']) && $_POST['year'] == '1981') ? 'selected' : ''; ?>>1981</option>
                                <option value="1982" <?php echo (isset($_POST['year']) && $_POST['year'] == '1982') ? 'selected' : ''; ?>>1982</option>
                                <option value="1983" <?php echo (isset($_POST['year']) && $_POST['year'] == '1983') ? 'selected' : ''; ?>>1983</option>
                                <option value="1984" <?php echo (isset($_POST['year']) && $_POST['year'] == '1984') ? 'selected' : ''; ?>>1984</option>
                                <option value="1985" <?php echo (isset($_POST['year']) && $_POST['year'] == '1985') ? 'selected' : ''; ?>>1985</option>
                                <option value="1986" <?php echo (isset($_POST['year']) && $_POST['year'] == '1986') ? 'selected' : ''; ?>>1986</option>
                                <option value="1987" <?php echo (isset($_POST['year']) && $_POST['year'] == '1987') ? 'selected' : ''; ?>>1987</option>
                                <option value="1988" <?php echo (isset($_POST['year']) && $_POST['year'] == '1988') ? 'selected' : ''; ?>>1988</option>
                                <option value="1989" <?php echo (isset($_POST['year']) && $_POST['year'] == '1989') ? 'selected' : ''; ?>>1989</option>
                                <option value="1990" <?php echo (isset($_POST['year']) && $_POST['year'] == '1990') ? 'selected' : ''; ?>>1990</option>
                                <option value="1991" <?php echo (isset($_POST['year']) && $_POST['year'] == '1991') ? 'selected' : ''; ?>>1991</option>
                                <option value="1992" <?php echo (isset($_POST['year']) && $_POST['year'] == '1992') ? 'selected' : ''; ?>>1992</option>
                                <option value="1993" <?php echo (isset($_POST['year']) && $_POST['year'] == '1993') ? 'selected' : ''; ?>>1993</option>
                                <option value="1994" <?php echo (isset($_POST['year']) && $_POST['year'] == '1994') ? 'selected' : ''; ?>>1994</option>
                                <option value="1995" <?php echo (isset($_POST['year']) && $_POST['year'] == '1995') ? 'selected' : ''; ?>>1995</option>
                                <option value="1996" <?php echo (isset($_POST['year']) && $_POST['year'] == '1996') ? 'selected' : ''; ?>>1996</option>
                                <option value="1997" <?php echo (isset($_POST['year']) && $_POST['year'] == '1997') ? 'selected' : ''; ?>>1997</option>
                                <option value="1998" <?php echo (isset($_POST['year']) && $_POST['year'] == '1998') ? 'selected' : ''; ?>>1998</option>
                                <option value="1999" <?php echo (isset($_POST['year']) && $_POST['year'] == '1999') ? 'selected' : ''; ?>>1999</option>
                                <option value="2000" <?php echo (isset($_POST['year']) && $_POST['year'] == '2000') ? 'selected' : ''; ?>>2000</option>
                                <option value="2001" <?php echo (isset($_POST['year']) && $_POST['year'] == '2001') ? 'selected' : ''; ?>>2001</option>
                                <option value="2002" <?php echo (isset($_POST['year']) && $_POST['year'] == '2002') ? 'selected' : ''; ?>>2002</option>
                                <option value="2003" <?php echo (isset($_POST['year']) && $_POST['year'] == '2003') ? 'selected' : ''; ?>>2003</option>
                                <option value="2004" <?php echo (isset($_POST['year']) && $_POST['year'] == '2004') ? 'selected' : ''; ?>>2004</option>
                                <option value="2005" <?php echo (isset($_POST['year']) && $_POST['year'] == '2005') ? 'selected' : ''; ?>>2005</option>
                                <option value="2006" <?php echo (isset($_POST['year']) && $_POST['year'] == '2006') ? 'selected' : ''; ?>>2006</option>
                                <option value="2007" <?php echo (isset($_POST['year']) && $_POST['year'] == '2007') ? 'selected' : ''; ?>>2007</option>
                            </select><br />
                      </fieldset>
                      <!-- END RELATIONS -->
                     
                     
                      <!-- START REFERENCE -->
                      <p class="formtitle">Reference</p>
                     
                        <fieldset>
                        <label>Description of me</label><textarea name="desc" id="desc" cols="30" rows="5"><?php if(isset($_POST['desc'])){echo $_POST['desc'];}?></textarea><br />
                      </fieldset>
                      <!-- END REFERENCE -->
                         
                      <input class="submit" type="submit" id="submit" name="submit" value="Register reference" />   
                           
                    </form>
                   
        </div>
      </div>
        <!-- END LAYOUT CONTENT -->
       
        <!-- START LAYOUT FOOTER -->
        <div class="footer">
            <div class="footer_text">
        <?php include ("html_fragments/footer.html.php"); ?>
        </div>
        </div>
        <!-- START LAYOUT FOOTER -->
        <div class="copy">Copyright Dynamic Web Pages
        </div>
     
 
   
   
</body>
</html>
Avatar billede jakobdo Ekspert
19. marts 2011 - 11:32 #1
Avatar billede Vivivroue Nybegynder
19. marts 2011 - 11:59 #2
Nej, tror nærmere det er noget med at jeg skal skifte denne:

<label>Town</label>
                            <select name="zip" id="zip">
                                <option value="NULL" <?php echo (isset($_POST['zip']) && $_POST['zip'] == 'NULL') ? 'selected' : ''; ?>>Town</option>
                                <option value="7323" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7323') ? 'selected' : ''; ?>>7323 Give</option>
                                <option value="7330" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7330') ? 'selected' : ''; ?>>7330 Brande</option>
                                <option value="7361" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7361') ? 'selected' : ''; ?>>7361 Ejstrupholm</option>
                                <option value="7362" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7362') ? 'selected' : ''; ?>>7362 Hampen</option>
                                <option value="7400" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7400') ? 'selected' : ''; ?>>7400 Herning</option>
                               
                                <option value="7430" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7430') ? 'selected' : ''; ?>>7430 Ikast</option>
                                <option value="7441" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7441') ? 'selected' : ''; ?>>7441 Bording</option>
                                <option value="7442" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7442') ? 'selected' : ''; ?>>7442 Engesvang</option>
                                <option value="7451" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7451') ? 'selected' : ''; ?>>7451 Sunds</option>
                                <option value="7470" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7470') ? 'selected' : ''; ?>>7470 Karup</option>
                                <option value="7480" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7480') ? 'selected' : ''; ?>>7480 Vildbjerg</option>
                                <option value="7490" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7490') ? 'selected' : ''; ?>>7490 Aulum</option>
                                <option value="7500" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7500') ? 'selected' : ''; ?>>7500 Holstebro</option>
                                <option value="7540" <?php echo (isset($_POST['zip']) && $_POST['zip'] == '7540') ? 'selected' : ''; ?>>7540 Haderup</option>
                            </select><br />


ud med en kode der henter data fra databasen.
Avatar billede Vivivroue Nybegynder
19. marts 2011 - 12:16 #3
Tror måske dette er svaret?

    <label>Town</label>
                            <select name="zip" id="zip">
                                <option>Town</option>
                                <?php
                                // SQL statement
                                $querytowns = "SELECT * FROM town ORDER BY town";
                                // Executing the query
                                $resulttowns = @mysqli_query($dbc, $querytowns);
                                if($resulttowns){
                                    // Loop through the result and write it out
                                    while ($row = mysqli_fetch_array($resulttowns, MYSQLI_ASSOC)){
                                        echo "<option value=\"($row('zip_code'))\"";
                                        if(isset($_POST['zip']) && $_POST['zip'] == $row['zip_code']){
                                            echo "selected=\"selected\"";
                                        }
                                            echo">{$row['zip_code']} {$row['town']}</option>\n";
                                    }
                                } else {
                                        echo "An error happened!".mysqli_error($dbc);
                                }
                                       
                                ?>
                            </select><br />
Avatar billede jakobdo Ekspert
19. marts 2011 - 14:20 #4
Det var nu bare sådan jeg tolkede opgaven. :o)
Avatar billede Vivivroue Nybegynder
09. juni 2011 - 12:02 #5
Jakob, vil du have point?
Avatar billede jakobdo Ekspert
09. juni 2011 - 12:14 #6
Ok
Avatar billede jakobdo Ekspert
09. juni 2011 - 12:20 #7
Takker for point
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester