patheticsam Posted May 13, 2010 Share Posted May 13, 2010 Hi! i'm a little bit new to php and I'm having a little issue to define the initial value of a select box depending on some data contained into a mySQL db... I have a link (href="xx.html?cmd=view&id=$id......") wich leads to a form with already some data... Here's my script : <?php mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_select_db("db") or die(mysql_error()); if( ($_GET['cmd']=="view") && (is_numeric($_GET['id'])) ) { $id=$_GET['id']; $data = mysql_query("SELECT * FROM opportunite WHERE id='$id'") or die(mysql_error()); while($info = mysql_fetch_array( $data )) { echo " <tr> <td style=\"text-align: center;\" colspan=\"2\"><h1>Fiche Client</h1></td> </tr> <tr> <td>Nom de l'entreprise : </td> <td style=\"text-align: right;\"><input type=\"text\" name=\"entreprise\" value=\"".$info['entreprise']."\" style=\"width: 200px;\"></td> </tr> <tr> <td>Date : </td> <td style=\"text-align: right;\"><input type=\"text\" name=\"date\" value=\"".$info['date']."\" style=\"width: 200px;\"></td> </tr> <tr> <td>Catégorie : </td> <td style=\"text-align: right;\"> <select name=\"categorie\" style=\"width: 206px;\"> <option value=\"Restaurant\">Restaurant</option> <option value=\"Salon de coiffure\">Salon de coiffure</option> <option value=\"Sale de bronzage\">Sale de bronzage</option> <option value=\"Magasin de vetements\">Magasin de vetements</option> <option value=\"Magasin de détail\">Magasin de détail</option> <option value=\"Centre commercial\">Centre commercial</option> <option value=\"Location Immobilier\">Location / Immobilier</option> <option value=\"Autre\">Autre</option> </select> </td> </tr> <tr> <td>État de l'appel : </td> <td style=\"text-align: right;\"> <select name=\"etat\" style=\"width: 206px;\"> <option value=\"1 - Jamais Appelé\">1 - Jamais Appelé</option> <option value=\"2 - Contact établi\">2 - Contact établi</option> <option value=\"3 - Suspect\">3 - Suspect</option> <option value=\"4 - Prospect\">4 - Prospect</option> <option value=\"5 - Interessé\">5 - Interessé</option> <option value=\"6 - Gagné\">6 - Gagné</option> </select> </td> </tr> </table> } ?> So basicly I want the selectbox named "categorie" and "etat" to be already selected with the value contained in $info['categorie'] and $info['etat'] but I just can't figure out how to do it..... Please help!! Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/201682-simple-question-with-selectbox-value/ Share on other sites More sharing options...
stuartbates Posted May 13, 2010 Share Posted May 13, 2010 You need to construct the select options using an array then loop through checking for a match... $categories = array('Restaurant', 'Salon de coiffure', 'Sale de bronzage', 'Magasin de vetements', 'Magasin de détai', 'Centre commercial', 'Location Immobilier', 'Autre'); foreach ($categories as $value) { echo '<option value="' . $value . '"'; if ($value == $info['categorie']){ echo ' selected'; } echo '>' . $value . '</option>'; } Enjoy! Quote Link to comment https://forums.phpfreaks.com/topic/201682-simple-question-with-selectbox-value/#findComment-1057980 Share on other sites More sharing options...
patheticsam Posted May 13, 2010 Author Share Posted May 13, 2010 Thanks! i'll try it right away! Quote Link to comment https://forums.phpfreaks.com/topic/201682-simple-question-with-selectbox-value/#findComment-1057982 Share on other sites More sharing options...
patheticsam Posted May 13, 2010 Author Share Posted May 13, 2010 I'm sorry as I said I'm a little bit new to php and can't get it to work... here's how I made it: if( ($_GET['cmd']=="view") && (is_numeric($_GET['id'])) ) { $id=$_GET['id']; $data = mysql_query("SELECT * FROM opportunite WHERE id='$id'") or die(mysql_error()); while($info = mysql_fetch_array( $data )) { echo " <tr> <td style=\"text-align: center;\" colspan=\"2\"><h1>Fiche Client</h1></td> </tr> <tr> <td>Nom de l'entreprise : </td> <td style=\"text-align: right;\"><input type=\"text\" name=\"entreprise\" value=\"".$info['entreprise']."\" style=\"width: 200px;\"></td> </tr> <tr> <td>Date : </td> <td style=\"text-align: right;\"><input type=\"text\" name=\"date\" value=\"".$info['date']."\" style=\"width: 200px;\"></td> </tr> <tr> <td>Catégorie : </td> <td style=\"text-align: right;\"> <select name=\"categorie\" style=\"width: 206px;\"> "; $categories = array('Restaurant', 'Salon de coiffure', 'Sale de bronzage', 'Magasin de vetements', 'Magasin de détail', 'Centre commercial', 'Location Immobilier', 'Autre'); foreach ($categories as $value) { echo '<option value="' . $value . '"'; if ($value == $info['categorie']){ echo ' selected'; } echo '>' . $value . '</option>'; } echo " </select> </td> </tr> <tr> <td>État de l'appel : </td> <td style=\"text-align: right;\"> <select name=\"etat\" style=\"width: 206px;\"> <option value=\"1 - Jamais Appelé\">1 - Jamais Appelé</option> <option value=\"2 - Contact établi\">2 - Contact établi</option> <option value=\"3 - Suspect\">3 - Suspect</option> <option value=\"4 - Prospect\">4 - Prospect</option> <option value=\"5 - Interessé\">5 - Interessé</option> <option value=\"6 - Gagné\">6 - Gagné</option> </select> </td> </tr> dosen't work.... Quote Link to comment https://forums.phpfreaks.com/topic/201682-simple-question-with-selectbox-value/#findComment-1057987 Share on other sites More sharing options...
patheticsam Posted May 14, 2010 Author Share Posted May 14, 2010 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/201682-simple-question-with-selectbox-value/#findComment-1058048 Share on other sites More sharing options...
kenrbnsn Posted May 14, 2010 Share Posted May 14, 2010 "Doesn't work" doesn't tell us much. Explain. Ken Quote Link to comment https://forums.phpfreaks.com/topic/201682-simple-question-with-selectbox-value/#findComment-1058049 Share on other sites More sharing options...
patheticsam Posted May 14, 2010 Author Share Posted May 14, 2010 if( ($_GET['cmd']=="view") && (is_numeric($_GET['id'])) ) { $id=$_GET['id']; $data = mysql_query("SELECT * FROM opportunite WHERE id='$id'") or die(mysql_error()); while($info = mysql_fetch_array( $data )) { echo " <tr> <td style=\"text-align: center;\" colspan=\"2\"><h1>Fiche Client</h1></td> </tr> <tr> <td>Nom de l'entreprise : </td> <td style=\"text-align: right;\"><input type=\"text\" name=\"entreprise\" value=\"".$info['entreprise']."\" style=\"width: 200px;\"></td> </tr> <tr> <td>Date : </td> <td style=\"text-align: right;\"><input type=\"text\" name=\"date\" value=\"".$info['date']."\" style=\"width: 200px;\"></td> </tr> <tr> <td>Catégorie : </td> <td style=\"text-align: right;\"> <select name=\"categorie\" style=\"width: 206px;\"> "; $categories = array('Restaurant', 'Salon de coiffure', 'Sale de bronzage', 'Magasin de vetements', 'Magasin de détail', 'Centre commercial', 'Location Immobilier', 'Autre'); foreach ($categories as $value) { echo '<option value="' . $value . '"'; if ($value == $info['categorie']){ echo ' selected'; } echo '>' . $value . '</option>'; } echo " </select> </td> </tr> <tr> <td>État de l'appel : </td> <td style=\"text-align: right;\"> <select name=\"etat\" style=\"width: 206px;\"> <option value=\"1 - Jamais Appelé\">1 - Jamais Appelé</option> <option value=\"2 - Contact établi\">2 - Contact établi</option> <option value=\"3 - Suspect\">3 - Suspect</option> <option value=\"4 - Prospect\">4 - Prospect</option> <option value=\"5 - Interessé\">5 - Interessé</option> <option value=\"6 - Gagné\">6 - Gagné</option> </select> </td> </tr> The select box dosen't primary select the value contained into the db ( $info['categorie'] ) .....Can't find how to set it up.... Thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/201682-simple-question-with-selectbox-value/#findComment-1058051 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.