Jump to content

Simple question with selectbox value....


patheticsam

Recommended Posts

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!!

 

Link to comment
https://forums.phpfreaks.com/topic/201682-simple-question-with-selectbox-value/
Share on other sites

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!

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....

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.