Luka Mramor
Luka Mramor

Reputation: 25

Outputting a Row from an SQL Table

At the end of the code, I'd like to output the name of the class (E1A, R4C, ...) rather than the number that represents the ID of the name in the timetable.

Is there a way of doing that, instead of having a bunch of if statements that give the variable a different string (getting it from the database maybe)?

Any help is appreciated!

The "..." mean there's code before/after.

...
<select id="class" name="class">
    <option <?php if ($class == "3" ) echo 'selected' ; ?> value="3">E1A</option>
    <option <?php if ($class == "5" ) echo 'selected' ; ?> value="5">E1B</option>
    <option <?php if ($class == "6" ) echo 'selected' ; ?> value="6">E1C</option>
    <option <?php if ($class == "15" ) echo 'selected' ; ?> value="15">G1A</option>
    <option <?php if ($class == "16" ) echo 'selected' ; ?> value="16">G1B</option>
    <option <?php if ($class == "23" ) echo 'selected' ; ?> value="23">R1A</option>
    <option <?php if ($class == "24" ) echo 'selected' ; ?> value="24">R1B</option>
    <option <?php if ($class == "25" ) echo 'selected' ; ?> value="25">R1C</option>
    <option <?php if ($class == "26" ) echo 'selected' ; ?> value="26">R1D</option>
    <option <?php if ($class == "7" ) echo 'selected' ; ?> value="7">E2A</option>
    <option <?php if ($class == "8" ) echo 'selected' ; ?> value="8">E2B</option>
    <option <?php if ($class == "9" ) echo 'selected' ; ?> value="9">E2C</option>
    <option <?php if ($class == "17" ) echo 'selected' ; ?> value="17">G2A</option>
    <option <?php if ($class == "18" ) echo 'selected' ; ?> value="18">G2B</option>
    <option <?php if ($class == "27" ) echo 'selected' ; ?> value="27">R2A</option>
    <option <?php if ($class == "28" ) echo 'selected' ; ?> value="28">R2B</option>
    <option <?php if ($class == "29" ) echo 'selected' ; ?> value="29">R2C</option>
    <option <?php if ($class == "30" ) echo 'selected' ; ?> value="30">R2D</option>
    <option <?php if ($class == "10" ) echo 'selected' ; ?> value="10">E3A</option>
    <option <?php if ($class == "11" ) echo 'selected' ; ?> value="11">E3B</option>
    <option <?php if ($class == "12" ) echo 'selected' ; ?> value="12">E3C</option>
    <option <?php if ($class == "19" ) echo 'selected' ; ?> value="19">G3A</option>
    <option <?php if ($class == "20" ) echo 'selected' ; ?> value="20">G3B</option>
    <option <?php if ($class == "31" ) echo 'selected' ; ?> value="31">R3A</option>
    <option <?php if ($class == "32" ) echo 'selected' ; ?> value="32">R3B</option>
    <option <?php if ($class == "33" ) echo 'selected' ; ?> value="33">R3C</option>
    <option <?php if ($class == "34" ) echo 'selected' ; ?> value="34">R3D</option>
    <option <?php if ($class == "13" ) echo 'selected' ; ?> value="13">E4A</option>
    <option <?php if ($class == "14" ) echo 'selected' ; ?> value="14">E4B</option>
    <option <?php if ($class == "21" ) echo 'selected' ; ?> value="21">G4A</option>
    <option <?php if ($class == "22" ) echo 'selected' ; ?> value="22">G4B</option>
    <option <?php if ($class == "35" ) echo 'selected' ; ?> value="35">R4A</option>
    <option <?php if ($class == "36" ) echo 'selected' ; ?> value="36">R4B</option>
    <option <?php if ($class == "37" ) echo 'selected' ; ?> value="37">R4C</option>
    <option <?php if ($class == "38" ) echo 'selected' ; ?> value="38">R4D</option>
    </select>
    ...
$sql = "SELECT *
FROM timetable t, class c, room r
WHERE c.id = t.class AND t.week = '".$_GET['week']."' AND t.id = '".$_GET['class']."' 
ORDER BY CASE
          WHEN Day = 'Sunday' THEN 1
          WHEN Day = 'Monday' THEN 2
          WHEN Day = 'Tuesday' THEN 3
          WHEN Day = 'Wednesday' THEN 4
          WHEN Day = 'Thursday' THEN 5
          WHEN Day = 'Friday' THEN 6
          WHEN Day = 'Saturday' THEN 7
      ELSE Day
     END ASC, Hour;
";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "Timetable of the class: ".$_GET['class']."";
    ...

Upvotes: 0

Views: 50

Answers (2)

Padmanabhan
Padmanabhan

Reputation: 158

You shall try this .. When dropdawn is clicked You will get a value like this 3:E1A, Explode it and manipulate whatever way you want

     <select id="class" name="class">
      <option value="" selectcted>Select one</option>
    <?php
       while.....  {
       ......
       $ClassID=$SomeID;  // ID row stuff Eg 3
       $OtherValue=$SomeValue; // Eg E1A

       $VisibleValue=$OtherValue;
       $InternalValue=$ClassID . ":" . $OtherValue;

    ?>  

    <option value ="<?php echo($InternalValue) ?>"><?php echo($VisibleValue) ?> 
    </option>

    <?php } ?> // closing the While loop
    </select>

Upvotes: 1

night-gold
night-gold

Reputation: 2421

You should be able to use the name associated with the id in your $result as you are using a "select *" in your request, but it's only a guess because we can't know the column with what you gave us.

Did you try to do a var_dump of your results to see what you get?

Upvotes: 1

Related Questions