Home > front end >  How to get 2 value in option in php
How to get 2 value in option in php

Time:12-03

here my database image->>

on option tag ^ here! i need Cost and service on Option tag values how to set 2 values in select tag -> how to do it please give me anwser i dont know how to do it i m doing my collage project i need help.

    <?php
                            if ($genders=$_GET["gen"]=="Male") {
                               .

                             $query=mysqli_query($con,"select * from tblservices WHERE gender='Male'");
                            }if ($genders=$_GET["gen"]=="Female") {
                                .
                            
                             $query=mysqli_query($con,"select * from tblservices WHEREgender='Female'");
                            }
          while($row=mysqli_fetch_array($query))
          {
          ?>
    <option value="<?php echo $row['Cost']; ?>" id="price"><?php echo $row['ServiceName'];?>
        (<?php echo $row['Cost']; ?>₹)</oPHPon>

    <?php } ?>
          </select>

CodePudding user response:

There are several problems in your code snippet which you can solve if you look in the error log of apache.

The first select tag and the closing option tag are missing, there are redundant points and space missing in the second where clause.

If "$_GET["gen"]" retrieves data (what I cannot check) and the column/table names are right it should work.

<select>
<?php
  if ($_GET["gen"] =="Male") {

  $query=mysqli_query($con,"select * from tblservices WHERE gender='Male'");
   }if ($_GET["gen"]=="Female") {
                    
   $query=mysqli_query($con,"select * from tblservices WHERE gender='Female'");
                        }
      while($row=mysqli_fetch_array($query))
      {
      ?>
      <option value="<?php echo $row['Cost']; ?>" id="price"><?php echo $row['ServiceName'];?>
    <?php echo $row['Cost']; ?> ₹</option>

    <?php }
?>
</select>

CodePudding user response:

To display two values in an <option> tag in PHP, you can create an array that contains the two values, and then use the implode function to convert the array to a string. Here's an example of how you could do this:

$values = array($row['Cost'], $row['ServiceName']);

echo '<option value="' . implode(',', $values) . '">' . implode(',', $values) . '</option>';

In this example, we are creating an array that contains the Cost and ServiceName values from the database query. Then we are using the implode function to convert the array to a string, and printing the string as the value and text of the <option> tag.

This will display the Cost and ServiceName values in the <option> tag, separated by a comma. You can then access these values in your PHP code by splitting the string on the comma character.

I hope this helps! Let me know if you have any other questions.

EDIT: To insert both the ServiceName and Cost values into a database, you can use an INSERT SQL query. Here is an example of how you could do this:

$values = array($row['Cost'], $row['ServiceName']);

$query = "INSERT INTO table_name (Cost, ServiceName) VALUES ('" . implode("','", $values) . "')";

mysqli_query($conn, $query);
  • Related