0

Im trying to make a datalist which will have the options as:

ItemCode - Name

Code and Name are in the same table but are in different columns

Ive got it working for just code:

<?php 
  while($res=mysql_fetch_row($code))
   {
    $fullname=$res[0];
    echo "<option value=$fullname></option>";
   }
 ?>

I tried doing the following for both:

<?php 
 while(($res=mysql_fetch_row($code)) && ($res2=mysql_fetch_row($name)))
 {
   $fullname=$res[0]." - ".$res2[0];
   echo "<option value=$fullname></option>";
 }
?>

However I had no joy, any help would be greatly appreciated.

1
  • Perhaps try to debug what is inside your $res array - i.e. var_dump($res). What you probably want is something like $res['1'] . '-' . $res['2'] Commented Feb 17, 2014 at 3:23

3 Answers 3

1
echo "<option value=$fullname>$fullname</option>";
//instead of $module?
Sign up to request clarification or add additional context in comments.

1 Comment

Apologies, that was me missing one when I changed the names of the variables from the originals.
1

Try this:

<?php
$query="select ItemCode,Name from tablename";
$result=mysql_query($query);

$cols=2;        
echo "<table>";
do{
    echo "<tr>";
    for($i=1;$i<=$cols;$i++)
    {   $row=mysql_fetch_array($result);
        ?>
             <td>
                 <table>
                  <tr valign="top">

                    <td>
                    <b><?=$row['ItemCode'] ?></b><br />
                    <?=$row['Name'] ?><br />

                    </td>
                    <td width="50">&nbsp;</td>  <!-- Create gap between columns -->
                     </tr>
                </table>
            </td>
            <?
    }
} while($row);
echo "</table>";
?>

Comments

0

You may want to use something like this:

while ($row = mysql_fetch_assoc($result)) {
    $value = $row['item_code'] . " - " . $row['name'];
    echo '<option value="' . $value . '">' . $value . '</option>';
}

Note: mysql_query, mysql_fetch_row and mysql_fetch_assoc are deprecated, use something else.

This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.