GetDotted Domains

Viewing Thread:
"PHP: nested while loops"

The "Freeola Customer Forum" forum, which includes Retro Game Reviews, has been archived and is now read-only. You cannot post here or create a new thread or review on this forum.

Mon 31/01/05 at 13:02
Regular
"NULL"
Posts: 1,384
I am trying to do some nested looping but it's not working with "while" statements.

When the nested while returns false, it fails the parent while loop as well.

Any thoughts?
Fri 04/02/05 at 15:52
Regular
"Devil in disguise"
Posts: 3,151
Messy hacks suggest you're modelling your data incorrectly for one thing. Although whether starting again is an option for you I dont know.

Looking at the function you've done, without testing, it looks as though its doing the right kind of thing. What isn't it doing that it should be? Is your problem to do with detecting the lowest level?
Fri 04/02/05 at 15:37
Regular
"NULL"
Posts: 1,384
Lol, cheers - any hints?
Fri 04/02/05 at 15:35
Regular
"Devil in disguise"
Posts: 3,151
Its possible if thats any help. :)
Fri 04/02/05 at 11:16
Regular
"Pouch Ape"
Posts: 14,499
Nah, sorry, me and arrays don't mix well.
Fri 04/02/05 at 11:14
Regular
"NULL"
Posts: 1,384
Anyone? Please? I know there are some very good c0ders around here. If you don't think there is a way to do it, please let me know and I'll stop trying! Lol.
Wed 02/02/05 at 09:02
Regular
"NULL"
Posts: 1,384
Right, this thing is killing me! I've spent god knows how many hours on it trying to work out a solution. I think it just needs a fresh look.

I need a function to do the following: display the array structure to a given depth. Here is an example of the array:

Array
(
[1] => Array
(
[2] => Array
(
[4] => 0
)

)

[3] => Array
(
[5] => 0
)

)

So, if the function was called with the parameter 1, it would display:

- 1

- 3

If the parameter was 2, it would display:

- 1
- - 2

- 3
- - 5

And 3 would give:

- 1
- - 2
- - - 4

- 3
- - 5

The problem is making a recursive function that could, in theory, handle an array of depth, say, 100. Any thoughts would be very much appreciated.

The best I've managed is this:

function rec ($data, $index, $i, $max) {
if (is_array($index)) {
if ($i<=$max)
{
foreach ($index as $volume => $issue)
{
echo "" . $data[$volume]['title'] . "
\n";
if ($i==$max) { $i=1; break; }
else { $i++;}
rec($data, $issue, $i, $max);
echo "
";
$i--;
}
echo "";
}
}
}

rec($data, $index, 1, 3);

-----------

The problem is I need to be able to insert content at the lowest level. Also, my script is a pretty messy hack...
Mon 31/01/05 at 15:34
Regular
"NULL"
Posts: 1,384
Damn this is turning out messy. Again, I've made a workaround, but it's not ideal.

The project I'm working on at the moment is deceptive - sounds simple but isn't because some of the high-level functions you would expect to exist, don't, and you have to go back to basics to get round them.

It's basically a recursive database lookup, where articles are within containers which can either be sub-containers or top-level containers - the depth of sub-containers levels is flexible and undefined though.

Makes the whole thing very complex.
Mon 31/01/05 at 15:15
Regular
"NULL"
Posts: 1,384
OK, another tricky one. I have the following array.

Array
(
[1] => Array
(
[0] => 4
[1] => 2
[2] => 1
)

[2] => Array
(
[0] => 5
[1] => 3
)

)

I have the value, for example, 4, and I want it to return the top level key, e.g. 1.

So, 4, 2 or 1 would return 1, and 5 or 3 would return 2.

I can get it to return the key, but not the top level key... any thoughts?
Mon 31/01/05 at 14:54
Regular
"Devil in disguise"
Posts: 3,151
Nimco wrote:
> It works, but if anyone knows of a better way to do it, please let me
> know!

Yours works so you might consider it the best way already. :)
You could have also done this though

$string = "Volume 5 -||- Issue 1 -||- Article 1";
$path = explode(" -||- ",$string);
$array=array();
$cp=&$array;
foreach($path as $node) {
array_push($cp,$node);
$cp[$node]=array();
$cp=&$cp[$node];
}

Or something close to that, probably needs a little tuning.

Hardly elegant either, probably the best way would have been to create a unique key from your original string but all depends on how you're using the data I suppose.
Mon 31/01/05 at 14:30
Regular
"NULL"
Posts: 1,384
Nah, that would create a 1-dimensional array not a multidimensional array.

I solved the first problem just by not using recursive while statements - I found a much easier way to do it with foreach anyway.

Freeola & GetDotted are rated 5 Stars

Check out some of our customer reviews below:

Just a quick note to say thanks for a very good service ... in fact excellent service..
I am very happy with your customer service and speed and quality of my broadband connection .. keep up the good work . and a good new year to all of you at freeola.
Matthew Bradley
Impressive control panel
I have to say that I'm impressed with the features available having logged on... Loads of info - excellent.
Phil

View More Reviews

Need some help? Give us a call on 01376 55 60 60

Go to Support Centre

It appears you are using an old browser, as such, some parts of the Freeola and Getdotted site will not work as intended. Using the latest version of your browser, or another browser such as Google Chrome, Mozilla Firefox, or Opera will provide a better, safer browsing experience for you.