Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,679 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,199 people online right now. Registration is fast and FREE... Join Now!




not sure where this is going wrong - nested while

 
Reply to this topicStart new topic

not sure where this is going wrong - nested while

-fedexer-
post 4 Jun, 2008 - 10:33 AM
Post #1


New D.I.C Head

*
Joined: 3 Jun, 2008
Posts: 41



Thanked 2 times
My Contributions


Well i have had to go an opposite direction from where i wanted to be heading, i am currently designing a site for a group at my school, but none of them know how to make changes etc so i am literally creating my own CMS for the crack >.>

I am currently working on having the navigation load from 2 database tables, one for the headers, and one for the sub items of those headers.


CODE
<?php while($headers = mysql_fetch_array($result)){   ?>
    <tr>
      <td><table>
          <tr>
            <th><?php if($headers['headerLink'] != NULL) {  
            echo "<a href=\"{$headers['headerLink']}\">{$headers['headerName']} {$headers['headerId']}</a>";  
            }else{  
            echo $headers['headerName']; echo " ".$headers['headerId'];
            }?></th>
          </tr>
          <?php while($subs = mysql_fetch_array($result1)){  
                        if($subs['relatedHeaderId'] == $headers['headerId']){
                           echo "<tr><td><a href=\"{$subs['subLink']}\">{$subs['subName']}</a></td></tr>";  
                                                                          }
                                                           }  
            ?>
        </table></td>
    </tr>
    <?php } ?>


Data retreival:


CODE
$query        = sprintf("SELECT * FROM menuhead");
$result     = mysql_query($query) or die("Error 4973: Please inform the web administrator of this error");
$headers        = mysql_fetch_array($result);

$query1        = sprintf("SELECT * FROM menusub");
$result1    = mysql_query($query1) or die("Error 4974: Please inform the web administrator of this error");
$subs        = mysql_fetch_array($result1);  



The headers all get loaded fine , it's when it comes to the sub-items, being called by the second while loop nested inside the first that causes the problem, the sub items of the first header will appear, but thereafter no sub items are shown, i for the life of me can't see where it is going wrong and am looking for assistance in tracking down the problem.

Thanks guys
-fedexer-

This post has been edited by -fedexer-: 4 Jun, 2008 - 10:34 AM
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 4 Jun, 2008 - 10:46 AM
Post #2


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 596



Thanked 22 times

Dream Kudos: 750
My Contributions


In your data retrieval, try taking out the

CODE

$subs = mysql_fetch_array($result1);


That might be causing the other $subs = mysql_fetch_array() to not work.

This post has been edited by akozlik: 4 Jun, 2008 - 10:47 AM
User is offlineProfile CardPM

Go to the top of the page

-fedexer-
post 4 Jun, 2008 - 10:52 AM
Post #3


New D.I.C Head

*
Joined: 3 Jun, 2008
Posts: 41



Thanked 2 times
My Contributions


No luck there , that just causes nothing to happen, as it is comparing the two, therefore it needs the first.
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 4 Jun, 2008 - 11:09 AM
Post #4


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 596



Thanked 22 times

Dream Kudos: 750
My Contributions


QUOTE(-fedexer- @ 4 Jun, 2008 - 01:52 PM) *

No luck there , that just causes nothing to happen, as it is comparing the two, therefore it needs the first.


Hmmm, can you echo out the values?
User is offlineProfile CardPM

Go to the top of the page

-fedexer-
post 4 Jun, 2008 - 11:58 AM
Post #5


New D.I.C Head

*
Joined: 3 Jun, 2008
Posts: 41



Thanked 2 times
My Contributions


I attempted to echo out the array, using print_r($subs); it would seem it only ever gets done once, whereas print_r($headers); executes for all of the headers...
I think you could quite literally say this one has me stumped.

This post has been edited by -fedexer-: 4 Jun, 2008 - 12:00 PM
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 4 Jun, 2008 - 12:04 PM
Post #6


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 596



Thanked 22 times

Dream Kudos: 750
My Contributions


QUOTE(-fedexer- @ 4 Jun, 2008 - 02:58 PM) *

I attempted to echo out the array, using print_r($subs); it would seem it only ever gets done once, whereas print_r($headers); executes for all of the headers...
I think you could quite literally say this one has me stumped.


Can you post what you see if you view the pages source after the script has been run?
User is offlineProfile CardPM

Go to the top of the page

-fedexer-
post 4 Jun, 2008 - 12:11 PM
Post #7


New D.I.C Head

*
Joined: 3 Jun, 2008
Posts: 41



Thanked 2 times
My Contributions


is the code generated:

CODE
<tr>
      <td><table>
          <tr>
            <th><a href="index.php">Index</a></th>
          </tr>
        
          <tr><td><a href="index.php">testhead1-2</a></td></tr>        </table></td>

    </tr>
        <tr>



note for that example there should have been an extra head "testhead1" which comes first in the database, but is clearly ignored by the php...

Also it is tabled as i am using a navigation script located elsewhere.

Also thanks for the help so far akozlik

This post has been edited by -fedexer-: 4 Jun, 2008 - 12:12 PM
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 4 Jun, 2008 - 12:16 PM
Post #8


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 596



Thanked 22 times

Dream Kudos: 750
My Contributions


QUOTE

Also thanks for the help so far akozlik


Sure thing, thanks for the thanks. I just had a family emergency pop up, so I might not be able to get back to this post until later this evening. Sorry for any delay. I'll let you know when I can get back to helping out, or hopefully somebody else can pick up where we left off.
User is offlineProfile CardPM

Go to the top of the page

JBrace1990
post 4 Jun, 2008 - 12:18 PM
Post #9


D.I.C Regular

Group Icon
Joined: 9 Mar, 2008
Posts: 474



Thanked 21 times

Dream Kudos: 350
My Contributions


i have two things you might want to try....
1. try removing the sprintf from the queries
2. remove the If statement in the second While and see where it goes.

alternately, move $result1 into the while loop, so that it selects ones from the database that are from that specific header...

for example, add a "WHERE relatedHeaderId = '$headers['headerId']'"
User is offlineProfile CardPM

Go to the top of the page

-fedexer-
post 4 Jun, 2008 - 12:35 PM
Post #10


New D.I.C Head

*
Joined: 3 Jun, 2008
Posts: 41



Thanked 2 times
My Contributions


Now you made me think JBrace, partly because i had tried it before, but i never thought about removing my if statement, so now with this:

CODE
<?php
             $query1     = sprintf("SELECT * FROM menusub WHERE relatedHeaderId = {$headers['headerId']}");
            $result1    = mysql_query($query1) or die("Error 4974: Please inform the web administrator of this error");
            $subs       = mysql_fetch_array($result1);  
            do{
                  //    print_r($subs);
                       // if($subs['relatedHeaderId'] == $headers['headerId']){
                           echo "<tr><td><a href=\"{$subs['subLink']}\">{$subs['subName']}</a></td></tr>";
                                                                         // }
                                                           }while($subs = mysql_fetch_array($result1));
            ?>


The headers and sub headers are printed as they should be, the only problem is (and this is why i had the if statement to begin with) is that now even the headers without a subitem get the subitem drop down, which is cause due to the do .. while loop, but as a while .. loop doesn't work (it misses the first sub item and prints the rest).

Hmm any ideas on how i can get out of this one?

Also no problem akozlik , i'm happy for all the help i get, and thanks JBrace for your contribution which made me actually sit and question why i had the if statement to begin with.


Edit:

Just went back and had a look, cracked it ph34r.gif

CODE

<?php
             $query1     = sprintf("SELECT * FROM menusub WHERE relatedHeaderId = {$headers['headerId']}");
            $result1    = mysql_query($query1) or die("Error 4974: Please inform the web administrator of this error");
            $subs       = mysql_fetch_array($result1);  
            do{
                  //    print_r($subs);
                       // if($subs['relatedHeaderId'] == $headers['headerId']){
                    if($headers['headerId'] == $subs['relatedHeaderId'])
                           echo "<tr><td><a href=\"{$subs['subLink']}\">{$subs['subName']}</a></td></tr>";
                                                                         // }
                                                           }while($subs = mysql_fetch_array($result1));
            ?>



Now we're all good, thanks again akozlik and JBrace.

(now i've made myself look like a right eejit.)

This post has been edited by -fedexer-: 4 Jun, 2008 - 12:39 PM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 06:41AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month