0

I have created a datatable which I am populating using data from an MYSQL database.

In order to run jQuery commands, I need to give the elements an id or class

but get an error when I add this into the PHP code.

Code I am using:

<?php
    $host_name = 'xxx.hosting-data.io';
    $database = 'xxx';
    $user_name = 'xxx';
    $password = 'xxx';
    $conn = mysqli_connect($host_name, $user_name, $password, $database);

    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    $sql = "SELECT * FROM `table`";
    $query = mysqli_query($conn, $sql);
    if (mysqli_num_rows($query) > 0) {
        // output data of each row
        while($result = mysqli_fetch_assoc($query)) {
            echo "<select>
                      <option>".$result['tablerow']."</option>
                  </select>";
        }
    } else { 
        echo "0 results";
    }
    mysqli_close($conn);
?>

I want to add a class, id and name to the select and a value to the option

When I try adding the class using <select class="class"> it seems to break the site same with id and value.

Is there a better way to add such values?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Paulmcf1987
  • 99
  • 10
  • FYI: Using double quotes for ` – Funk Forty Niner Sep 07 '19 at 17:50

2 Answers2

0

Beware the quoting here, using double quotes will confuse PHP, this will likely do what you need:

<select class='class'> 
suspectus
  • 16,548
  • 8
  • 49
  • 57
0

Instead of using

echo "<select>
        <option class="yourClass">".$result['tablerow']."</option>
      </select>";

Try using single quotes

 echo "<select>
            <option class='yourClass'>".$result['tablerow']."</option>
          </select>";
Frosty
  • 299
  • 5
  • 31