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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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