1

Not experienced with creating forms in PHP.

I can get my form to produce a dropdown list that has one of my rows listed as an option, but as soon as I try to concatenate 2 rows together (from the same table) for option output...

a) It just doesn't work and I get errors

b) I get the first row as a single option, then my next row as a separate option.

I know there is a simple solution to this, but I am an online student just learning, and I can't seem to find a good example of the code to write it. I'm pretty sure it's an issue of quotes not being placed correctly.

MySQLTable Data:

Table Name: courses
Table Rows: course_id, course_name, max_enrolment
Sample Data: LO-COMP-8001, Intro to HTML, 20

function select_course(){
        global $open;
        $select = "SELECT * FROM courses";
        $result = mysqli_query($open, $select);
        return $result;
    }
    <form action="insert.php" method="post">
    <dl>
        <dt>Select Course</dt>
        <dd><select name="course_id">
           <?php   // CREATE dropdown menu
                 $result = select_course();  
                    while ($row = mysqli_fetch_assoc($result)) {
                    foreach ($row as $selection) {
                       echo "<option value=\"$selection\">$selection</option>";
                    }}
              ?>
        </select>
        </dd>
    </dl>

Then there are a few more form fields such as student name and student id afterwards...

Goal Output:

  • course_id course-name
  • "LO-COMP-8001 Intro to HTML" ... as a single connected dropdown option and other remaining courses in a dropdown menu

Current Output:

  • LO-COMP-8001 (as an option)
  • Intro to HTML (as another option! ... No good)
  • 20 (must be hidden, I need to check if course is full in another function and either allow or deny a student to enrolled etc.)

I have tried:

// output is the one mentioned above..
echo "<option value=\"$selection\">$selection</option>";
// or alternatively...
echo '<option value="'.$row['course_id'].'">'.$row['course_id'].'</option>';

But the second option creates all kinds of weird results.

This is what I am experimenting with right now...

echo '<option value="'.$row['course_id'] $row['course_name']'">'.$row['course_id'] $row['course_name'].'</option>';

But there is a bunch of issues with quotes and square brackets, and I just don't know how to format it correctly for the output.

Any assistance is appreciated.

2
  • Your last effort is almost there. Try echo '<option value="' . $row['course_id'] . ' ' . $row['course_name'] . '">' . $row['course_id'] . ' ' . $row['course_name'] . '</option>'; Commented Apr 18, 2020 at 1:35
  • Thanks! Worked perfectly in combination with the answer from u/nelsonrakson. I took out the foreach loop and used the echo you provided. Perfect! Commented Apr 18, 2020 at 1:43

2 Answers 2

2

$row holds the entire row as an associative array therefore no need for the 'foreach' loop.

function select_course(){
        global $open;
        $select = "SELECT * FROM courses";
        $result = mysqli_query($open, $select);
        return $result;
    }
    <form action="insert.php" method="post">
    <dl>
        <dt>Select Course</dt>
        <dd><select name="course_id">
           <?php   // CREATE dropdown menu
                 $result = select_course();  
                    while ($row = mysqli_fetch_assoc($result)) {?>
                       <option value="<?php echo $row["course_id"]; ?>"><?php echo $row["course_name"]; ?></option>
                    <?php }
              ?>
        </select>
        </dd>
    </dl>
    </form>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! As soon as I took the foreach out and changed the echo to: echo '<option value="' . $row['course_id'] . ' ' . $row['course_name'] . '">' . $row['course_id'] . ' ' . $row['course_name'] . '</option>'; It worked great. Appreciated!
0

I was able to come up with another solution as well:

Once the foreach loop was removed, I tried cleaning up the code some... I'm not sure if this is uncommon or 'bad' style, but it does work.

  $result = select_course();  
                    while ($row = mysqli_fetch_assoc($result)) {
                       $course_id     = $row['course_id'];
                       $course_name   = $row['course_name'];
                       echo  "<option value=\"$course_id\">$course_id $course_name</option>";

Results in: LO-COMP-8001 Intro to HTML as a single option, plus all my other courses in the database.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.