Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 29th, 2003, 11:23 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default for loops from within foreach

The following is a complete test script adapted from a class that I'm developing. The object of the script is to update x and y coordinates for absolutely positioning an image, in this case onto a calender. I've been through this code again and again and again and cannot figure out why the 'for' statements do not fire. Nothing between the for statements executes or outputs. The $y coordinate updates as it should but the value of $x does not change. There has to be something wrong with my for loop logic. But I can't see what it is.
Maybe a new set of eyes will help!

Thanks in advance!
: )
Rich

Code:
<?php

        function build_icons()
        {

            $icon[29] = 'star.png';
            $icon[34] = 'star.png';            

            $offset_left = (int) 65;
            $offset_top  = (int) 50;

            if (isset($icon) && is_array($icon))
            {    

                foreach($icon as $i => $file)
                {
                    # Return x and y to default values
                    $x = (int) 35;
                    $y = (int) 125;

                    # Range 0-6 is the first row of the calender
                    if ($i >= 0 && $i <= 6)
                    {

                        for($d = 0, $f = 0; $f == $i; $d++, $f++)
                        {
                            if ($d > 0) $x = ($x + ($offset_left * $d));
                        }

                    }

                    else if ($i >= 7 && $i <= 13)
                    {

                        $y = ($y + $offset_top);
                        for($d = 0, $f = 7; $f == $i; $d++, $f++)
                        {
                            if ($d > 0) $x = ($x + ($offset_left * $d));
                        }

                    }

                    else if ($i >= 14 && $i <= 20)
                    {

                        $y = ($y + ($offset_top * 2));
                        for($d = 0, $f = 14; $f == $i; $d++, $f++)
                        {
                            if ($d > 0) $x = ($x + ($offset_left * $d));
                        }

                    }

                    else if ($i >= 21 && $i <= 27)
                    {

                        $y = ($y + ($offset_top * 3));
                        for($d = 0, $f = 21; $f == $i; $d++, $f++)
                        {
                            if ($d > 0) $x = ($x + ($offset_left * $d));
                        }

                    }

                    else if ($i >= 28 && $i <= 34)
                    {

                        $y = ($y + ($offset_top * 4));
                        for($d = 0, $f = 28; $f == $i; $d++, $f++)
                        {
                            if ($d > 0) $x = ($x + ($offset_left * $d));
                        }

                    }

                    else if ($i >= 35 && $i <= 41)
                    {

                        $y = ($y + ($offset_top * 5));
                        for($d = 0, $f = 35; $f == $i; $d++, $f++)
                        {
                            if ($d > 0) $x = ($x + ($offset_left * $d));
                        }

                    }

                    echo "<img src='images/calender/icons/{$file}' style='position: absolute; top: {$y}px; left: {$x}px; z-index: 99; width: 25px; height: 25px;' />";

                }

            } else {

                echo "Error: icon is not an array or is not set";            

            }

        }

        build_icons();

?>
:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #2 (permalink)  
Old September 30th, 2003, 01:05 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

I tried writing several variations of this code in different test scripts. And not a single one of them outputs anything.

Is this a bug? Or simply not allowed?

Thanks,
: )
Rich

Code:
function foo()
{
    $foo[1] = 'test1';
    $foo[2] = 'test2';    

    foreach($foo as $key => $value)
    {

        for($d = 0; $d == 10; $d++) {echo $d;}

    }

}

function foo2()
{
    $foo[1] = 'test1';
    $foo[2] = 'test2';    

    foreach($foo as $key => $value)
    {

        loop_it();

    }

}

function loop_it()
{
    for($d = 0; $d == 10; $d++) {echo $d;}            
}

$foo[1] = 'test1';
$foo[2] = 'test2';

foreach($foo as $key => $value)
{

    loop_it();

}
:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #3 (permalink)  
Old September 30th, 2003, 01:40 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

OK after a few hours of pulling out my hair I realized the 'condition' of the statement wasn't ever being met. For some reason I assumed that when that condition was met it would break iteration. And at last I see! Its been a long day : )

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #4 (permalink)  
Old September 30th, 2003, 01:14 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yeah, Rich -- sorry I didn't get to it earlier. When you have a condition in a loop, the loop iterates only when that condition is true.

for($i = 0; $i == 10; ++$i) {...}

will NEVER iterate because before the first iteration, the condition (1 == 10) will be false, and the loop will terminate.

If you want to iterate 10 times, the convention is to use:

for($i = 0; $i < 10; ++$i)


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #5 (permalink)  
Old October 2nd, 2003, 12:19 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Thanks Nik!
I sure could have used your insight the other night.
I was so fixated on when I wanted the mathematical operations preformed on variable $x that I had momentary amnesia regarding the premise of conditional statements in loop structures. I felt pretty stupid when I realized what I was trying to do! lol :D




:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #6 (permalink)  
Old October 2nd, 2003, 01:54 AM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No problem. It's nothing to feel stupid about -- I've never worked on a project where I didn't have at least one really obvious error in my code that took way too long to find. One of the more frequent ones is a missing = in my conditionals:

echo "\$foo is [[$foo]]\n";
if ($foo = "Hello, world")
{
   echo "it's Hello, world!";
}

I would always get confused when my output would be:

$foo is [[I love pizza]]
it's Hello, world!

Take care,

Nik
http://www.bigaction.org/
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
foreach with two lists silasla C# 13 May 2nd, 2008 12:27 PM
break for-each loops, or limit amount of loops warhero XSLT 2 July 4th, 2007 02:18 AM
ForEach Loop Bushido121 BOOK: Professional SQL Server 2005 Integration Services ISBN: 0-7645-8435-9 0 October 23rd, 2006 07:55 PM
While loops and For loops in XSLT spencer.clark XSLT 1 August 5th, 2005 09:50 AM
Need help with foreach loop Arsi C# 3 September 7th, 2004 09:41 PM



All times are GMT -4. The time now is 05:12 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.