garry27 Posted November 2, 2006 Share Posted November 2, 2006 i'm tring to create a new array called $location which will consist of the values of two other arrays $longitude and $latitude (both of which have an equal number of values) joined together in asingle string (plus the comma). am i doing this right because i keep getting errors saying "undefined offset...in line 31"[code]$result = count($longitude);for ( $i = 0; $i <$result; $i++ ) { $location[$i] = array ( $longitude[$i].','.$latitude[$i] ); //line 31 }[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25891-undefined-offset/ Share on other sites More sharing options...
Daniel0 Posted November 2, 2006 Share Posted November 2, 2006 Try this instead: [code]<?php$result = count($longitude);$location = array();for($i=0; $i<$result; $i++){ $location[] = "{$longitude[$i]},{$litatude[$i]}";}?>[/code]Note that somethings was just changed to fit my coding style, what I think caused the error was that $location was not an array. Quote Link to comment https://forums.phpfreaks.com/topic/25891-undefined-offset/#findComment-118270 Share on other sites More sharing options...
garry27 Posted November 2, 2006 Author Share Posted November 2, 2006 i still get the same error. here's the code from the start of the function:[code]function a(){ $sql = mysql_query ( "select Longitude from Pubs" ); $longitude = mysql_fetch_array($sql); if (!$sql){ die('Invalid query: ' . mysql_error()); } $sql = mysql_query ( "select Latitude from Pubs" ); $latitude = mysql_fetch_array($sql); if (!$sql){ die('Invalid query: ' . mysql_error()); } $result = count($longitude);$location = array();for($i=0; $i<$result; $i++){ $location[] = "{$longitude[$i]},{$latitude[$i]}";}[/code]did you mean to leave the index value blank for location inside the loop? i get the same error messages both ways anyway :-\cheers for the help Quote Link to comment https://forums.phpfreaks.com/topic/25891-undefined-offset/#findComment-118280 Share on other sites More sharing options...
btherl Posted November 2, 2006 Share Posted November 2, 2006 "undefined offset" means you are trying to access an array element that doesn't exist.It's likely that your latitude and longitude don't have the same number of elements. To check, use count() on both of them and see if the numbers match up.Are you intending to fetch several rows of data from your sql queries, or just a single row with many columns? mysql_fetch_array() will only fetch a single row. That may also be causing problems. Quote Link to comment https://forums.phpfreaks.com/topic/25891-undefined-offset/#findComment-118281 Share on other sites More sharing options...
garry27 Posted November 2, 2006 Author Share Posted November 2, 2006 yes, the numbers match up but i'm also trying to fetch several rows. is there a built in function or some other way i can do this? :) Quote Link to comment https://forums.phpfreaks.com/topic/25891-undefined-offset/#findComment-118438 Share on other sites More sharing options...
alpine Posted November 2, 2006 Share Posted November 2, 2006 Try:[code]<?phpfunction a(){$sql = mysql_query ("select Longitude,Latitude from Pubs") or die('Invalid query: ' . mysql_error());$location = array();while($elements = mysql_fetch_array($sql)){$location[] = $elements[0].",".$elements[1];}return $location;}echo "<pre>";print_r(a());echo "</pre>";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25891-undefined-offset/#findComment-118454 Share on other sites More sharing options...
garry27 Posted November 2, 2006 Author Share Posted November 2, 2006 that's great!thanks for all the advice, fellas. Quote Link to comment https://forums.phpfreaks.com/topic/25891-undefined-offset/#findComment-118513 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.