/    Sign up×
Community /Pin to ProfileBookmark

Array problems. Want index value not text value.

Hello,

I’m having some problems with an array I’ve made:

[code=php]
<?

$coptions = array();
$coptions[] = “Any”;
$coptions[] = “Flat/apartment”;
$coptions[] = “House”;
$coptions[] = “Bungalow”;
$coptions[] = “Villa”;
$coptions[] = “Semi-Detached”;
$coptions[] = “Detached”;
$coptions[] = “Other”;

echo ‘<select name=”property_type” id=”property_type” style=”width:115px”>’;
foreach ($coptions as $key => $val) {
echo “<option” . ($val == $_SESSION[‘property_type’] ? ” selected” : “”) . “>$val</option>”;
}
echo “</select>”;

?>
[/code]

Basically when submitted I wish to store the index value of the array in the session(rather than the name value which it is just now).

So if other’ was selected and submitted, then the HTML echo’d out would be:

[code=html]<option value=”1″ selected>Flat/apartment</option>[/code]

I’m sure it’s something simple needing changed, thanks for your help.

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@scragarJan 06.2008 — you havn't shown any code relating to where you set it, but I'm sure it wouldn't be too hard to use:

[url=php.net/array_search]array_search()[/url]
Copy linkTweet thisAlerts:
@invisionauthorJan 06.2008 — Hey,

Thanks for the reply.

[code=php]
session_start();

$_SESSION['property_type'] = (isset($_GET['property_type']) ? escape_data($_GET['property_type']) : $_SESSION['property_type']);
[/code]
Copy linkTweet thisAlerts:
@scragarJan 06.2008 — how about something like:
[code=php]session_start();
$coptions = array();
$coptions[] = "Any";
$coptions[] = "Flat/apartment";
$coptions[] = "House";
$coptions[] = "Bungalow";
$coptions[] = "Villa";
$coptions[] = "Semi-Detached";
$coptions[] = "Detached";
$coptions[] = "Other";
if(in_array($_GET['property_type']))
$_SESSION['property_type'] = array_search($_GET['property_type']);[/code]
Copy linkTweet thisAlerts:
@invisionauthorJan 06.2008 — Thanks for the help so far, really appreciate it.

It's just that I'm after the index value. It's currently taking the name 'Flat/apartment' so shoves that into the query string and session, rather than '1'. Should I use array_search or is there a quicker method?
Copy linkTweet thisAlerts:
@scragarJan 06.2008 — why not just try putting:

[code=php]foreach ($coptions as $key => $val) {
echo "<option value='$key'" . ($val == $_SESSION['property_type'] ? " selected" : "") . ">$val</option>";
}[/code]
inplace of your current loop then?
Copy linkTweet thisAlerts:
@invisionauthorJan 06.2008 — Hi.

Again, thanks for your patience.

I tried using the loop you created above, but unfortunately it only outputted:
[code=html]
<select name="property_type" id="property_type" style="width:115px"><option selected>Any</option><option>Flat/apartment</option><option>House</option><option>Bungalow</option><option>Villa</option><option>Semi-Detached</option><option>Detached</option><option>Other</option></select>
[/code]


And that's after submitting 'Flat/apartment'. It adds that text to the query string, instead of the index value assoc. with it.


Thanks again.
Copy linkTweet thisAlerts:
@scragarJan 06.2008 — reload the page and/or clear your cache, then try again.

I know it will work, for some reason your looking at a version of the page before it was edited.
Copy linkTweet thisAlerts:
@invisionauthorJan 06.2008 — OK. Almost there.

It now stores the index value in the query string, and in the <option value='1'> part, but unfortunately doesn't keep the value there ?

And that's me using
[code=php]<?

$doptions = array();
$doptions[] = "Any";
$doptions[] = "Flat/apartment";
$doptions[] = "House";
$doptions[] = "Bungalow";
$doptions[] = "Villa";
$doptions[] = "Semi-Detached";
$doptions[] = "Detached";
$doptions[] = "Other";

echo '<select name="property_type" id="property_type" style="width:115px">';
foreach ($doptions as $key => $val) {
echo "<option value='$key'" . ($val == $_SESSION['property_type'] ? " selected" : "") . ">$val</option>";
}
echo "</select>";

?>
[/code]
Copy linkTweet thisAlerts:
@invisionauthorJan 06.2008 — *bump* any solution to the above would be very much appreciated.

Many thanks!
Copy linkTweet thisAlerts:
@kattenJan 06.2008 — i can see the problem ?

At the top of the script you do not have
[code=php]
<?php
//your missing this part bellow
session_start();
// Rest of your code
?>
[/code]
Copy linkTweet thisAlerts:
@invisionauthorJan 07.2008 — Sorry, this is a condensed part of the script. I have that(see #3) and store the value in the SESSION part too.
[code=php]
session_start();

$_SESSION['property_type'] = (isset($_GET['property_type']) ? escape_data($_GET['property_type']) : $_SESSION['property_type']);
[/code]
Copy linkTweet thisAlerts:
@callumdJan 07.2008 — Use this for your SELECT BOX loop:

[code=php]foreach ($doptions as $key => $val) {
echo '<option value="' . $key . '"';
if ($key == $_SESSION['property_type']) {
echo ' selected="selected"';
}
echo '>' . $val . '</option>' . "n";
}[/code]
×

Success!

Help @invision 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.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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