# Undefined index...



## JiminSA (Dec 15, 2011)

I am trying to sort an array using a value in that array, using the following code...

```
$vehicles = LoadVehicles();
if ( (array) $vehicles == $vehicles ) 
{
	function compare_datetime($a, $b)
	{
		return strnatcmp($a['datetime'], $b['datetime']);
	}
	usort($vehicles, 'compare_datetime');
	var_dump ($vehicles);
}
exit;
```
which I gleaned from this tutorial.
Unfortunately I am getting a notification - viz:-


> Notice: Undefined index: datetime in C:\xampp\htdocs\S4W\public_html\CMS\convert.php on line 25


Does one need to "define" or "declare" an associative array, before one can use it? If so, how?


----------



## dukevyner (Nov 4, 2011)

"This error appears because of your PHP error reporting settings. Usually, it appears when your variable is not properly set."
make sure that all variables are set such as "$a" and "$b"


----------



## JiminSA (Dec 15, 2011)

I have modified the code thus but still pick up the notice

```
$vehicles_in = LoadVehicles();

if ( (array) $vehicles_in == $vehicles_in ) 
{
	function compare_datetime($a, $b)
	{
		return strnatcmp($a['datetime'], $b['datetime']);
	}
}
	$a = $vehicles_in;
	$b = $vehicles_in;
	usort($vehicles_in, 'compare_datetime');
	var_dump ($vehicles_in);
exit;
```


----------



## JiminSA (Dec 15, 2011)

I don't see that it's necessary, but I initialise the array thus...

```
$vehicles_in = array(
    $vehicles_in["fame"] = "",
    $vehicles_in["phone"] = "",
    $vehicles_in["cell"] = "",
    $vehicles_in["suburb"] = "",
    $vehicles_in["make"] = "",
    $vehicles_in["model"] = "",
    $vehicles_in["year"] = "",
    $vehicles_in["kms"] = "",
    $vehicles_in["colour"] = "",
    $vehicles_in["extras"] = "",
    $vehicles_in["servhist"] = "",
    $vehicles_in["accidents"] = "",
    $vehicles_in["settlement"] = "",
    $vehicles_in["finhouse"] = "",
    $vehicles_in["monthsrem"] = "",
    $vehicles_in["monthsarr"] = "",
    $vehicles_in["amtrem"] = "",
    $vehicles_in["price"] = "",
    $vehicles_in["comments"] = "",
    $vehicles_in["tprice"] = "",
    $vehicles_in["rprice"] = "",
    $vehicles_in["natis"] = "",
    $vehicles_in["pics4"] = "",
    $vehicles_in["wcdisc"] = "",
    $vehicles_in["aprice"] = "",
    $vehicles_in["status"] = "",
    $vehicles_in["notes"] = "",
    $vehicles_in["datetime"] = ""
);
```


----------



## allnodcoms (Jun 30, 2007)

Hi Ho Jim...

I think the problem here is that your two sort functions are expecting a single dimensional array, and unless I'm very much mistaken, your LoadVehicles(); function will return a multidimensional one (as your strnatcmp(); call is comparing similar keys and, obviously, because you told me the other day!).

This ain't gonna work fella. The array_multisort(); function is the way to go on this as it sieves multidimensional and multiple arrays by a single key.

_Do I really need to mention that your array definition looks like a table in a MySQL database and that you could sort your records with a single call, using standard PHP? No... thought not._ 

Danny


----------



## JiminSA (Dec 15, 2011)

Thanks for the solution Danny.

I shall indeed be looking to come out of hibernation with this project and have already opened one eye inasmuch as I have set up the vehicles db on my server

Can you recommend a good tutorial, without too much PHPspeak?


----------



## allnodcoms (Jun 30, 2007)

Give this one a bash mate. If you've got any questions you know the addy 

Have fun fella... It's a doddle once you get going.

Danny


----------



## JiminSA (Dec 15, 2011)

Aargh!

No Joy - adjusted the code so...

```
$vehicles_in = LoadVehicles();

if ( (array) $vehicles_in == $vehicles_in ) 
{

	foreach ($vehicles_in as $key => $row) {
		$datetime[$key]  = $row['datetime'];
	}

	array_multisort($datetime, SORT_DESC, $vehicles_in);
	$vehicles_out = $vehicles_in;
	var_dump ( $vehicles_out );
}
exit;
```
But still get..


> Notice: Undefined index: datetime in C:\xampp\htdocs\S4W\public_html\CMS\convert.php on line 88


Line 88 = $datetime[$key] = $row['datetime'];

This is vexing!


----------



## allnodcoms (Jun 30, 2007)

Right, I've taken a good long look at this and there are two possible solutions:

Explicitly define $datetime as an array
There is an issue with your LoadVehicles(); function
To test theory 1, add:

```
$datetime = array();
```
before your '_foreach()_'. This will ensure that $datetime is defined as an array, but I don't think that's it...

LoadVehicles() should return a correctly formatted, multidimensional array:

```
$vehicle = array(); 
$vehicle[] = array('key1' => 'value1', 'key2' => 'value2');
$vehicle[] = array('key1' => 'value1', 'key2' => 'value2');
$vehicle[] = array('key1' => 'value1', 'key2' => 'value2'); 
// and so on...
```
Give these a go and see if we are getting any nearer. Keep me posted!

Danny


----------



## JiminSA (Dec 15, 2011)

This a var_dump of a portion (1st record) of the array retrieved by Loadvehicles

```
array(2) { ["vehicles"]=> array(59) { [1]=> array(30) { ["fame"]=> string(6) "Victor" ["email"]=> string(18) "[email protected]" ["phone"]=> string(10) "0727504912" ["cell"]=> string(10) "0727504912" ["suburb"]=> string(11) "Sunninghill" ["make"]=> string(3) "BMW" ["model"]=> string(4) "325i" ["year"]=> string(4) "2008" ["kms"]=> string(6) "87 000" ["colour"]=> string(5) "white" ["extras"]=> string(73) "sunroof, climate control, auto cruise control, PDC, motor plan up to 100k" ["servhist"]=> NULL ["accidents"]=> string(4) "none" ["settlement"]=> string(10) "Yes - Full" ["finhouse"]=> string(6) "toyota" ["monthsrem"]=> string(3) "n/a" ["monthsarr"]=> string(3) "n/a" ["amtrem"]=> string(3) "n/a" ["price"]=> string(7) "210 000" ["comments"]=> string(6) "change" ["photos"]=> array(0) { } ["tprice"]=> string(4) "0.00" ["rprice"]=> string(4) "0.00" ["natis"]=> string(3) "Yes" ["pics4"]=> string(9) "Requested" ["wcdisc"]=> string(3) "Yes" ["aprice"]=> string(0) "" ["status"]=> string(10) "No Contact" ["notes"]=> string(0) "" ["datetime"]=> int(1341054720)
```
["datetime"] is right at the record's end
I've adjusted the code for the array define...

```
$vehicles_in = LoadVehicles();

$datetime = array();
$row = array();
$row["datetime"] = "";
var_dump ( $vehicles_in );

if ( (array) $vehicles_in == $vehicles_in ) 
{

	foreach ($vehicles_in as $key => $row) {
		$datetime[$key]  = $row['datetime'];
	}

	array_multisort($datetime, SORT_ASC, $vehicles_in);

	$vehicles_out = $vehicles_in;
	var_dump ( $vehicles_out );
}
exit;
```
still get the notification! for this line ... $datetime[$key] = $row['datetime'];


----------



## JiminSA (Dec 15, 2011)

$vehicles is multi-dimensional - I forgot to define it correctly - $vehicles_in["vehicles"]

Now we are getting closer to the actual sort and have hit this problem...


> Warning: array_multisort() [function.array-multisort]: Array sizes are inconsistent in C:\xampp\htdocs\S4W\public_html\CMS\convert.php on line 97


 where line 97 is

```
array_multisort($datetime, SORT_ASC, $vehicles_in);
```
the current code is...

```
$vehicles_in = LoadVehicles();

$vehicle = $vehicles_in["vehicles"][1];
echo "Date-Time " . $vehicle["datetime"];

if ( (array) $vehicles_in == $vehicles_in ) 
{

    foreach ($vehicles_in["vehicles"] as $key => $row) { 
        $datetime[$key] = $row['datetime']; 
    }

    array_multisort($datetime, SORT_ASC, $vehicles_in);

    $vehicles_out = $vehicles_in; }
/*    var_dump ( $vehicles_out ); }*/
exit;
```


----------



## allnodcoms (Jun 30, 2007)

Inconsistent array sizes is down to your loading / saving code mate. If there is an empty field you'll need to write out a string, a blank or empty string may not make it through your serialisation and this would lead to your problem. It's the number of fields in each sub-array and not the length of them that is the issue, so I'm guessing it's down to the read / write on your csv file.

That's where I'd start looking anyway...

Danny


----------



## JiminSA (Dec 15, 2011)

Ta Danny - I'll look tomz... ni-night...


----------



## JiminSA (Dec 15, 2011)

Danny, would I be right in assuming that these difficulties would be alleviated if I went MySql and read the array in from there?


----------



## allnodcoms (Jun 30, 2007)

By Jove I think he's got it! 

Yes mate - "_SELECT * FROM 'vehicles' ORDER BY 'datetime'_" - that's it!

Good choice Jamesy, any problems - you know how to find me.

Danny


----------



## JiminSA (Dec 15, 2011)

Ha-ha! I'm also glad!
Perhaps you might help me with a quick question - I'm setting up the db using the vehicles.dat file and hit a small prob...
can I use php data variables in the insert statement? Like this...

```
mysql_select_db("surfwhee_vehicles", $con);

foreach ($vehicles["vehicles"] as $id => $vehicles)
{
	$count+=1;
	echo "Count: " . $count;
	mysql_query("INSERT INTO webentry 
	(datetime, fullname, phone, cell, suburb, make, model, year, kms, colour, extras, servhist, accidents, settlement, finhouse, monthsrem, monthsarr, amtrem, price, comments, tprice, rprice, natis, pics4, wcdisc, aprice, status, notes, agent, datetimestamp)
	VALUES 
	($vehicles['datetime'], $vehicles['fame'], $vehicles['phone'], $vehicles['cell'], $vehicles['suburb'], $vehicles['make'], $vehicles['model'], $vehicles['year'], $vehicles['kms'], $vehicles['colour'], $vehicles['extras'], $vehicles['servhist'], $vehicles['accidents'], $vehicles['settlement'], $vehicles['finhouse'], $vehicles['monthsrem'], $vehicles['monthsarr'], $vehicles['amtrem'], $vehicles['price'], $vehicles['comments'], $vehicles['tprice'], $vehicles['rprice'], $vehicles['natis'], $vehicles['pics4'], $vehicles['wcdisc'], $vehicles['aprice'], $vehicles['status'], $vehicles['notes'], 'Jim', date())");
}

mysql_close($con);
```
And yet another quessy...
I created the db using the server ControlPanel software (phpMyAdmin) and half expected to pick up the localhost MySql directory in FileZilla, but haven't. Is there any way that I can? (I would like to download it to xampp)


----------



## allnodcoms (Jun 30, 2007)

Sorry Jim, was watching the cycling... Yes, you can use PHP variables, but you drop the single quotes -_$vehicles['datetime']_ would become _$vehicles[datetime]_... No idea why, it's just the way it is.

You made the DB with phpMyAdmin on your remote server and want to download it to localhost? Your control panel has an export option which lets you save not only the DB data, but also the SQL code necessary to create the whole thing again somewhere else. Just export your DB and import it locally, it'll all be there waiting for you. Did I mention this was easy? 

Danny


----------



## JiminSA (Dec 15, 2011)

You did mate! Easy-peasy as Oliver might say - thanks ol' mate 'specially for the quotes thing - I'd a been going in circles otherwise...:up:


----------

