Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Web Design & Development Web Design & Development
Search Search
Search for:
Tech Support Guy > > >

Undefined index...


(!)

JiminSA's Avatar
JiminSA   (Jim) JiminSA is offline JiminSA has a Profile Picture
Computer Specs
Member with 1,668 posts.
THREAD STARTER
 
Join Date: Dec 2011
Location: Johannesburg South Africa
Experience: Intermediate
10-Jul-2012, 04:58 AM #1
Undefined index...
I am trying to sort an array using a value in that array, using the following code...
PHP 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:-
Quote:
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's Avatar
dukevyner   (Luke) dukevyner is offline
Computer Specs
Member with 787 posts.
 
Join Date: Nov 2011
Location: Pakenham, VIC, Australia
Experience: Advanced
10-Jul-2012, 08:46 AM #2
"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's Avatar
JiminSA   (Jim) JiminSA is offline JiminSA has a Profile Picture
Computer Specs
Member with 1,668 posts.
THREAD STARTER
 
Join Date: Dec 2011
Location: Johannesburg South Africa
Experience: Intermediate
10-Jul-2012, 09:02 AM #3
modified code...
I have modified the code thus but still pick up the notice
PHP Code:
$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's Avatar
JiminSA   (Jim) JiminSA is offline JiminSA has a Profile Picture
Computer Specs
Member with 1,668 posts.
THREAD STARTER
 
Join Date: Dec 2011
Location: Johannesburg South Africa
Experience: Intermediate
10-Jul-2012, 10:18 AM #4
Defining/Intialising the array...
I don't see that it's necessary, but I initialise the array thus...
PHP Code:
$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's Avatar
allnodcoms   (Danny) allnodcoms is offline allnodcoms has a Profile Picture
Computer Specs
Member with 610 posts.
 
Join Date: Jun 2007
Location: Hertfordshire - England
Experience: Advanced
10-Jul-2012, 02:18 PM #5
Question My SQuirreL is hibernating. Just thought I'd mention it.
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

Last edited by allnodcoms; 10-Jul-2012 at 02:19 PM.. Reason: Added "Squirrel" gag!
JiminSA's Avatar
JiminSA   (Jim) JiminSA is offline JiminSA has a Profile Picture
Computer Specs
Member with 1,668 posts.
THREAD STARTER
 
Join Date: Dec 2011
Location: Johannesburg South Africa
Experience: Intermediate
11-Jul-2012, 02:50 AM #6
Salutations!
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's Avatar
allnodcoms   (Danny) allnodcoms is offline allnodcoms has a Profile Picture
Computer Specs
Member with 610 posts.
 
Join Date: Jun 2007
Location: Hertfordshire - England
Experience: Advanced
11-Jul-2012, 03:45 AM #7
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's Avatar
JiminSA   (Jim) JiminSA is offline JiminSA has a Profile Picture
Computer Specs
Member with 1,668 posts.
THREAD STARTER
 
Join Date: Dec 2011
Location: Johannesburg South Africa
Experience: Intermediate
11-Jul-2012, 03:53 AM #8
Aargh!

No Joy - adjusted the code so...
PHP Code:
$vehicles_in LoadVehicles();

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

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

    
array_multisort($datetimeSORT_DESC$vehicles_in);
    
$vehicles_out $vehicles_in;
    
var_dump $vehicles_out );
}
exit; 
But still get..
Quote:
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's Avatar
allnodcoms   (Danny) allnodcoms is offline allnodcoms has a Profile Picture
Computer Specs
Member with 610 posts.
 
Join Date: Jun 2007
Location: Hertfordshire - England
Experience: Advanced
11-Jul-2012, 06:55 AM #9
Right, I've taken a good long look at this and there are two possible solutions:
  1. Explicitly define $datetime as an array
  2. There is an issue with your LoadVehicles(); function
To test theory 1, add:
PHP Code:
$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:
PHP Code:
$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's Avatar
JiminSA   (Jim) JiminSA is offline JiminSA has a Profile Picture
Computer Specs
Member with 1,668 posts.
THREAD STARTER
 
Join Date: Dec 2011
Location: Johannesburg South Africa
Experience: Intermediate
11-Jul-2012, 07:28 AM #10
This a var_dump of a portion (1st record) of the array retrieved by Loadvehicles
PHP Code:
array(2) { ["vehicles"]=> array(59) { [1]=> array(30) { ["fame"]=> string(6"Victor" ["email"]=> string(18"vomoil@hotmail.com" ["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...
PHP Code:
$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($datetimeSORT_ASC$vehicles_in);

    
$vehicles_out $vehicles_in;
    
var_dump $vehicles_out );
}
exit; 
still get the notification! for this line ... $datetime[$key] = $row['datetime'];
JiminSA's Avatar
JiminSA   (Jim) JiminSA is offline JiminSA has a Profile Picture
Computer Specs
Member with 1,668 posts.
THREAD STARTER
 
Join Date: Dec 2011
Location: Johannesburg South Africa
Experience: Intermediate
11-Jul-2012, 09:12 AM #11
Got over one hurdle (undefined index) now the next...
$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...
Quote:
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
PHP Code:
    array_multisort($datetimeSORT_ASC$vehicles_in); 
the current code is...
PHP Code:
$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($datetimeSORT_ASC$vehicles_in); 

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

Last edited by JiminSA; 11-Jul-2012 at 09:15 AM.. Reason: ommission
allnodcoms's Avatar
allnodcoms   (Danny) allnodcoms is offline allnodcoms has a Profile Picture
Computer Specs
Member with 610 posts.
 
Join Date: Jun 2007
Location: Hertfordshire - England
Experience: Advanced
11-Jul-2012, 12:32 PM #12
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's Avatar
JiminSA   (Jim) JiminSA is offline JiminSA has a Profile Picture
Computer Specs
Member with 1,668 posts.
THREAD STARTER
 
Join Date: Dec 2011
Location: Johannesburg South Africa
Experience: Intermediate
11-Jul-2012, 01:51 PM #13
Ta Danny - I'll look tomz... ni-night...
JiminSA's Avatar
JiminSA   (Jim) JiminSA is offline JiminSA has a Profile Picture
Computer Specs
Member with 1,668 posts.
THREAD STARTER
 
Join Date: Dec 2011
Location: Johannesburg South Africa
Experience: Intermediate
12-Jul-2012, 06:18 AM #14
Going MySql at last!
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's Avatar
allnodcoms   (Danny) allnodcoms is offline allnodcoms has a Profile Picture
Computer Specs
Member with 610 posts.
 
Join Date: Jun 2007
Location: Hertfordshire - England
Experience: Advanced
12-Jul-2012, 10:38 AM #15
Talking Yippeee!
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
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑