While array result starting from 1

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
danf_1979
Forum Commoner
Posts: 72
Joined: Sun Feb 20, 2005 9:46 pm

While array result starting from 1

Post by danf_1979 »

Hi,
I have this function:

Code: Select all

function obtener_vendedor () {
$query = "SELECT nombre FROM ev_vendedores ORDER by idvendedor ASC";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
		while ($row = mysql_fetch_row($result))
					{
					$get_vendedorї] = $row;
					}
	return $get_vendedor;
}
How can I make the array $get_vendedor start from one? like in $get_vendedor[1] for the first result, and not $get_vendedor[0]
Maybe this is basic, but I couldnt find any help on google.
Thanks.
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Arrays are 0 indexed. Why do you need it to start with 1? It doesn't change the behavior of the array...
Last edited by smpdawg on Wed Mar 02, 2005 10:21 am, edited 1 time in total.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

Code: Select all

function obtener_vendedor () { 
$start = 1;
$query = "SELECT nombre FROM ev_vendedores ORDER by idvendedor ASC"; 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 
      while ($row = mysql_fetch_row($result)) 
               { 
               $get_vendedorї$start++] = $row; 
               } 
   return $get_vendedor; 
}
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

Code: Select all

function obtener_vendedor () {
$query = "SELECT nombre FROM ev_vendedores ORDER by idvendedor ASC";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
$get_vendedorї0] = 0;
      while ($row = mysql_fetch_row($result))
               {
                  array_push($get_vendedor, $row);
               }
   return $get_vendedor;
}

that will set the 1st element to 0 and then the results will be what ever you select
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

:lol: collision of the posts! aaah! :P
danf_1979
Forum Commoner
Posts: 72
Joined: Sun Feb 20, 2005 9:46 pm

Post by danf_1979 »

Thank you, both...
Post Reply