Hello
I want an array to appear in a form as a drop down menu.
I am struggling with the foreach code.

So far I have got

<TD WIDTH="50%" HEIGHT="32"><FONT FACE="Arial, Helvetica, sans-serif">Client:</FONT></TD>
<TD WIDTH="50%"><FONT FACE="Arial, Helvetica, sans-serif"> 
<?php 
echo '<select name="client"> 
<option selected>choose</option> '
foreach($CoName as $value){ 
printf("<option>".$value."</option>"); 
} 
echo  '</select>'?>
</FONT></TD><TR>

but this doesn't work - Parse error: parse error, expecting ','' or';'' in the line foreach($CoName as $value)

By the way, the variable $CoName comes from a query earlier in the code that I have echo'd out to test and it does work......

Firstly is the use of foreach correct in this type of code request?

Secondly what should the correct syntax be, I have looked in the manual and cannot see any difference to what I have in my code.

thanks

    Hi,
    you are missing the ; after the echo before the foreach...

      thanks bad76,

      added the ; so it now reads

      echo '<select name="client"> 
      <option selected>choose</option> ; '
      foreach($CoName as $value){ 
      printf("<option>".$value."</option>"); 
      } 
      echo  '</select>'
      

      but still get the same parse error: parse error, expecting ','' or';'' in the line foreach($CoName as $value)

      any ideas?
      thanks

        Nononono, outside the string, so that PHP will read it!

        echo '<select name="client">
        <option selected>choose</option>';
        foreach($CoName as $value){ 
        printf("<option>".$value."</option>"); 
        } 
        echo  '</select>'

          thanks weedpacket - am I dumb or what !!?!

          never mind, this has resolved the parse error, but the code doesn't quite do what I was wanting.
          the output in the drop down menu just says 'choose' (the option I set up in the code) it doesn't display the array information from the query.

          The query to define the array is as follows;

          $query = "SELECT `CompanyRef` FROM `tblUser` WHERE `CLogIn` = '$username'";
          $result = mysql_query($query)
          	or die(mysql_error());
          $row = mysql_fetch_array($result);
          $Company = $row["CompanyRef"];
          
          $query = "SELECT `CoName` FROM `tblClient` WHERE `CompanyRef` = '$Company' ";
          $result1 = mysql_query($query)
          	or die(mysql_error());
          $row1 = mysql_fetch_array($result1);
          $CoName = $row1["CoName"]; 
          

          ($username comes as a variable from the login page)

          I know that there are 2 values in the array created by this query - ie 2 CoName's - and I wanted these 2 values (or however many there are in the array) to be shown in the drop down menu, but this isn't happening.

          What do I need to do with this code;

          <TD WIDTH="50%" HEIGHT="32"><FONT FACE="Arial, Helvetica, sans-serif">Client:</FONT></TD> 
          <TD WIDTH="50%"><FONT FACE="Arial, Helvetica, sans-serif"> 
          <?php  
          echo '<select name="client"> <option selected>choose</option> ' ; foreach($CoName as $value){
          printf("<option>".$value."</option>");
          }
          echo '</select>'?> </FONT></TD><TR>

          to make this happen

          thanks

            $CoName = $row1["CoName"];

            ...But this don't do an array. This make a sngle assignement.

            May be you means this:

             
            $result1 = mysql_query($query)
                or die(mysql_error());
            while($row1 =  mysql_fetch_array($result1))
              $CoName[] = $row1["CoName"];
            

            This give you an array named $CoName and containing all the db entry in the resultset.

            bye bye

              thank you for making that clear.
              However I still need some advise on how to complete the code.
              I have sucessfully created the array. I have tested the array by echoing it out and I know there are 2 values that have been recognised in the array.

              However I still cannot get these values to show in the menu in the form........
              the code I have so far
              1) to create the array

              $query = "SELECT `CompanyRef` FROM `tblUser` WHERE `CLogIn` = '$username'";
              $result = mysql_query($query)
              	or die(mysql_error());
              $row = mysql_fetch_array($result);
              $Company = $row["CompanyRef"];
              
              $query = "SELECT `CoName` FROM `tblClient` WHERE `CompanyRef` = '$Company'";
              $result1 = mysql_query($query)
              	or die(mysql_error());
              while($row1 =  mysql_fetch_array($result1)) 
                $CoName[] = $row1["CoName"];
              

              2) to create the drop down menu in the form

              <TR><TD WIDTH="15%">&nbsp;</TD>
              <TD WIDTH="25%" HEIGHT="32"><FONT FACE="Arial, Helvetica, sans-serif">Client:</FONT></TD>
              <TD WIDTH="35%"><FONT FACE="Arial, Helvetica, sans-serif"> 
              <?php 
              echo '<select name="client"> 
              <option selected>choose</option>  ';
              foreach($CoName[] as $value){ 
              printf("<option>".$value."</option>"); 
              } 
              echo  '</select>'?>
              <FONT FACE="Arial, Helvetica, sans-serif"> </FONT></FONT></TD>
              <TD WIDTH="25%">&nbsp;</TD></TR>
              

              Like I say, this doesn't work, all I get in the drop down menu is 'choose'.

              Can anyone see where I am going wrong with this - thanks

                The $CoName[] in the foreach loop should be just $CoName (the [] is for putting things into the array).

                Incidentally, you don't need two queries to do what you're doing - you should be able to drop the first query (and subsequent code) and change the second query to

                SELECT CoName
                FROM tblClient, tblUser
                WHERE tblUser.CLogin='$username'
                      AND tblUser.CompanyRef=tblClient.CompanyRef
                

                  BIG THANK YOU...........

                  I have got the array working now in the drop down menu

                  BTW, very nice shortcut on the query code - works a treat.

                    
                    <?php  
                    
                    echo "<select name=\"client\"> 
                    <option selected>choose";' 
                    foreach($CoName as $value){  
                    echo "<option>$value";
                    }
                    echo "</select>"; ?>
                      Write a Reply...