Re: Re: enhanced_list_box, 2 tables from a database

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 11/7/07, Sebastian Hopfe <s.hopfe@xxxxxxx> wrote:
>
> Dear kNish,
>
> first of all i have formated your PHP Code it looks better now.
>
> <?php
>
>   echo "<tr>\n";
>   echo "  <td height=\"33\">&nbsp;</td>\n";
>   echo "  <td width=\"14%\" class=\"style3\">Artist</td>\n";
>
>   $options = mysql_query("SELECT artist_name FROM artist");
>   $options=mysql_fetch_array($options);
>
>   echo '<td>';
>
>   enhanced_list_box(array(
>     'table' => 'artist',
>     'value_field' => 'artist_name'));
>
>   function enhanced_list_box($options)
>   {
>     $sql = "select " . $options['value_field'];
>     $sql .= " from " . $options['table'];
>     $result = mysql_query($sql)or die("error in SQL");
>
>     echo '<select name="', $options['value_field'],'" size="1">';
>     while ($row = mysql_fetch_array($result, MYSQL_NUM))
>       echo '<option value="' . $row[0] . '">' . $row[0] . '</option>';
>     echo '</select>';
>   }
>
>   echo '  </td>';
>   echo "</tr>\n";
>
>   echo "<tr>\n";
>   echo "  <td height=\"33\">&nbsp;</td>\n";
>   echo "  <td width=\"14%\" class=\"style3\">Project</td>\n";
>
>   $options = mysql_query("SELECT project_name FROM project");
>   $options=mysql_fetch_array($options);
>
>   echo '<td>';
>
>   enhanced_list_box(array(
>     'table' => 'project',
>     'value_field' => 'project_name'));
>
>   function enhanced_list_box($options)
>   {
>     $sql = "select " . $options['value_field'];
>     $sql .= " from " . $options['table'];
>     $result = mysql_query($sql)or die("error in SQL");
>
>     echo '<select name="', $options['value_field'],'" size="1">';
>
>     while ($row = mysql_fetch_array($result, MYSQL_NUM))
>       echo '<option value="' . $row[0] . '">' . $row[0] . '</option>';
>
>     echo '</select>';
>   }
>
>   echo '</td>';
> ?>
>
> After i had check your code, my php interpreter shows me an fatal error on
> the browser.
>
>
> "kNish" <singhai.nish@xxxxxxxxx> schrieb im Newsbeitrag
> news:81bfef2e0711070041p1a2ed9b3p4b972a5c985a7e8f@xxxxxxxxxxxxxxxxx
> > Hi,
> >
> > A newbie question. I have more than one table to access from a database.
> >
> > When I use the code as below, it gives no response on the web page.
> >
> > What may I do to run more than one table from the same database into the
> > script.
> >
> > BRgds,
> >
> > kNish
> >
> > .
> > .
> > .
> > .
> > <tr>
> > <td height="33">&nbsp;</td>
> > <td width="14%" class="style3">Artist</td>
> > <?php
> > $options = mysql_query("SELECT artist_name FROM artist");
> > $options=mysql_fetch_array($options);
> > echo '<td>';
> > enhanced_list_box(array(
> > 'table' => 'artist',
> > 'value_field' => 'artist_name'));
> > function enhanced_list_box($options){
> > $sql = "select " . $options['value_field'];
> > $sql .= " from " . $options['table'];
> > $result = mysql_query($sql)or die("error in SQL");
> > echo '<select name="', $options['value_field'],'" size="1">';
> > while ($row = mysql_fetch_array($result, MYSQL_NUM))
> > echo '<option value="' . $row[0] . '">' . $row[0] . '</option>';
> > echo '</select>';
> > }
> > echo '</td>';
> > ?>
> > </tr>
> >
> > <tr>
> > <td height="33">&nbsp;</td>
> > <td width="14%" class="style3">Project</td>
> > <?php
> > $options = mysql_query("SELECT project_name FROM project");
> > $options=mysql_fetch_array($options);
> > echo '<td>';
> > enhanced_list_box(array(
> > 'table' => 'project',
> > 'value_field' => 'project_name'));
> > function enhanced_list_box($options){
> > $sql = "select " . $options['value_field'];
> > $sql .= " from " . $options['table'];
> > $result = mysql_query($sql)or die("error in SQL");
> > echo '<select name="', $options['value_field'],'" size="1">';
> > while ($row = mysql_fetch_array($result, MYSQL_NUM))
> > echo '<option value="' . $row[0] . '">' . $row[0] . '</option>';
> > echo '</select>';
> > }
> > echo '</td>';
> > ?>
> > </tr> <http://www.php.net/a>



Let's clean up a lil' bit. First, define your logic, then present what you
need to. For example:

<?php
function enhanced_list_box ($options)
{
   $sql = "SELECT " . $options['value_field'].
      " FROM " . $options['table'];
   $result = mysql_query($sql) or die("error in SQL");

   $list = '<select name="' .
      htmlentities ($options['value_field'], ENT_QUOTES) .
      '" size="1">';

   while ($row = mysql_fetch_array($result, MYSQL_NUM))
      $list .= '<option value="' . htmlentities ($row[0], ENT_QUOTES) .
         '">' . htmlentities ($row[0], ENT_QUOTES) . '</option>';

   $list .= '</select>';
   return $list;
}

// Get however many lists you want
$selectA = enhanced_list_box ($optionsA);
$selectB = enhanced_list_box ($optionsB);
?>
...
<table>
  <tr>
    <td><?php echo $selectA; ?></td>
    <td><?php echo $selectB; ?></td>
  </tr>
  ...
</table>

As Robin pointed out, you defined the function twice - not needed (or
allowed). You should attempt to separate your logic from your presentation -
this helps with readability, upgrading, and is good practice (along with a
lengthy list of other reasons).

There's a starting point. Hope that helps.

~Philip

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux