FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Change State





DanielXP
I have my states saved like this

Code:
$state[] = "New York";


Allong with my other states

I want to echo them out in my form option

all apart from the one they are currently in saved as
Code:
$logged[state]
Thats a number in the database

I would like them to go like

Code:
<option value='STATE ID'>STATE NAME</option>


Cheers if you could help me make this and i hope i have gave you enough info


post edited by wumingsden
hexkid
DanielXP wrote:
i hope i have gave you enough info


Is the 'STATE ID' in the value of the option tag the index into the $state array? I'll assume yes
Is the state they are on ($logged['state']) the index into the $state array or the state name? I'll assume it's the index

So you have an array with indexes ranging from 0 to (NUMBER_OF_STATES - 1); and you have an index to ignore. I might do this:
Code:
foreach ($state as $index=>$name) {
  if ($index != $logged['state']) {
    echo '<option value="', $index, '">', $name, '</option>';
  }
}
DanielXP
No the state ID is

$state[ID]

like to get my states i have to do this

$state[$logged[state] - 1]
hexkid
DanielXP wrote:
like to get my states i have to do this

$state[$logged[state] - 1]


So you have an array with indexes ranging from 0 to (NUMBER_OF_STATES - 1); and you have an index to ignore. I might do this:
Code:
foreach ($state as $index=>$name) {
  if ($index != $logged['state'] - 1) {
  # inserted                    ^^^^
    echo '<option value="', $index, '">', $name, '</option>';
  }
}
DanielXP
This is the source i get from that code

Code:
<option value="1">Illinois</option><option value="2">New Jersey</option><option value="3">California</option><option value="4">Colorado</option><option value="5">Florida</option><option value="6">Michigan</option><option value="7">New York</option><option value="8">Illinois</option><option value="9">New Jersey</option><option value="10">California</option><option value="11">Colorado</option><option value="12">Florida</option><option value="13">Michigan</option><option value="14">New York</option><option value="15">Illinois</option><option value="16">New Jersey</option><option value="17">California</option><option value="18">Colorado</option><option value="19">Florida</option><option value="20">Michigan</option>


Theres only 7 states (6 should show)
hexkid
DanielXP wrote:
Theres only 7 states (6 should show)

And what state were you in when you run the code?

21? New York?
How could it be 21 if there are only 7 states?
DanielXP
Code:
$state[] = "New York";
$state[] = "Illinois";
$state[] = "New Jersey";
$state[] = "California";
$state[] = "Colorado";
$state[] = "Florida";
$state[] = "Michigan";


Thats all there is
hexkid
On my machine, this script
Code:
<?php
$state[] = "New York";
$state[] = "Illinois";
$state[] = "New Jersey";
$state[] = "California";
$state[] = "Colorado";
$state[] = "Florida";
$state[] = "Michigan";

$logged['state'] = 3;

foreach ($state as $index=>$name) {
  if ($index != $logged['state'] - 1) {
    echo '<option value="', $index, '">', $name, '</option>';
  }
}
?>
outputs
Quote:
<option value="0">New York</option><option value="1">Illinois</option><option value="3">California</option><option value="4">Colorado</option><option value="5">Florida</option><option value="6">Michigan</option>
DanielXP
Thats weird becoz i keep getting

Code:
<option value="1">Illinois</option><option value="2">New Jersey</option><option value="3">California</option><option value="4">Colorado</option><option value="5">Florida</option><option value="6">Michigan</option><option value="7">New York</option><option value="8">Illinois</option><option value="9">New Jersey</option><option value="10">California</option><option value="11">Colorado</option><option value="12">Florida</option><option value="13">Michigan</option><option value="14">New York</option><option value="15">Illinois</option><option value="16">New Jersey</option><option value="17">California</option><option value="18">Colorado</option><option value="19">Florida</option><option value="20">Michigan</option>
hexkid
DanielXP wrote:
Code:
$state[] = "New York";
$state[] = "Illinois";
$state[] = "New Jersey";
$state[] = "California";
$state[] = "Colorado";
$state[] = "Florida";
$state[] = "Michigan";


Thats all there is


Debug the $state array right after your code:
Code:
echo '<pre>';
print_r($state);
echo '</pre>';
If you find there are more than 7 elements in the array, that means the array wasn't empty to start with; you have to find out where the array gets assigned to and squash the bug. If there are only 7 elements, that means the array gets changed between this place of your code and the output of the options: you have to find where that is happening and squash the bug.
DanielXP
Code:
Array
(
    [0] => New York
    [1] => Illinois
    [2] => New Jersey
    [3] => California
    [4] => Colorado
    [5] => Florida
    [6] => Michigan
    [7] => New York
    [8] => Illinois
    [9] => New Jersey
    [10] => California
    [11] => Colorado
    [12] => Florida
    [13] => Michigan
    [14] => New York
    [15] => Illinois
    [16] => New Jersey
    [17] => California
    [18] => Colorado
    [19] => Florida
    [20] => Michigan
}


And i have no clue what ur on about a bug, sorry
hexkid
DanielXP wrote:
Code:
Array
(
    [0] => New York
...
    [20] => Michigan
}


And i have no clue what ur on about a bug, sorry

You wrote your code such that the array would have 7 elements. PHP shows 21 elements. I call that a bug. [EDIT] make that a big bug Smile
DanielXP
right

so how can i fix it?
hexkid
DanielXP wrote:
how can i fix it?

First thing to do is to find the bug. You can't fix it otherwise.

Search your script for assignments to $state (other than the ones you really want). When you find them, you know that's where the bug is. You can now fix it. The fixing method depends on what the script does.

If you can't find the assignments, you can try to sprinkle your code with debugging aids
Code:
echo 'DEBUGGING line #', __LINE__, ': $state has ', @count($state), ' elements</pre>';

Insert copies of this line before and after suspicious function calls, before and after if() delimited code blocks, inside loops, ..., ...
DanielXP
I found the problem

them states was in my config file and it was included in a few pages included in the page with the error

i made a new page settings instead works great now,

thanks
hexkid
DanielXP wrote:
I found the problem

them states was in my config file and it was included in a few pages included in the page with the error

i made a new page settings instead works great now


That's why I never (that may be a slight exaggeration) have directly executable code in include files.

Code:
<?php
// this is an include file I'd never write
$state[] = 'New York';
$state[] = 'Illinois';
?>

====================

<?php
include 'states.php';
echo $state[1];
?>


Code:
<?php
// this is my way to write initialization in include files
function create_states() {
  $state[] = 'New York';
  $state[] = 'Illinois';
  return $state;
}
?>

====================

<?php
require 'states.php';
$state = create_states();
echo $state[1];
?>
Related topics
Australian state to ban workplace e-mail spying
Students for Progressive Change
5 Steps To Being More Photogenic
How To: Change/Update directory listing.
Is faith necessary?
Climate change escalates Darfur crisis
Did you know that you can change your name?
Climate Change/Global Warming
Can't change link colors!
Hillary as Sec. of State
Separation of church and state: my ideal solution.
Solid State Economy
Star eating black hole
Climate change data
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.