开发者

Need help pulling into multiple columns from one database table - mysql_query

Afternoon all,

A very quick question... a friend has set up a form for me using mysql_query. Since he wrote this, I have added an extra column into the database, which I want to pull through into the form.

However I can't seem to get this extra column to appear (labelled Currency). The reason I need it is the query below will pulls back a value and the £ symbol. Because I want to display not only £, but also € prices, I need this extra column to pull through (obviously I will have to remove the £ from the echo below too).

I've tried adding the extra column (Currency) to the code, e.g. "SELECT Room, Currency, Price FROM Spa_Upgrades

but this hasn't worked.

The code is:

 <?php 
            if (isset($id))
                {
                $query2 = mysql_query("SELECT Room, Price FROM Spa_Upgrades WHERE Spa_Upgrades.Spa_Id = '".$id."' AND Spa_Upgrades.Id = '".$pack."' order by Spa_Upgrades.Order");
                $rows = mysql_num_rows($query2);
                if($rows==0) {echo "disabled='disabled'/>";} 
                else
                    {
                    echo "/>";
                    echo "<option value='Default'>Please Select</option>";
                    for($i=0; $i<$rows; $i++)
                        {
                        开发者_运维百科$result2 = mysql_fetch_array($query2);
                        echo "<option value='".$result2[0]."&nbsp;&pound;".$result2[1]."pp'>$result2[0]&nbsp;&pound;$result2[1]pp</option>";
                        }
                    }
                }

Hugely grateful if someone can solve this!

Thanks, Motley


Alter the query as follows:

SELECT Room, Price, Currency FROM Spa_Upgrades ...

Alter the line beginning echo inside the for loop: replace &pound; with $result2[2] wherever it appears. (Or if the Currency column doesn't contain the HTML entity for the currency symbol, then replace &pound; with appropriate code to obtain the symbol from the Currency column entry.)


You also need to add the column to the output... I would also switch to an associative array otherwise if you add a column and its not at the end you have to change all the indexes.

        if (isset($id))
            {
            $query2 = mysql_query("SELECT Room, Price, Currency FROM Spa_Upgrades WHERE Spa_Upgrades.Spa_Id = '".$id."' AND Spa_Upgrades.Id = '".$pack."' order by Spa_Upgrades.Order");
            $rows = mysql_num_rows($query2);
            if($rows==0) {echo "disabled='disabled'/>";} 
            else
                {
                echo "/>";
                echo "<option value='Default'>Please Select</option>";
                for($i=0; $i<$rows; $i++)
                    {
                    $result2 = mysql_fetch_assoc($query2);
                    $value = $result2['Room'] . '&nbsp;' . $result2['Currency'].$result2['Price'].'pp';

                    echo sprintf('<option value="%s">%s</option>', $value, $value);
                    }
                }
            }


  • Use mysql_fetch_assoc() instead of mysql_fetch_array()
  • A good practice as well is to separate data retrieving from display logic

Try this:

<?php

$results = array();

if (isset($id))
{
  $resource = mysql_query("SELECT room, currency, price FROM Spa_Upgrades
    WHERE Spa_Upgrades.Spa_Id = '".intval($id)."' AND Spa_Upgrades.Id = '".intval($pack)."'
    order by Spa_Upgrades.Order");

  while($row = mysql_fetch_assoc($resource))
    $results[] = $row;
}

?>
<select name="..." <?php echo (count($results) > 0 ? '' : 'disabled') ?>>
  <option value="Default">Please Select</option>
  <?php
    foreach($results as $result)
    {
      $value = $result['room'].' '.$result['currency'].$result['price'].'pp';
      echo '<option value="'.htmlspecialchars($value).'">'.htmlspecialchars($value).'</option>'."\n";
    }
  ?>
</select>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜