12 Replies - 963 Views - Last Post: 08 September 2012 - 04:28 AM Rate Topic: -----

#1 webwired   User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Help with loops

Posted 07 September 2012 - 05:42 PM

Hello everyone...

Could someone please help me with this looping problem that I am having...

I have a WHILE loop that iterates through the matching database records... and then inside of that loop I have a FOR loop... As you can probably already imagine, for every record there is in the database, it repeats the FOR loop that many times... I am baffled if I can think of a different way to code it though...

Here's what I have...
$query = "SELECT OrderTickets.*, Appointments.* FROM Appointments 
    LEFT JOIN OrderTickets ON Appointments.OrderTicketID = OrderTickets.OrderTicketID 
    WHERE Appointments.AppointmentStartDateTime BETWEEN '$BeginningDateTime' AND '$EndingDateTime'";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
    for ($i=1;$i<25;$i+=0.25)
    {



I'd appreciate any advice anyone can give me...

Is This A Good Question/Topic? 0
  • +

Replies To: Help with loops

#2 Sho Ke   User is offline

  • D.I.C Regular
  • member icon

Reputation: 110
  • View blog
  • Posts: 250
  • Joined: 13-October 11

Re: Help with loops

Posted 07 September 2012 - 06:10 PM

..What exactly are you trying to do with those loops? Are you getting an error? Is that the full code?
Was This Post Helpful? 0
  • +
  • -

#3 webwired   User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Re: Help with loops

Posted 07 September 2012 - 06:19 PM

View PostSho Ke, on 07 September 2012 - 08:10 PM, said:

..What exactly are you trying to do with those loops?


Loop

View PostSho Ke, on 07 September 2012 - 08:10 PM, said:

Are you getting an error?


No, if I had, one would think that I would have mentioned that.

View PostSho Ke, on 07 September 2012 - 08:10 PM, said:

Is that the full code?


Let me answer your question with a question, do you typically find full code with open brackets?


You know, I am, and I'm sure that I can't be the only one... That is sick and tired of someone like you posting the first reply to a post with absolute garbage... For future reference, if you don't have something to contribute to a post, STAY THE F**K AWAY!
Was This Post Helpful? 0
  • +
  • -

#4 Sho Ke   User is offline

  • D.I.C Regular
  • member icon

Reputation: 110
  • View blog
  • Posts: 250
  • Joined: 13-October 11

Re: Help with loops

Posted 07 September 2012 - 06:33 PM

Maybe if you would explain what you need help with, no one would have to ask?

You said you have two loops, then you ask for advice. Advice for what? "Looping"? If you're asking for help on how to "loop", then just slap on two ending braces and you're "looping". But I doubt that's what you want.

Then again, I don't know what you want to do because you didn't tell me.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: Help with loops

Posted 07 September 2012 - 06:35 PM

View Postwebwired, on 07 September 2012 - 08:42 PM, said:

I have a WHILE loop that iterates through the matching database records... and then inside of that loop I have a FOR loop... As you can probably already imagine, for every record there is in the database, it repeats the FOR loop that many times... I am baffled if I can think of a different way to code it though...

Here's what I have...
$query = "SELECT OrderTickets.*, Appointments.* FROM Appointments 
    LEFT JOIN OrderTickets ON Appointments.OrderTicketID = OrderTickets.OrderTicketID 
    WHERE Appointments.AppointmentStartDateTime BETWEEN '$BeginningDateTime' AND '$EndingDateTime'";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
    for ($i=1;$i<25;$i+=0.25)
    {


You haven't stated what the problem is so you should expect people to start from square one. Of course the for loop will be repeated for every record found in the table. That's what it means to put the for loop inside the while loop.
Was This Post Helpful? 0
  • +
  • -

#6 webwired   User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Re: Help with loops

Posted 07 September 2012 - 06:46 PM

View PostCTphpnwb, on 07 September 2012 - 08:35 PM, said:

You haven't stated what the problem is so you should expect people to start from square one. Of course the for loop will be repeated for every record found in the table. That's what it means to put the for loop inside the while loop.


If I didn't state what my problem was, then why did you just mention it in your reply?

BTW: Sho Ke, stay away from my posts.

This post has been edited by webwired: 07 September 2012 - 06:47 PM

Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: Help with loops

Posted 07 September 2012 - 06:50 PM

So your problem is that the code is doing what you told it to do? The solution then is simple: don't tell it to do that! If it's something else, please tell us. Maybe you could start with why you've put a for loop inside a while loop.
Was This Post Helpful? 1
  • +
  • -

#8 webwired   User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Re: Help with loops

Posted 07 September 2012 - 06:55 PM

View PostCTphpnwb, on 07 September 2012 - 08:50 PM, said:

So your problem is that the code is doing what you told it to do? The solution then is simple: don't tell it to do that! If it's something else, please tell us. Maybe you could start with why you've put a for loop inside a while loop.


Well, with the FOR loop I'm creating a scheduling calendar... With the WHILE loop I was pulling from the database any appointments that fall in the calendar's timeline... When I wrote the code I only used one appointment to test it with... after I started adding other appointments on the same day I realized that I had put a loop inside of a loop... but now can't think of a way to write the code so that it's not a loop inside of a loop...

$BeginningDateTime = date('Y-m-d 00:00:00', strtotime($SelectedDay));
$EndingDateTime = date('Y-m-d 23:59:59', strtotime($SelectedDay));
$query = "SELECT OrderTickets.*, Appointments.* FROM Appointments LEFT JOIN OrderTickets ON Appointments.OrderTicketID = OrderTickets.OrderTicketID WHERE Appointments.AppointmentStartDateTime BETWEEN '$BeginningDateTime' AND '$EndingDateTime'";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
    for ($i=1;$i<25;$i+=0.25)
    {
        $rowCounter += 1;
        echo '<tr class="'.OddOrEven2($rowCounter).'">';
        echo '<td class="time"> '; 
        if ($i > 12.5){$e = $i - 12;} else {$e = $i;} 
        $e = number_format($e, 2, '.', ' ');
        list($whole, $decimal) = explode('.', $e);
        if ($decimal == 25){$decimal = 15;}
        if ($decimal == 50){$decimal = 30;}
        if ($decimal == 75){$decimal = 45;}
        if ($whole == 0){$whole = 12;}
        if ($i < 12 || $i == 24){$f = 'am';}else{$f = 'pm';}
        if ($decimal == 00){echo $whole.':'.$decimal.' '.$f;}
        echo '</td>';

        $theBeginningTime = date('H:i:s', strtotime($row['AppointmentStartDateTime']));
        $theEndingTime = date('H:i:s', strtotime($row['AppointmentFinishDateTime']));
        $theTime = $whole.':'.$decimal.' '.$f;
        $LoopTime = date('H:i:s', strtotime($theTime));
        if ($LoopTime == $theBeginningTime || $LoopTime == $theEndingTime || ($LoopTime > $theBeginningTime && $LoopTime < $theEndingTime))
        {
            echo '<form id="CalendarCell" action="calendar.php?eventaction" method="post">';
            echo '<input type="hidden" id="AppointmentID" name="AppointmentID" value="'.$row['AppointmentID'].'" />';
            echo '<input type="hidden" id="SelectedTime" name="SelectedTime" value="'.$LoopTime.'" />';
            echo '<td class="appointment" style="background-color: #7C92AA;">';
            if ($LoopTime == $theBeginningTime)
            {
                echo date('g:i a', strtotime($row['AppointmentStartDateTime'])).' - '.date('g:i a', strtotime($row['AppointmentFinishDateTime'])).'<br />';
                if (!empty($row['OrderTicketID']))
                {
                    echo $row['OrderTicketFirstName'].' ' .$row['OrderTicketLastName'].' - '.$row['OrderTicketZipCode'].'<br />
                    '.FetchRequirements($row['OrderTicketID']);
                }
                else
                {
                    echo '<pre>'.$row['WhatsHappening'].'</pre>';
                }
            }
            else
            {
                echo '&nbsp;';
            }
            echo '</td>';
            echo '</form>';
        }
        else 
        {
            echo '<form id="CalendarCell" action="calendar.php?eventaction" method="post">';
            echo '<input type="hidden" id="SelectedTime" name="SelectedTime" value="'.$LoopTime.'" />';
            echo '<td class="appointment">&nbsp;</td>';
            echo '</form>';
        }
        echo '</tr>';
    }
}


This post has been edited by webwired: 07 September 2012 - 06:56 PM

Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: Help with loops

Posted 07 September 2012 - 07:07 PM

It looks like you're trying to use $i to display time slots through the day, but you're doing that before you've got all the time slots. I would read everything into an associative array where the time is the key and then display the array. That way I could initialize the array to all blank values and used the database to fill in whatever it finds.

By the way, you really should be using prepared statements and not deprecated mysql* functions.
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15790
  • View blog
  • Posts: 63,277
  • Joined: 12-June 08

Re: Help with loops

Posted 07 September 2012 - 07:17 PM

Everyone relax a bit. webwired - in the future if you are not getting errors, but instead getting odd behavior it's best to also explain what you would like the behavior to do in addition to what you put down (what it is currently doing). Once everyone has a picture then everyone can join arms and skip down the path of help and learning together! (humming Kumbaya is optional) ;)
Was This Post Helpful? 0
  • +
  • -

#11 webwired   User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Re: Help with loops

Posted 07 September 2012 - 07:21 PM

View Postmodi123_1, on 07 September 2012 - 09:17 PM, said:

Everyone relax a bit. webwired - in the future if you are not getting errors, but instead getting odd behavior it's best to also explain what you would like the behavior to do in addition to what you put down (what it is currently doing). Once everyone has a picture then everyone can join arms and skip down the path of help and learning together! (humming Kumbaya is optional) ;)


You bet :rolleyes:

View PostCTphpnwb, on 07 September 2012 - 09:07 PM, said:

It looks like you're trying to use $i to display time slots through the day, but you're doing that before you've got all the time slots. I would read everything into an associative array where the time is the key and then display the array. That way I could initialize the array to all blank values and used the database to fill in whatever it finds.

By the way, you really should be using prepared statements and not deprecated mysql* functions.


I'm going to have to meditate on the Associative Array solution... Sounds like a plan, just takes me a while to put it together in my head first...

As far as the deprecated mysql, I know... It's all I know for now though, this application just has to last a little while before I have it professionally rewritten...

This post has been edited by webwired: 07 September 2012 - 07:22 PM

Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: Help with loops

Posted 07 September 2012 - 07:26 PM

A little while may be all it takes to have your site hacked.

There are good PHP tutorials on this site, and then there are excellent ones like this one.
Was This Post Helpful? 1
  • +
  • -

#13 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Help with loops

Posted 08 September 2012 - 04:28 AM

Doesn't this code render multiple forms with the id of CalendarCell? You can't have valid HTML with duplicate element IDs.

You want one form, and one form only.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1