Jump to content

undefined offset


garry27

Recommended Posts

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]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

"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.
Link to comment
Share on other sites

Try:
[code]

<?php

function 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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.