/    Sign up×
Community /Pin to ProfileBookmark

List Options default problem

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>”;[/code]

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 :p

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>[/code]

to post a comment
PHP

15 Comments(s)

Copy linkTweet thisAlerts:
@TyreeDec 10.2006 — 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:
[code=php]
<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>
[/code]
Copy linkTweet thisAlerts:
@coppocksDec 10.2006 — Does $myrow['geo_id] match one of these $geo_info['geo_id']?

If so, do something like this:

<i>
</i>if($geo_info = $db-&gt;sql_fetchrow($result)){
if ($geo_info['geo_id'] == $myrow['geo_id]) {
echo '&lt;option SELECTED value="' .$geo_info['geo_id'].'"&gt;'.$geo_info['geo_name'].'&lt;/option&gt;';
}
else {
echo"&lt;option value="".$geo_info['geo_id'].""&gt;".$geo_info['geo_name']."&lt;/option&gt;";
}

// 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.
Copy linkTweet thisAlerts:
@paulredeyeauthorDec 10.2006 — 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?

&lt;?php
$sql = "Select geo_name, geo_id from geo order by geo_name asc";
$result = $db-&gt;sql_query($sql);
if($geo_info = $db-&gt;sql_fetchrow($result)){
if ($geo_info['geo_id'] == $myrow['geo_id']) {
echo '&lt;option SELECTED value="' .$geo_info['geo_id'].'"&gt;'.$geo_info['geo_name'].'&lt;/option&gt;';
}
else {
echo"&lt;option value="".$geo_info['geo_id'].""&gt;".$geo_info['geo_name']."&lt;/option&gt;";
}

<i> </i> while($geo_info = $db-&gt;sql_fetchrow($result));

<i> </i> $db-&gt;sql_freeresult();
<i> </i> }
<i> </i> ?&gt;


P.S. Thanks tyree but im using the geo_id variables from the sql rather than in a form base.
Copy linkTweet thisAlerts:
@TyreeDec 10.2006 — 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. ?
Copy linkTweet thisAlerts:
@paulredeyeauthorDec 10.2006 — 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.
Copy linkTweet thisAlerts:
@coppocksDec 10.2006 — 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']
Copy linkTweet thisAlerts:
@paulredeyeauthorDec 10.2006 — 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.

[code=php]
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;[/code]
Copy linkTweet thisAlerts:
@coppocksDec 10.2006 — Have you tried?:
<i>
</i>if ($geo_info['geo_id'] == $id) {
echo '&lt;option SELECTED value="' .$geo_info['geo_id'].'"&gt;'.$geo_info['geo_name'].'&lt;/option&gt;';
}
else {
echo"&lt;option value="".$geo_info['geo_id'].""&gt;".$geo_info['geo_name']."&lt;/option&gt;";
}
Copy linkTweet thisAlerts:
@paulredeyeauthorDec 10.2006 — 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... :/
Copy linkTweet thisAlerts:
@paulredeyeauthorDec 11.2006 — Anyone else got any ideas?
Copy linkTweet thisAlerts:
@theRamonesDec 11.2006 — [code=php]while($geo_info = $db->sql_fetchrow($result)); [/code]
can you please write this method's code (sql_fetchrow()), because this methods seems doesn't do any looping
Copy linkTweet thisAlerts:
@paulredeyeauthorDec 11.2006 — 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.
Copy linkTweet thisAlerts:
@paulredeyeauthorDec 11.2006 — 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
Copy linkTweet thisAlerts:
@paulredeyeauthorDec 11.2006 — 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.
[code=php]
<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>[/code]
Copy linkTweet thisAlerts:
@TyreeDec 11.2006 — Good deal...I was just gearing up to write it all out in a working, online example...Glad you got it and I'm off the hook! ?
×

Success!

Help @paulredeye spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...