• February 25, 2023

    here’s an example PHP code that allows users to select their age, sex, and displays 4 plans of a table for comparing the value of the premium:

    <!DOCTYPE html>
    <html>
    <head>
    	<title>Compare Premium Plans</title>
    </head>
    <body>
    	<h1>Compare Premium Plans</h1>
    	<form method="POST">
    		<label for="age">Age:</label>
    		<input type="number" name="age" id="age" min="18" max="100" required><br><br>
    		<label for="sex">Sex:</label>
    		<select name="sex" id="sex" required>
    			<option value="">--Select--</option>
    			<option value="male">Male</option>
    			<option value="female">Female</option>
    		</select><br><br>
    		<input type="submit" name="submit" value="Submit">
    	</form>
    
    	<?php
    	if (isset($_POST['submit'])) {
    		$age = $_POST['age'];
    		$sex = $_POST['sex'];
    
    		if ($sex == 'male') {
    			if ($age >= 18 && $age <= 25) {
    				$plan1 = '$500';
    				$plan2 = '$700';
    				$plan3 = '$900';
    				$plan4 = '$1100';
    			} elseif ($age >= 26 && $age <= 40) {
    				$plan1 = '$600';
    				$plan2 = '$800';
    				$plan3 = '$1000';
    				$plan4 = '$1200';
    			} else {
    				$plan1 = '$700';
    				$plan2 = '$900';
    				$plan3 = '$1100';
    				$plan4 = '$1300';
    			}
    		} else {
    			if ($age >= 18 && $age <= 25) {
    				$plan1 = '$450';
    				$plan2 = '$650';
    				$plan3 = '$850';
    				$plan4 = '$1050';
    			} elseif ($age >= 26 && $age <= 40) {
    				$plan1 = '$550';
    				$plan2 = '$750';
    				$plan3 = '$950';
    				$plan4 = '$1150';
    			} else {
    				$plan1 = '$650';
    				$plan2 = '$850';
    				$plan3 = '$1050';
    				$plan4 = '$1250';
    			}
    		}
    
    		echo '<h2>Premium Plan Comparison</h2>';
    		echo '<table border="1">';
    		echo '<tr><th>Plan</th><th>Price</th></tr>';
    		echo "<tr><td>Plan 1</td><td>$plan1</td></tr>";
    		echo "<tr><td>Plan 2</td><td>$plan2</td></tr>";
    		echo "<tr><td>Plan 3</td><td>$plan3</td></tr>";
    		echo "<tr><td>Plan 4</td><td>$plan4</td></tr>";
    		echo '</table>';
    	}
    	?>
    </body>
    </html>
    

    The code above first displays a form that allows users to enter their age and select their sex. When the user submits the form, the code uses the entered age and sex to determine the premium plan prices based on some pre-defined rules. The code then displays a table that shows the comparison of the premium plans and their prices.

    add check box “occupation” for all value plan will multiply 1.4 :
    To add a checkbox for occupation and multiply the premium plan values by 1.4 if the checkbox is checked, you can modify the HTML form and the PHP code as follows:

    <label for="occupation">Occupation:</label>
    <input type="checkbox" name="occupation" id="occupation">
    ...
    if (isset($_POST['submit'])) {
        $age = $_POST['age'];
        $sex = $_POST['sex'];
        $occupation = isset($_POST['occupation']) && $_POST['occupation'] == 'on';
    
        if ($sex == 'male') {
            if ($age >= 18 && $age <= 25) {
                $plan1 = '$500';
                $plan2 = '$700';
                $plan3 = '$900';
                $plan4 = '$1100';
            } elseif ($age >= 26 && $age <= 40) {
                $plan1 = '$600';
                $plan2 = '$800';
                $plan3 = '$1000';
                $plan4 = '$1200';
            } else {
                $plan1 = '$700';
                $plan2 = '$900';
                $plan3 = '$1100';
                $plan4 = '$1300';
            }
        } else {
            if ($age >= 18 && $age <= 25) {
                $plan1 = '$450';
                $plan2 = '$650';
                $plan3 = '$850';
                $plan4 = '$1050';
            } elseif ($age >= 26 && $age <= 40) {
                $plan1 = '$550';
                $plan2 = '$750';
                $plan3 = '$950';
                $plan4 = '$1150';
            } else {
                $plan1 = '$650';
                $plan2 = '$850';
                $plan3 = '$1050';
                $plan4 = '$1250';
            }
        }
    
        if ($occupation) {
            $plan1 = '$' . (int)str_replace('$', '', $plan1) * 1.4;
            $plan2 = '$' . (int)str_replace('$', '', $plan2) * 1.4;
            $plan3 = '$' . (int)str_replace('$', '', $plan3) * 1.4;
            $plan4 = '$' . (int)str_replace('$', '', $plan4) * 1.4;
        }
    
        echo '<h2>Premium Plan Comparison</h2>';
        echo '<table border="1">';
        echo '<tr><th>Plan</th><th>Price</th></tr>';
        echo "<tr><td>Plan 1</td><td>$plan1</td></tr>";
        echo "<tr><td>Plan 2</td><td>$plan2</td></tr>";
        echo "<tr><td>Plan 3</td><td>$plan3</td></tr>";
        echo "<tr><td>Plan 4</td><td>$plan4</td></tr>";
        echo '</table>';
    }
    

    The modified PHP code checks if the occupation checkbox is checked and multiplies the premium plan values by 1.4 if it is. Note that the premium plan values are converted to integers before the multiplication and then converted back to strings with the $ symbol.

    To display all selected form values, you can add the following code after the plan comparison table:

    <?php
    if (isset($_POST['submit'])) {
        $age = $_POST['age'];
        $sex = $_POST['sex'];
        $occupation = isset($_POST['occupation']) ? $_POST['occupation'] : '';
    
        // code to determine premium plans
    
        echo '<h2>Selected Form Values</h2>';
        echo '<ul>';
        echo "<li>Age: $age</li>";
        echo "<li>Sex: $sex</li>";
        if (!empty($occupation)) {
            echo "<li>Occupation: $occupation</li>";
        }
        echo '</ul>';
    
        // code to display premium plan comparison table
    }
    ?>
    This code checks if the "occupation" checkbox was selected and stores the value in a variable called $occupation. It then displays the selected form values in an unordered list. If the "occupation" checkbox was selected, it will display the occupation value in the list.

    To display “renew only” when age is between 26 and 40, you can add an if-else condition inside the table loop. Here’s the modified code

    echo '<table border="1">';
    echo '<tr><th>Plan</th><th>Price</th><th>Renewal Option</th></tr>';
    echo "<tr><td>Plan 1</td><td>$plan1</td><td>".(($age >= 26 && $age <= 40) ? 'Renew Only' : '')."</td></tr>";
    echo "<tr><td>Plan 2</td><td>$plan2</td><td>".(($age >= 26 && $age <= 40) ? 'Renew Only' : '')."</td></tr>";
    echo "<tr><td>Plan 3</td><td>$plan3</td><td>".(($age >= 26 && $age <= 40) ? 'Renew Only' : '')."</td></tr>";
    echo "<tr><td>Plan 4</td><td>$plan4</td><td>".(($age >= 26 && $age <= 40) ? 'Renew Only' : '')."</td></tr>";
    echo '</table>';
    

    In this code, we are checking if the age is between 26 and 40 using the ternary operator ?: inside each row of the table. If the condition is true, then the string “Renew Only” is displayed in the “Renewal Option” column. Otherwise, an empty string is displayed.

    หรือ พิม์ต่อท้ายง่าย

    if ($age >= 18 && $age <= 25) {
        $plan1 = '$500';
        $plan2 = '$700';
        $plan3 = '$900';
        $plan4 = '$1100';
    } elseif ($age >= 26 && $age <= 40) {
        $plan1 = '$600';
        $plan2 = '$800 (renew only)';
        $plan3 = '$1000 (renew only)';
        $plan4 = '$1200 (renew only)';
    } else {
        $plan1 = '$700';
        $plan2 = '$900';
        $plan3 = '$1100';
        $plan4 = '$1300';
    }
    

    To modify the age selection to include age ranges like “11-15”, “16-20”, “21-25”, you can use the option tag with value attributes to define the age ranges as follows:

    <select name="age" id="age" required>
      <option value="">--Select Age Range--</option>
      <option value="11-15">11-15</option>
      <option value="16-20">16-20</option>
      <option value="21-25">21-25</option>
      <option value="26-30">26-30</option>
      <!-- Add more age ranges as needed -->
    </select>
    

    Then in your PHP code, you would need to modify the logic to extract the age range selected from the form and use it to determine the premium plan pricing.

    For example, you could use the explode function to split the age range string into two numbers, and then use those numbers to determine the appropriate pricing for each plan:

    if (isset($_POST['submit'])) {
      $age_range = $_POST['age'];
      $age_range_parts = explode('-', $age_range);
      $min_age = intval($age_range_parts[0]);
      $max_age = intval($age_range_parts[1]);
      
      // Use min_age and max_age to determine the appropriate pricing for each plan
      // ...
    }
    

    change age selection to rang slider

    <label for="age">Age:</label>
    <input type="range" name="age" id="age" min="18" max="100" step="1" value="18">
    
    

    This will display a range slider input that ranges from 18 to 100, with a step of 1, and an initial value of 18. You can adjust the min, max, step, and value attributes as needed.



เวอไนน์ไอคอร์ส

ประหยัดเวลากว่า 100 เท่า!






เวอไนน์เว็บไซต์⚡️
สร้างเว็บไซต์ ดูแลเว็บไซต์

Categories


Uncategorized