skip to Main Content

I’m not new to PHP and MySQL, but I’m not professional in it.

I have a mysql database table related to program settings.
Thirty cities, each of which has fifteen items, and every day these items must be reviewed by the user and, if necessary, changed and saved. Like the picture below:
first twenty rows of table

I can make a table with a div and put an input in each cell, read the information from the database and display it to the user and save it in the database after the change.
But I feel that this method of mine may work, but it is not professional at all.
My question is, what is the best way to do this, especially if I want the names of the cities and their items to be assigned by the program to each of the input boxes, so that if the city name changes in the database, there is no need to change the program code.

Footnote: The language used is PHP and the names of the cities and their items in the image are hypothetical and the names are not in sequence.

I appreciate your help in advance.

2

Answers


  1. Chosen as BEST ANSWER

    @Karl Hill Thank you for ur response, the first row data from table is:

    array(16) { ["id"]=> string(1) "2" ["main_province"]=> string(27) "city1" ["Fertilaid_Men"]=> string(1) "1" ["Fertilaid_Women"]=> string(1) "2" ["FH_PRO_Men"]=> string(3) "552" ["FH_PRO_Women"]=> string(4) "1423" ["Ovaboost_60"]=> string(1) "0" ["Ovaboost_120"]=> string(4) "8569" ["Fertilecm"]=> string(1) "0" ["Motility_Boost"]=> string(1) "0" ["Count_Boost"]=> string(1) "0" ["Balance_Blend"]=> string(1) "0" ["Pregease"]=> string(1) "0" ["Myo_Inositol"]=> string(1) "0" ["BabyDance"]=> string(1) "0" ["Nursing_Blend"]=> string(1) "0" }
    

    how to get the items name and province name?


  2. You can create a dynamic form based on the data from your database. This way, if the city names or items change in the database, your form will automatically reflect these changes without needing to modify your code.

    Here’s a basic example of how you can do this in PHP:

    // Fetch the data from the database
    $query = "SELECT * FROM your_table";
    $result = mysqli_query($connection, $query);
    
    // Start the form
    echo '<form method="post" action="your_action_page.php">';
    
    // Loop through the data and create the form fields
    while ($row = mysqli_fetch_assoc($result)) {
        $city = $row['city'];
        $item = $row['item'];
        $value = $row['value'];
    
        echo "<label for='$city-$item'>$city - $item:</label>";
        echo "<input type='text' id='$city-$item' name='$city-$item' value='$value'><br>";
    }
    
    // End the form
    echo '<input type="submit" value="Submit">';
    echo '</form>';
    

    In this code, mysqli_query is used to fetch the data from the database. Then, a while loop is used to iterate over the data and create an input field for each row. The id and name attributes of the input fields are set to the city and item names, and the value attribute is set to the current value. This creates a dynamic form based on the data in your database.

    When the form is submitted, you can then process the form data and update the database accordingly. Note that you’ll need to replace your_table and your_action_page.php with your actual table name and form action page.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search