-1

Ok so I didn't see anything like this in the previous postings so if if has been covered before sorry for the repost.

I am trying to put a second do..while statement in the same page but when I do this it seems to drag down the page. it loads fine if I have only one statement in there but the minute I put in a second one it takes forever to load the page. I have a need of adding a total of 9 do..while statements which will most likely kill the page at this point. I'm not sure why it would be doing this any ideas?

The code I'm using is as follows

<p><?php
    do {
    $row_myContacts = mysql_fetch_assoc($myContacts);
    if ($row_myContacts['staffID'] != "81"){                                
    }
    else {
     echo $row_myContacts['firstName'] . " " . $row_myContacts['lastName'];?></p>
                  <p class="staff_title_national"><?php echo $row_nmyContacts['titleName'];?></p>
                  <br />
                  <li><?php echo $row_myContacts['address1'] 
. " " . $row_myContacts['address2'] ;?></li>
                  <li><?php echo $row_myContacts['cityName'] 
. ", " . $row_myContacts['state_abreviation'] . " " 
. $row_myContacts['zipCode'];?></li>
                  <br />
                  <li>Office: <?php echo format_phone($new_office_phone); ?></li>
                  <li>Fax: <?php echo format_phone($new_fax_phone); ?></li>
                  <br />
                  <br />
                  <li>E-Mail: 
<a href="<?php echo $row_myContacts['email'];?>"><?php echo $row_myContacts['email'];?></a></li>
                  <br />
           <?php }
    } while ($row_myContacts['staffID'] != '81')?>

I have tried testing it so that the staffID is == to the number i need, but it doesn't output anything. I think it should be that way but for some reason it doesn't work. and yes I moved the stuff in that variation so that the else was empty and the if had the information in it.

The page holds 4 sections of contacts under section 1 there is one section with 2 names (same address info) section 2 has 3 names different info, sections 3 and 4 have 2 each with different info. I even created 2 new tables in my database that reference the 4 sections if that helps. I do know my sql statement is correct it pulls all 9 contacts with their corresponding information.

/edit below/

so I also tried the code this way. and I do get it to load the first one but when I add a section for loop it shows nothing. I think it is because I am telling the loop to access the database at the same point that it left off so it isn't starting over on the loop.

<?php
            for ($x=$totalRows_myContacts; $x>=1; $x--){

                if ($row_myContacts['staffID'] == '81') {
                echo $row_myContacts['firstName'] . " " . $row_myContacts['lastName'];?></p>
                  <p class="staff_title"><?php echo $row_myContacts['titleName'];?></p>
                  <br />
                  <li><?php echo $row_myContacts['address1'] . " " . $row_myContacts['address2'] ;?></li>
                  <li><?php echo $row_myContacts['cityName'] . ", " . $row_myContacts['state_abreviation'] . " " . $row_myContacts['zipCode'];?></li>
                  <br />
                  <li>Office: <?php echo format_phone($new_office_phone); ?></li>
                  <li>Fax: <?php echo format_phone($new_fax_phone); ?></li>
                  <br />
                  <br />
                  <li class="emailTealGrey">E-Mail: <a href="<?php echo $row_myContacts['email'];?>"><?php echo $row_myContacts['email'];?></a></li>
                  <br />   

            <?php }$row_myContacts = mysql_fetch_assoc($Contacts);  
            }?>

so not sure how to tell the database to access itself again and start the query from the top I guess is what I need to do. any ideas how to tell it to do that?

1 Answer 1

1

What happens if there is no row for staffID=81 change code to something like this,

.....
do {
    $row_myContacts = mysql_fetch_assoc($myContacts);
    if ($row_myContacts == false){
        break;
    }
.....
Sign up to request clarification or add additional context in comments.

5 Comments

ok so this sort of fixes my problem but now it won't find staff member 81 which is the one I need or any other staff member I put in there.
Check if your query returns staffID = 81. Also check if there is leading/trailing space in the returned value.
I am getting the 9 staff members including staff 81. like the for loop i added above it will display the first query but when i add a second one it shows empty. Do you know how to make the query start from the beginning instead of where it left off from the last one?
I don't know why you may want to iterate through the same result set more than once, but here is the solution: check this stackoverflow question Multiple PHP WHILE loops using the same query
ahh thank you yes that answers my question. and the reason is because I have a set of contacts that are not in a particular order and I need to find their staff information so that I can display it in different sections of the page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.