Jump to content

Could someone tell me why this code does not display the first record found


Recommended Posts

I use the code below to populate a combo box from a Mysql database it works with the exception that it fails to display the first record. Could someone tell me where I am going wrong and how to fix it.

<?
$sql="SELECT id, Type FROM Listings_t";
$result=mysql_query($sql);
$options_t="";
$No_T =mysql_num_rows($result);
while ($row=mysql_fetch_array($result)) {
    $id=$row["id"];
    $value=$row["Type"];
    $options_t.="<OPTION VALUE=\"$id\">".$value.'</option>';
}
echo $options_t
?>
Link to comment
Share on other sites

You need to echo $options_t within the while loop.

while ($row=mysql_fetch_array($result)) {
    $id=$row["id"];
    $value=$row["Type"];
    echo "<OPTION VALUE=\"$id\">".$value.'</option>';
}


why it doesn't display the first record is because $options_t changes with each query ( 2 entries in the database = 2 queries) . when not displayed within the loop it has to display the last result
Link to comment
Share on other sites

[code]<?
$sql="SELECT id, Type FROM Listings_t";
$result=mysql_query($sql);
$options_t="";
$No_T =mysql_num_rows($result);
while ($row=mysql_fetch_array($result)) {
    $id=$row["id"];
    $value=$row["Type"];
    echo "<OPTION VALUE=\"$id\">" .$value. "</option>";
?>[/code]
Link to comment
Share on other sites

He doesn't have to echo anything inside the while loop.  However, where are your select statements?
[code=php:0]
<?php

$options_t = '<select name="menu">';
while ($row=mysql_fetch_array($result)) {
    $id=$row["id"];
    $value=$row["Type"];
    $options_t .= "<OPTION VALUE=\"$id\">" .$value. "</option>";
}
$options_t .= '</select>';
?>
[/code]

He only made one query.  When he used mysql_fetch_array, its getting the data from the resource (1 query) and placing it into an array.
Link to comment
Share on other sites

try this ok

<?php

$options_t = '<select name="menu">';
while ($row=mysql_fetch_assoc($result)) {
    $id=$row["id"];
    $value=$row["Type"];
    $options_t .= "<OPTION VALUE=\"$id\">" .$value. "</option>";
}
$options_t .= '</select>';
?>
Link to comment
Share on other sites

Redarrow, thanks tried it ..failed to display any records.

Here is the full form code.. basically three combo boxes and a submit button (the browse all button has not yet been coded..)

Please note I am echoing the $No_A, $No_T and $No_V variables to  see how many records are being returned - they do match the number of records in each table.

Cheers DD

Here is the code extracted and placed in a page:

<head>
  <title></title>
</head>

<body>

<!-- This is the Quicksearch Form -->
<div id="Form_QSLYR" style="border-left-color:  rgb(255,102,51); border-top-color:  rgb(255,102,51); border-right-color:  rgb(255,102,51); border-bottom-color:  rgb(255,102,51);"><layer id="Form_QSLYR" visibility="inherit" top="166" left="6" width="216" height="184" z-index="5">
<form name="FORM_QS" action="./html/results.php" target="Iframe" method="POST">
        <table id="Form_QS" border="1" BORDERCOLOR="#FF6633" cellspacing="1" cellpadding="1" width="215" height="162">
        <tr height="16">
          <td width="201" style="font-family: Arial,Helvetica,Geneva,Sans-serif; font-size: 14px; color: rgb(255,255,255); background-color: rgb(255,102,51);">
          <p style="text-align: center;"><span style="color: rgb(255,255,255);">Quick Property Search</span></p>
          </td>
        </tr>
        <tr height="27">
          <td>
          <p>
            <table width="34" border="0" cellspacing="0" cellpadding="0" align="left" nof="te">
            <tr>
              <td class="TextObject" style="font-family: Arial,Helvetica,Geneva,Sans-serif; font-size: 12px; color: rgb(0,0,255); text-align: left;">
              <p><b><span style="color: rgb(255,102,51);">&nbsp;Area</span></b></p>
              </td>
            </tr>
            </table>
<select id="CB_A" name="CB_A" style="font-family: Arial,Helvetica,Geneva,Sans-serif; font-size: 10px; color: rgb(255,102,51);"
<?
$sql="SELECT id, Area FROM Listings_a";
$result=mysql_query($sql);
$options_a="";
$No_A =mysql_num_rows($result);
while ($row=mysql_fetch_array($result)) {
    $id=$row["id"];
    $value=$row["Area"];
    $options_a.="<OPTION VALUE=\"$id\">".$value.'</option>';
}
echo $options_a
?>
</select>
            <b><span style="font-family: Arial,Helvetica,Geneva,Sans-serif; font-size: 10px; color: rgb(255,102,51);"><?echo $No_A?></span></b></p>
          </td>
        </tr>
        <tr height="27">
          <td>
          <p>
            <table width="33" border="0" cellspacing="0" cellpadding="0" align="left" nof="te">
            <tr>
              <td class="TextObject" style="font-family: Arial,Helvetica,Geneva,Sans-serif; font-size: 12px; color: rgb(0,0,255); text-align: left;">
              <p><b><span style="color: rgb(255,102,51);">&nbsp;Cost</span></b></p>
              </td>
            </tr>
            </table>
<select id="CB_V" name="CB_V" style="font-family: Arial,Helvetica,Geneva,Sans-serif; font-size: 10px; color: rgb(255,102,51);"
<?
$sql="SELECT id, Value FROM Listings_v";
$result=mysql_query($sql);
$options_v="";
$No_V =mysql_num_rows($result);
while ($row=mysql_fetch_array($result)) {
    $id=$row["id"];
    $value=$row["Value"];
    $options_v.="<OPTION VALUE=\"$id\">".$value.'</option>';
}
echo $options_v
?>
            </select>
            <b><span style="font-family: Arial,Helvetica,Geneva,Sans-serif; font-size: 10px; color: rgb(255,102,51);"><?echo $No_V?></span></b></p>
          </td>
        </tr>
        <tr height="27">
          <td>
          <p>
            <table width="34" border="0" cellspacing="0" cellpadding="0" align="left" nof="te">
            <tr>
              <td class="TextObject" style="font-family: Arial,Helvetica,Geneva,Sans-serif; font-size: 12px; color: rgb(0,0,255); text-align: left;">
              <p><b><span style="color: rgb(255,102,51);">&nbsp;Type</span></b></p>
              </td>
            </tr>
            </table>
<select id="CB_T" name="CB_T" style="font-family: Arial,Helvetica,Geneva,Sans-serif; font-size: 10px; color: rgb(255,102,51);"
<?
$sql="SELECT id, Type FROM Listings_t";
$result=mysql_query($sql);
$options_t="";
$No_T =mysql_num_rows($result);
while ($row=mysql_fetch_array($result)) {
    $id=$row["id"];
    $value=$row["Type"];
    $options_t.="<OPTION VALUE=\"$id\">".$value.'</option>';
}
echo $options_t
?>
            </select>
            <b><span style="font-family: Arial,Helvetica,Geneva,Sans-serif; font-size: 10px; color: rgb(255,102,51);"><?echo $No_T?></span></b></p>
          </td>
        </tr>
        <tr height="27">
          <td style="border-left-style: none; border-top-style: none; border-right-style: none; border-bottom-style: none;">
          <p style="text-align: center;"><input type="submit" name="Button_QS" style="color: rgb(255,0,0); font-weight: bold;" value="Search" id="Button_QS"></p>
          </td>
        </tr>
<!-- This is the BrowseAll Button on the Quick Search Form -->
        <tr height="27">
          <td>
          <p style="text-align: center;"><input type="button" name="Button_BAL" style="color: rgb(255,0,0); font-weight: bold;" value="Browse All Listings" id="Button_BAL"></p>
          </td>
        </tr>
        </table>
      </form>
      </layer></div>


</body>

</html>
Link to comment
Share on other sites

I apologise,I forgot to include the database conn when I extracted the code...!

However, it would be fair to assume from my previous posts that the dbase conn works as the combo boxes get populated except for the first record. and the $No_x vars count the records which they of course would not if the dbconn failed.

The issues remains...first record not being displayed in the combo boxes... any ideas?

Link to comment
Share on other sites

WF, interestingly enough, the rendered source shows all records being returned yet the first option (option value="1") still fails to be displayed..here is an extract..

<select id="CB_V" name="CB_V" style="font-family: Arial,Helvetica,Geneva,Sans-serif; font-size: 10px; color: rgb(255,102,51);"
<OPTION VALUE="1">Any</option>
<OPTION VALUE="2">Below 10,000</option>
<OPTION VALUE="3">10,001 to 25,000</option>
<OPTION VALUE="4">25,001 to 50,000</option>
<OPTION VALUE="5">50,001 to 100,000</option>
<OPTION VALUE="6">100,001 to 200,000</option>
<OPTION VALUE="7">200,001 to 500,000</option>
<OPTION VALUE="8">500,001 to 1,000,000</option>
<OPTION VALUE="9">1,000,001 to 5,000,000</option>
</select>
<b><span style="font-family: Arial,Helvetica,Geneva,Sans-serif; font-size: 10px; color: rgb(255,102,51);">9</span></b></p>
Link to comment
Share on other sites

Barand,

I'm not sure what you mean by your post, but he does need to close his <select> tag with a >
<select id="CB_V" name="CB_V" style="font-family: Arial,Helvetica,Geneva,Sans-serif; font-size: 10px; color: rgb(255,102,51);">
<OPTION VALUE="1">Any</option>
<OPTION VALUE="2">Below 10,000</option>
<OPTION VALUE="3">10,001 to 25,000</option>
<OPTION VALUE="4">25,001 to 50,000</option>
<OPTION VALUE="5">50,001 to 100,000</option>
<OPTION VALUE="6">100,001 to 200,000</option>
<OPTION VALUE="7">200,001 to 500,000</option>
<OPTION VALUE="8">500,001 to 1,000,000</option>
<OPTION VALUE="9">1,000,001 to 5,000,000</option>
</select>
Link to comment
Share on other sites

select box example ok.



<select name="name">
<?

$db=mysql_connect("xxxx","xxxx","xxxx");
mysql_select_db("xxxx",$db);

$query="SELECT * from xxxx";
$result=mysql_query($query);

while($record=mysql_fetch_assoc($result)){


echo "<option value='".$record["what_ever"]."'>".$record['what_ever']."</option>";

}
?>

</select>
Link to comment
Share on other sites

Got it...now i understand.... yes the <select......... needed to be closed with >
thanks guys and I apologise for my stupidity.

<select id="CB_T" name="CB_T" style="font-family: Arial,Helvetica,Geneva,Sans-serif; font-size: 10px; color: rgb(255,102,51);">      <-----the missing bit!
Link to comment
Share on other sites

heres even an  easer example to understand ok

<select name="name">
<?

$db=mysql_connect("localhost","xxx","xxx");
mysql_select_db("xxx",$db);

$query="SELECT * from xxx";
$result=mysql_query($query);

while($record=mysql_fetch_assoc($result)){

$name=$record['what_ever'];
?>
echo "<option value='<?echo $name; ?>'> <?echo $name; ?> </option>";


<?}?>

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