0

I've created in PHP a html table with a combobox that retrieves the values of a mysql database table.

This is my code:

<?php
    include_once 'indeling/header.php';

print '<a href="overzicht_notass.php" class="button">Overzicht nota\'s</a> <br /><br />';

$sql = "
SELECT
    notas.id AS notaid
    , notas.klantid AS klantid
    , notas.bedrag AS bedrag
    , notas.datum AS datum
    , contacten.bedrijf AS bedrijf
    , contacten.adres AS adres
    , contacten.woonplaats AS woonplaats
FROM
    notas
    LEFT JOIN contacten 
        ON (notas.klantid = contacten.id);
";

function bedrijven($mysqli) {

$sqlbedrijven = "
SELECT 
    id
,   bedrijf
,   adres
,   woonplaats
FROM
    contacten
    ORDER BY bedrijf ASC
";

    $resultbedrijven = $mysqli->query($sqlbedrijven); 
    if (!$resultbedrijven) {
        echo "something went wrong: (" . $mysqli->error .")";
    }
        echo "<select name = klantid>\n";
        while ($row = $resultbedrijven->fetch_assoc()) {
echo <<<opt
<option value="{$row['id']}"> {$row['bedrijf']} - {$row['adres']} - {$row['woonplaats']} </option>

opt;
        }
        echo "</select>\n";
}


$result = $mysqli->query($sql); 
    if (!$result) {
        echo "Oeps hier gaat iets fout: (" . $mysqli->error .")";
    }
    else {
        printf("Er zijn momenteel %d nota's.<br />", $result->num_rows);

echo "
<table>
    <tr>
        <th>notanummer.</th>
        <th>bedrijf</th>
        <th>bedrag</th>
        <th>datum</th>
        <th>bewerken</th>
    </tr>
";

    while ($row = $result->fetch_assoc()) {
        echo '<tr> <form action="overzicht_relaties_bewerken.php" method="post">
        <td> <input type="text" class="short" name="notaid" value="' . $row['notaid'] . '"></td>
        <td> ' . bedrijven($mysqli) . ' </td> 
        <td> <input type="text" name="bedrag" value="' . $row['bedrag'] . '"></td>
        <td> <input type="date" name="datum" value="' . $row['datum'] . '"></td>
        <td> <input type="submit" name="update" value="aanpassen" class="button">' . '<br />
        <input type="submit" name="delete" value="verwijderen" class="button"' . '"></td>
        </tr></form>
        ';
    }
        echo "</table>";
}

?>

it all works but I see the comboboxes above the table and not in the second TD bedrijven($mysqli)

here is a dump of mij html source code of the webpage:

<a href="overzicht_notass.php" class="button">Overzicht nota's</a> <br /><br />Er zijn momenteel 2 nota's.<br />
<table>
    <tr>
        <th>notanummer.</th>
        <th>bedrijf</th>
        <th>bedrag</th>
        <th>datum</th>
        <th>bewerken</th>
    </tr>
<select name = klantid>
<option value="37"> afsdf - fasdf12 - Klarenbeek - (Gelderland) </option>
<option value="36"> afsdf - fasdf12 - Klarenbeek - (Gelderland) </option>
<option value="38"> afsdf2 - fdas - Klarenbeek </option>

now we have a bunch of more option values and then:

</select>
<tr> <form action="overzicht_relaties_bewerken.php" method="post">
        <td> <input type="text" class="short" name="notaid" value="2"></td>
        <td>  </td> 
        <td> <input type="text" name="bedrag" value="125.50"></td>
        <td> <input type="date" name="datum" value="2013-06-04"></td>

the table data under tabledata id is blank...?

So I call the function at the right place (2nd TD) but is shows the comboboxes somewhere else.

Any idea how this is possible?

1 Answer 1

1

I am not good at php but I think your select tag is not in the table! See how i do this in html:

<table>
<tr>
    <th>notanummer.</th>
    <th>bedrijf</th>
    <th>bedrag</th>
    <th>datum</th>
    <th>bewerken</th>
</tr>
<tr>
    <td colspan='5'>
        <select name = klantid>
        <option value="37"> afsdf - fasdf12 - Klarenbeek - (Gelderland) </option>
        <option value="36"> afsdf - fasdf12 - Klarenbeek - (Gelderland) </option>
        <option value="38"> afsdf2 - fdas - Klarenbeek </option>
        </select>
    </td>
</tr>

<tr> <form action="overzicht_relaties_bewerken.php" method="post">
    <td> <input type="text" class="short" name="notaid" value="2"></td>
    <td>  </td> 
    <td> <input type="text" name="bedrag" value="125.50"></td>
    <td> <input type="date" name="datum" value="2013-06-04"></td>

so change your php code from:

    echo "<select name = klantid>\n";
    while ($row = $resultbedrijven->fetch_assoc()) {
    echo <<<opt
    <option value="{$row['id']}"> {$row['bedrijf']} - {$row['adres']} -       {$row['woonplaats']} </option>

    opt;
    }
    echo "</select>\n";

to:

    echo "<tr><td colspan='5'><select name = klantid>\n";
    while ($row = $resultbedrijven->fetch_assoc()) {
    echo <<<opt
    <option value="{$row['id']}"> {$row['bedrijf']} - {$row['adres']} -       {$row['woonplaats']} </option>

    opt;
    }
    echo "</select></td></tr>\n";
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Seyed your solution did not work. the html part was correct, but I cold'nt do it in PHP what I did was: I copied the content of my function into the php table. that solved it. thanks for the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.