TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Problem with Arrays (http://www.talkphp.com/absolute-beginners/2339-problem-arrays.html)

CoryMathews 02-26-2008 02:24 PM

Problem with Arrays
 
Im having a small problem with arrays, this code used to work, but now i get something like

Code:

Notice: Undefined offset: 1 on line 69

Notice: Undefined offset: 2 on line 77

Notice: Undefined offset: 3 on line 85

you can view the errors here

Heres the code, in short it gets the categories from the db, and splits them into 4 columns. Im pretty sure the error is because im trying to pull from an empty array. The errors only occur when the amount of categories are not directly divisable by 4.

Wildhoney 02-26-2008 04:17 PM

That's a crazy bit of PHP code. What the hell is going on? :-) And why are you doing it like that? And what does it achieve?

CoryMathews 02-26-2008 07:42 PM

lol it takes a list of categories returned from a db query. Then i am arranging it in 4 columns. But i want them in alpha order going down instead of across. So i had to come up with this crazy work around. I couldnt figure out another way to do it.

But the problem is when it trys to push on an empty value on these lines

Code:

for ($x=0;$x<$tot_rows;$x++) { array_push($byFours1, $subs[$y]); $y++; }
there is nothing left in $subs[] to add. I have tried isset, i have tried seeing if $y is higher then the number of rows returned but still no luck.

I hope thats a little more clear.

TlcAndres 02-26-2008 09:13 PM

Do you have your server set to E_STRICT? and ++$var us faster then then $var++

ReSpawN 02-26-2008 09:53 PM

Like TlcAndress just said, maybe you should try using error_reporting( E_ALL ^ E_NOTICE ); for your debugging.

So, what you want to do is retrieve a 1D (one dimensional array as we call it at work) and filter every value depending on the row name. For example;
PHP Code:

$queryFinal $queryRow[['fieldcat']['fieldname']] = $queryRow['queryvalue']; 

If I am right, this is what you are trying to accomplish?

Else, from another perspective, you want to sort every value? Or push it into 4 different arrays?
You should take a look at PHP: Arrays - Manual. That might solve some of your issues. I advise you to read them carefully since I use a lot of them for different tasks.

I hope this helps.

xenon 02-26-2008 11:47 PM

Use a query to fetch the data already ordered, put all that data into one big array, then use array_slice to "cut" the big array into 4 pieces...this is a less trickier way to achieve what I believe you want to achieve. There are other ways, but I'm too tired to come up with a more efficient solution right now...

CoryMathews 02-27-2008 02:04 PM

Well i finally figured it out. I ended up just adding '' to the array if there were not any more values to add. Thanks for the suggestions though.


All times are GMT. The time now is 06:51 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0