www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: List Options default problem

  1. #1
    Join Date
    Dec 2006
    Posts
    16

    List Options default problem [FIXED!]

    This one should be easy for someone to explain, but for the life of me, despite hours of looking around or increasingly annoying syntax errors I just cant seem to get it to do what I want.

    This is a portion of code from an editing page in the admin area of my site.

    Its obvious what it does I think, but anyway, I want to be able to edit sql entries, which it does just fine and dandy, however, the geo_id is in the form of a drop down list, which it calls correctly and everything displays fine. It even posts fine when I make changes.

    What I would really like it to do however, is display the correct "geo_id" when it retrieves the information. Currently, no matter what the geo_id of the country is in the sql, it always defaults to the first one in the list (sorted by asc from the sq query).

    Question : What am I doing wrong?

    I realise the issue is around this area
    Code:
    echo"<option value=\"".$geo_info['geo_id']."\">".$geo_info['geo_name']."</option>";
    I also realise I need to tell the option value to use the default (in this case from the country I am editing, which is stored under $myrow['geo_id], but I am just not sure how to get it into that piece of code above.

    Any help would be appreciated as ive now run out of ciggys and drunk too much coffee

    Full code below.

    Code:
    <table width="600" align="center" cellspacing="0" cellpadding="0" border="0">
    			<tr>
    <form action="<?php echo "http://".$host."/ranks/admin/?action=edit_country";?>" method="post">
     <input type=hidden name="country_id" value="<?php echo $myrow["country_id"] ?>"></tr>
    <tr>
    <td> Country :</td><td><INPUT TYPE="TEXT" NAME="country_name" VALUE="<?php echo $myrow["country_name"] ?>"></td>
    </tr>
    <tr>
    <td>Flag File :</td><td><INPUT TYPE="TEXT" NAME="country_flag" VALUE="<?php echo $myrow["country_flag"] ?>"></td>
    </tr>
    <tr>
    <td>GeoGraphy :</td><td><select name="geo_id" class="event" >
    					<?php
    					$sql = "Select geo_name, geo_id from geo order by geo_name asc";
    					$result = $db->sql_query($sql);
    					if($geo_info = $db->sql_fetchrow($result))
    					{
    						do
    						{
    							echo"<option value=\"".$geo_info['geo_id']."\">".$geo_info['geo_name']."</option>";
    					
    							
    						}
    						while($geo_info  = $db->sql_fetchrow($result));
    					
    						$db->sql_freeresult();
    					}
    					?>
    					</select></td>
    </tr>
    <tr>
    <td>Country Pic :</td><td><INPUT TYPE="TEXT" NAME="country_pic" VALUE="<?php echo $myrow["country_pic"] ?>"></td>
    </tr>
    <tr>
    <td><input type="submit" class="event_button"></td></tr>
    </form>
    </table>
    Last edited by paulredeye; 12-11-2006 at 06:27 AM.

  2. #2
    Join Date
    Feb 2005
    Posts
    100
    Not to worry...this is an easy one.

    So, when you get the info from the db, you're setting the var $geo_id (if it's an edit session, obviously).

    Here's the way I like to do select menus:
    PHP Code:
    <select name="geo_id">
       <?php
            
    //Define your possible regions
            
    $regions = array("region1""region2""region3");

              foreach (
    $regions AS region) {
                       print 
    '<option value="' $region '" ' .
                       ((
    $geo_id == $region) ? 'selected' '') .
                       
    ">" $region '</option>' "\n"
                 }
        
    ?>
    </select>

  3. #3
    Join Date
    Nov 2006
    Posts
    197
    Does $myrow['geo_id] match one of these $geo_info['geo_id']?

    If so, do something like this:

    Code:
    if($geo_info = $db->sql_fetchrow($result)){
    	if ($geo_info['geo_id'] == $myrow['geo_id]) {
    		echo '<option SELECTED value="' .$geo_info['geo_id'].'">'.$geo_info['geo_name'].'</option>';
    	}
    	else {
    		echo"<option value=\"".$geo_info['geo_id']."\">".$geo_info['geo_name']."</option>";
    	}
    
    // etc...
    I'm just adding "SELECTED" to the select option you want to come up first. Then when it's displayed, that option will be shown and all the others will be in the select list in asc order.

  4. #4
    Join Date
    Dec 2006
    Posts
    16
    Well, coppocks thanks for the help but its not working, in fact its not showing any other option other than the first (and wrong one) for each item now.

    My issue isnt really with logic, but more with understanding the syntax errors i keep getting ^^

    Perhaps you can check this and make sure I used your code snippet in the right way please?

    Code:
    <?php
    					$sql = "Select geo_name, geo_id from geo order by geo_name asc";
    					$result = $db->sql_query($sql);
    					if($geo_info = $db->sql_fetchrow($result)){
    	if ($geo_info['geo_id'] == $myrow['geo_id']) {
    		echo '<option SELECTED value="' .$geo_info['geo_id'].'">'.$geo_info['geo_name'].'</option>';
    	}
    	else {
    		echo"<option value=\"".$geo_info['geo_id']."\">".$geo_info['geo_name']."</option>";
    	}
    												
    						while($geo_info  = $db->sql_fetchrow($result));
    					
    						$db->sql_freeresult();
    					}
    					?>
    P.S. Thanks tyree but im using the geo_id variables from the sql rather than in a form base.
    Last edited by paulredeye; 12-10-2006 at 08:31 AM.

  5. #5
    Join Date
    Feb 2005
    Posts
    100
    It's the same thing. Just populate the array with your sql query and roll.
    It compares each geo_id to the geo_id you specified and marks the one that matches it 'selected'. Trust me, I use it A LOT.

    Either way though.

  6. #6
    Join Date
    Dec 2006
    Posts
    16
    Sorry Tyree, but I just dont get what you mean. Could you explain how the query would look with the array filled out.

    As I explaine above, I have tried both ways but just get multiple syntax errors and to be honest, thats my main problem with learning php right now is the stupid syntax errors.

  7. #7
    Join Date
    Nov 2006
    Posts
    197
    None of your code indicates to me where/how the $myrow array originates. So from that array, take whatever matches $geo_info['geo_id'] , whether it's $myrow['geo_id'] or $myrow['country_id] or whatever. There was nothing wrong syntactically with what I posted... that I could see.

    You need to determine what from the $myrow array matches the array item $geo_info['geo_id']. Once you determine that, then ONE of your opening if statements would look like for example:

    if ($geo_info['geo_id'] == $myrow['country_id']) {
    ~OR~
    if ($geo_info['geo_id'] == $myrow['geo_id']) {

    Once you determine what matches then either my code or Tyree's posted code should work just fine.

    You could also be getting an error from this line:

    <input type=hidden name="country_id" value="<?php echo $myrow["country_id"] ?>"></tr>

    $myrow["country_id"] should be $myrow['country_id']

  8. #8
    Join Date
    Dec 2006
    Posts
    16
    Ok guys, let me paste the whole area of code for you to see, so that you can make sure im not doing something stupid.

    PHP Code:
    case "edit_country":
        {
            ?>
            <?php
    }

    if(
    $_GET["mode"]=="edit_country" || $_POST["mode"]=="edit_country"//if the link for the edit cmd was selected, do this
    {
    $id $_GET["country_id"]; //get the id of the country being edited. This is provided in the link to edit it.
    $sql "SELECT * FROM country WHERE country_id=$id"//select all columns from country with this id
    $result mysql_query($sql);
    $myrow $db->sql_fetchrow($result);
    ?>

    <table width="600" align="center" cellspacing="0" cellpadding="0" border="0">
                <tr>
    <form action="<?php echo "http://".$host."/ranks/admin/?action=edit_country";?>" method="post">
     <input type=hidden name="country_id" value="<?php echo $myrow['country_id'?>"></tr>
    <tr>
    <td> Country :</td><td><INPUT TYPE="TEXT" NAME="country_name" VALUE="<?php echo $myrow["country_name"?>"></td>
    </tr>
    <tr>
    <td>Flag File :</td><td><INPUT TYPE="TEXT" NAME="country_flag" VALUE="<?php echo $myrow["country_flag"?>"></td>
    </tr>
    <tr>
    <td>GeoGraphy :</td><td><select name="geo_id" class="event" >
                        <?php
                        $sql 
    "Select geo_name, geo_id from geo order by geo_name asc";
                        
    $result $db->sql_query($sql);
                        if(
    $geo_info $db->sql_fetchrow($result)){
        if (
    $geo_info['geo_id'] == $myrow['geo_id']) {
            echo 
    '<option SELECTED value="' .$geo_info['geo_id'].'">'.$geo_info['geo_name'].'</option>';
        }
        else {
            echo
    "<option value=\"".$geo_info['geo_id']."\">".$geo_info['geo_name']."</option>";
        }
                                                    
                            while(
    $geo_info  $db->sql_fetchrow($result));
                        
                            
    $db->sql_freeresult();
                        }
                        
    ?>
                        </select></td>
    </tr>
    <tr>
    <td>Country Pic :</td><td><INPUT TYPE="TEXT" NAME="country_pic" VALUE="<?php echo $myrow["country_pic"?>"></td>
    </tr>
    <tr>
    <td><input type="submit" class="event_button"></td></tr>
    </form>
    </table>

            <?php
        
    }
        break;

  9. #9
    Join Date
    Nov 2006
    Posts
    197
    Have you tried?:
    Code:
    if ($geo_info['geo_id'] == $id) {
            echo '<option SELECTED value="' .$geo_info['geo_id'].'">'.$geo_info['geo_name'].'</option>';
        }
        else {
            echo"<option value=\"".$geo_info['geo_id']."\">".$geo_info['geo_name']."</option>";
        }

  10. #10
    Join Date
    Dec 2006
    Posts
    16
    I did just to try it, but it doesnt return any differently.

    $id relates to the Country_id posted on the page anyway, not the geo_id of the country being edited.

    If you look at the other lines $myrow is working as I get all the data from the sql in country from it, so the way it should work is by using $myrow['geo_id'] and indeed, to test this theory, I simply used an edit box instead of a drop down list and sure enough it returned the correct geo_id from $myrow.

    However, the code I just posted is still only returning the very top most option in the drop down box and ONLY that option, none of the others, which would suggest some problem... :/

  11. #11
    Join Date
    Dec 2006
    Posts
    16
    Anyone else got any ideas?

  12. #12
    Join Date
    Oct 2006
    Location
    Indonesia
    Posts
    70
    PHP Code:
    while($geo_info  $db->sql_fetchrow($result)); 
    can you please write this method's code (sql_fetchrow()), because this methods seems doesn't do any looping

  13. #13
    Join Date
    Dec 2006
    Posts
    16
    Theramones : I checked that one and it works fine. The reason I know this, as explained earlier, is that I removed the list options and simply used the TEXT function and it posted the correct geo_id for each country i looked at the edit page for.

  14. #14
    Join Date
    Dec 2006
    Posts
    16
    I should also explain perhaps that im trying to set the country geo_id and not the geo geo_id here.

    However, in this part of the code, im trying to get the geo_id from the country ($myrow['geo_id']) which is in the table "country" as geo_id and have it be the default when i select the edit page from a drop down list of the rest of the options which come from $geo_info['geo_id'] which is in the table "geo" under geo_id

  15. #15
    Join Date
    Dec 2006
    Posts
    16
    SUCCESS!

    Finally after two days I have figured it out!

    Thanks to all of those who tried to help here, I really appreciate it.

    I eventually used a mixture of tyree's code and coppocks and my own, through testing.

    Just for completeness, here is the solution.

    Thanks guys.
    PHP Code:
    <select name="geo_id">
      <?php
            
    //Define your possible regions
            
    $sql "Select geo_name, geo_id from geo order by geo_name asc";
                        
    $result $db->sql_query($sql);
                        if (
    $geo_info $db->sql_fetchrow($result)) {
    do {
              echo 
    '<option value="' $geo_info['geo_id'] . '" ' .
                       ((
    $myrow['geo_id'] == $geo_info['geo_id']) ? 'selected' '') .
                       
    ">" $geo_info['geo_name'] . '</option>' "\n";
                 } 
                 while(
    $geo_info $db->sql_fetchrow($result));
                        
                            
                 }
        
    ?>
    </select>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles