Page 1 of 1

Can't work out how to fit some code in

Posted: Sun Jul 12, 2009 1:15 pm
by Addos
I need to have this bit of code

Code: Select all

echo($dbRow["counties_idpk"] == $row_GetLocSel['counties_idfk'] ? "selected='selected'" : "") ;
output in the code below roughly line 16 below. See commented out section below. However I just can’t seem to work out how to get this to work as it will be outside of the while loop where it’s created in the query at the bottom of this script and thus throw an undefined error. I’m sure it’s obvious but I cannot get my head around this.
Any ideas as to how I can insert this without errors?

Code: Select all

<?php
    // Query for insert for multipal select locations
    mysql_select_db($database_*****, $*****);
    $query_GetLocations = "SELECT * FROM loc ORDER BY cou_idpk ASC";
    $GetLocations = mysql_query($query_GetLocations, $*****) or die(mysql_error());
    $totalRows_GetLocations = mysql_num_rows($GetLocations);
    
    echo "<select size=\"5\" multiple=\"multiple\" name='counties_idpk[]' >\n >";
    
    while($dbRow = mysql_fetch_array($GetLocations)){
    echo  "<option value='"
          . $dbRow["counties_idpk"]
          . "'" 
          . "'>"
          . $dbRow["counties_name"];  
        // NEED THIS CODE IN HERE BUT IT WONT WORK THIS WAY AS IT STANDS
        //echo ($dbRow["counties_idpk"] == $row_GetLocSel['counties_idfk'] ? "selected='selected'" : "") ;    
    mysql_select_db($database_*****, $*****);
    $query_GetLocSel = 
    "SELECT * 
    FROM users_locations_test
    WHERE counties_idfk = '$dbRow[counties_idpk]' ";
    $GetLocSel = mysql_query($query_GetLocSel, $*****) or die(mysql_error());
    $totalRows_GetLocSel = mysql_num_rows($GetLocSel);
 
    while($row_GetLocSel = mysql_fetch_assoc($GetLocSel)) {
    
    echo ($dbRow["counties_idpk"] == $row_GetLocSel['counties_idfk'] ? "selected='selected'" : "") ;
    
    echo "</option>\n"; 
    
    }
}   

Re: Can't work out how to fit some code in

Posted: Sun Jul 12, 2009 3:26 pm
by requinix
The stuff on lines 11-15 writes the opening <option> and the label. Your code should go right in between those two.

You need to change it to write
- The <option
- The selected=selected if necessary
- The > and the option value
Split lines 11-15 into separate parts (so it's not one statement) then move some of them down around line 27 and some around line 29.

Re: Can't work out how to fit some code in

Posted: Mon Jul 13, 2009 6:31 am
by Addos
Thanks for the pointers. I got this finally sorted.
Thanks again