Can't work out how to fit some code in

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Can't work out how to fit some code in

Post 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"; 
    
    }
}   
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

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

Post by Addos »

Thanks for the pointers. I got this finally sorted.
Thanks again
Post Reply