Jump to content

Break in an out of code


fife

Recommended Posts

Im having trouble writing this code as im not very good at breaking in an out of an echo.  Here is the code.  It has an error somewhere in it.  Ithink its something to do with the if statement as it was fine until i added that.

 

    echo "<option value=\"{$a[$id]}\" " .if($row_rs_doc['network']==$a['id']){echo"selected";} . ">$a[$field]</option>";

Link to comment
Share on other sites

cool thanks but that brings back an error too.  Here is the view source

 

 

<select name="networks"  id="networks">

              <option value="24" <option value="33" <option value="14" <option value="23" <option value="35" <option value="19" <option value="7" <option value="2" <option value="6" <option value="34" <option value="32" <option value="12" <option value="1" <option value="31" <option value="25" <option value="15" <option value="4" <option value="8" <option value="28" <option value="22" <option value="13" <option value="30" <option value="26" <option value="27" <option value="16" <option value="9" <option value="21" <option value="20" <option value="10" <option value="11" <option value="3" <option value="29" <option value="36"

Notice: Undefined variable: row_rs_doc in /home/sites/members/edit-documents.php on line 254

selected></option>

          </select>

 

Basically this is the code that works

 

echo "<option value=\"{$a[$id]}\">$a[$field]</option>";

 

I just want to edit it so that if $row_rs_doc['network'] == the option id echo the selected as this row is from an edit form

Link to comment
Share on other sites

Notice: Undefined variable: row_rs_doc in /home/sites/members/edit-documents.php on line 254 is the error i get. 

 

The thing is though if i echo $row_rs_doc['network'] into the page i get a value so I know its there. further more if I echo <option value=\"{$a[$id]}\">$a[$field]</option> I get <option value="33">Asset Management Plan</option> So i know both of them are there.

 

If i echo

echo "<option value=\"{$a[$id]}\" ";
if($row_rs_doc['network']==$a['id']){
echo"selected";
} 
echo ">". $a[$field]. "</option>";

 

then i get

 

<option value="24" <option value="33" etc with that error at the end.  There are no other errors on the page.

 

Is it not something to do with the syntax?  To be honest im just guessing an its driving me insane

Link to comment
Share on other sites

It may be set in some iterations of the loop and not set in others. Try something like this:

echo "<option value=\"{$a[$id]}\" ";
if(isset($row_rs_doc['network']) && $row_rs_doc['network']==$a['id']){
echo"selected";
} 
echo ">". $a[$field]. "</option>";

Link to comment
Share on other sites

Ok I think that's nearly it.  There are at least no php errors flagging up but there is an error.  First ill show the code then Ill explain the problem.

thanks to everyone that helped I now have this for my function

function createoptions($table , $id , $field)
{
    $sql = "select * FROM $table ORDER BY $field";
    $res = mysql_query($sql) or die(mysql_error());
    
while ($a = mysql_fetch_assoc($res)){
echo "<option value=\"{$a[$id]}\" ";
                        if(isset($row_rs_doc['network']) && $row_rs_doc['network']==$a['id']){ echo"selected";}
echo ">". $a[$field]. "</option>";
}
}
// and in the form I have this
<select name="networks" id="networks">
     
                  <?php  createoptions("networks", "idnetworks", "netname");   ?>  
     
      </select>
     

 

This is an edit page.  I have a query that pulls the data down.  If i echo out $row_rs_doc['network'] I get a number so I know that has a value.  However when I view the page the item that is selected as selected is the last item in the list.  The number for $row_rs_doc['network'] on the loaded page is 1 yet the selected item is ......

<select name="networks" id="networks">
     
                  <option value="24" selected>Admissions</option><option value="33" selected>Asset Management Plan</option><option value="14" selected>Capital</option><option value="23" selected>Child Trust Rep</option><option value="35" selected>Childrens Centre Rep</option><option value="19" selected>Childrens University</option><option value="7" selected>Conference</option><option value="2" selected>Core</option><option value="6" selected>CPD</option><option value="34" selected>Early Years</option><option value="32" selected>Emergency Planning</option><option value="12" selected>Forthright</option><option value="1" selected>General</option><option value="31" selected>Healthy Schools</option><option value="25" selected>High Cost</option><option value="15" selected>Home</option><option value="4" selected>Horizon</option><option value="8" selected>ICT</option><option value="28" selected>In Year Fair Access</option><option value="22" selected>LSCB</option><option value="13" selected>MADCOS</option><option value="30" selected>NCSL Rep</option><option value="26" selected>NW PHA Link</option><option value="27" selected>SACRE</option><option value="16" selected>SAPH Doc Store</option><option value="9" selected>Schools Forum</option><option value="21" selected>SEN Review</option><option value="20" selected>Social Care</option><option value="10" selected>Southport</option><option value="11" selected>Stanley</option><option value="3" selected>Strand</option><option value="29" selected>Support Services</option><option value="36" selected>Traded Services Evalution</option>  
     
      </select>

 

All i want is to show the correctly selected item which should be "general" as the value there is 1  thanks

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.