1

So I have this database and it has column called selectedDates. In that column there is a row with the values

2018-08-22
2018-08-15
2018-08-20

Keep in mind this is 3 sets of dates, but in one row. How do I make a select tag/dropdown with these three values. Is this possible to do? If more information is needed, let me know and I will edit.

Down below is my code for having it do a dropdown of options within one column, but different rows. Such as

 Column 1
Row 1 -  Dog
Row 2 - Cat
Row 3 - Mouse

I need it to be like

Column 1 
Row 1 - Dog,Cat,Mouse 

and then have the dropdown options as dog cat mouse

example code below of column 1, 3 different rows

 <?php
$con = new mysqli('localhost', 'root', '', 'races');

if($con->connect_errno) {
// error reporting here
}

$result = $con->query("SHOW TABLES");

if($result->num_rows > 0) {
echo '<select id="myTable" onchange="myFunction()">';
while($row = $result->fetch_array(MYSQLI_NUM)) {
    echo '<option value="Test" name="SelectedDate">' . $row[0] . '</option>';
}
echo '</select>';
} ?>

edit for replying to comment Example database

Id | Info | SelectedDates 
1    hi    2018-18,2018-19 
2    hi    2018-20,2018-21 

In the code you provided, in my example it would only create a dropdown with id 1, but if I had another row like id 2, can i change $row[0] to $row[1] to have my dropdown display the 2nd row of selected dates?

3

2 Answers 2

1
  • If you want to check for a connection without explicitly checking the connect_errno property, you can simply check for a false $con value.
  • You should nominate the exact columns that you are going to access from your resultset. If you are only targeting the one row, only query for that one.
  • As a more brief syntax, you can drop > 0 and check for a non-falsey $result->num_rows value.
  • If you want to identify resultset columns by index, you can use $result->fetch_row() (again shorter syntax)
  • myTable is an inappropriate id value for your <select> tag -- because it is not a "table". I cannot suggest a better one because I don't know what your greater form structure is.
  • <option> tags don't take a name attribute, that goes in the parent <select> tag.
  • Setting a static value attribute value of Test will result in all options submitting the same value: Test (obviously that doesn't make sense). In fact, unless you need to reformatted/modify the date value, don't write a value attribute at all; just rely on the option's text to contain the value.

Untested Suggestion: (assuming you are processing just one row of data)

if (!$con = new mysqli('localhost', 'root', '', 'races')) {
    // connection error
} elseif (!$result = $con->query("SELECT selectedDates FROM `your_table` WHERE id = " . (int)$id)) {
    // query syntax error
} elseif (!$row = $result->fetch_row()) {
    // possible logic error, no row found
} else {
    echo '<select id="myTable" name="SelectedDate" onchange="myFunction()">';
    foreach (explode(",", $row[0]) as $date) {
        echo "<option>$date</option>";
    }
    echo "</select>";
}
Sign up to request clarification or add additional context in comments.

7 Comments

This works, but if it's from a different row, can I just change $row[0] accordingly?
Do you mean you have several rows that have comma-separated values? Do you want all of the dates from all rows as <option>s in a single <select>? or do you mean you want to create a new <select> for each row of the resultset?
[0] of $row[0] is the column identifier. If you used associative keys, the comma-separated values would be accessed via $row['selectedDates'].
If you want all selectedDates data from multiple rows, then I would recommend grouping in the query itself (joining with commas). If you want separate <select>s, then you replace the second elseif with else and write a while($row = $result->fetch_row()){ loop inside of that.
Never mind, I figured it out. Basically I just had to do 'WHERE Id = 17' and that was all
|
0

You say you must have all the dates in the same column, why?

Isn't this a database misconception? If you need multiple row for one value, you should have a table with "id | selectedDate" so that the issue you are speaking about does not happen.

If you must continue like this, you should try to look at this: https://secure.php.net/manual/fr/function.explode.php

Hope it helps.

2 Comments

I need all the dates in one row, not column. I wish i can add images to show what my database looks like. I have all the rows set up with an Id as my auto int, primary key. I can't use explode since there is no array that I am using.
We don't need an image of your db, you can go to phpmyadmin and export the structure and row -- in fact that is preferred from a volunteer's perspective because then the details are 100% accurate and we can copy-paste. explode() doesn't require an array, it creates one.

Your Answer

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