Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP if/else statement doesn't produce the desired results
    primarykey
    data
    text
    <p>I have a dropdown menu populated from a table in a MySQL database. It functions, but I would like to add an if/else statement to the code to change the default value of the dropdown according to the conditions. Here is the code for the dropdown (much thanks to AlienWebguy for his assistance in getting this far):</p> <pre><code> &lt;?php $sql="SELECT techID, tech_userlogin FROM technicians"; $result=mysql_query($sql); while ($row=mysql_fetch_array($result)) { $techID = $row['techID']; $tech_userlogin=$row['tech_userlogin']; // Here - check if the $_GET value in the query matches the tech_userlogin ($selected = ($tech_userlogin == $_GET['wtech_userlogin']) ? 'selected="selected"' : ''); echo '&lt;option value="' . $tech_userlogin . '" ' . $selected . '&gt;' . $tech_userlogin . '&lt;/option&gt;' . "\n"; } ?&gt; </code></pre> <p>I've tried a couple of things to accomplish what I'm after, but I'm severely hampered by my lack of experience and/or expertise in PHP/MySQL. </p> <p>This is the first time I've tried these things and it has been hit and miss so far. I've recieved help here at Stack Overflow and I'm grateful such a resource exists.</p> <p>This is my latest effort and it fails to achieve what I'm after. I want the default value of the dropdown to be "--Select Technician--" if the condition is not met in the if statement.</p> <pre><code>&lt;?php $sql="SELECT techID, tech_userlogin FROM technicians"; $result=mysql_query($sql); $options=""; while ($row=mysql_fetch_array($result)) { $techID = $row['techID']; $tech=$row['tech_userlogin']; $options.="&lt;option value=\"$tech\"&gt;$tech&lt;/option&gt;"; // Here - check if the $_GET value in the query matches the tech_userlogin if ($selected = ($tech == $_GET['wtech_userlogin']) ? 'selected="selected"' : '') {echo '&lt;option value="' . $tech . '" ' . $selected . '&gt;' . $tech . '&lt;/option&gt;' . "\n";} else {echo '&lt;option value="' . $options . '" . 'selected="selected"'&gt;'--Select Tech--'&lt;. $options .&gt;' . "\n";} } ?&gt; </code></pre> <p>Can someone please help me or point me in the right direction? Many thanks.</p> <p>Cheers</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload