Jump to content

Repeating Region Frustration


herschen

Recommended Posts

I have created the following code and am tearing my hair out. I have a list of contact information for clients and some of them have multiple contact people within their organization. I've created several tables (phone, fax, email) and in each table I've included the field subcontact.  I've also created a separate table subContacts, which contains the main contacts name along with the "subcontact" name. So I've made a repeating region that goes through the subContacts table and is filtered by the main contact name (through a URL parameter). Inside that repeating region, I go through each table with another repeating region that will display a phone number, email, address, etc. only when the subcontact fields match. At the end of this repeating region I use this code:

<?php mysql_data_seek($rsPhone,0); ?>

to reset this field for when the subContacts table comes around again (and now has a different subcontact name).

 

Fax Numbers worked fine. However, when I tested phone numbers, the first subcontact would not display. I then ran a series of frustrating tests that verified to me that the two fields matched, but for some reason, on the first "repeat" the phone number would not display. The test I did was I replaced the table I had with the text "Go screw yourself royally" when the two fields matched. It worked flawlessly. I know the two fields are matching and the if then parameters are fine, what's wrong? If you need more information, please let me know. This is my first post in a while, so I more than likely forgot a key element. Here's the code I'm using (don't worry about the contacts that don't have subContacts...I've already handled that issue):

 

  <table width="100%" border="0" cellpadding="0" cellspacing="0" class="style1">
    <tr>
      <td width="16%"><strong>Phone Number</strong></td>
      <td width="15%"><strong>Hours</strong></td>
      <td width="57%"><strong>Department</strong></td>
  </tr>

  <?php do { ?>
  <?php if ($row_rsSubContact['subContact'] == $row_rsPhone['subContact']) { echo 'Go screw yourself royally'; ?> <br />
    <tr>
      <td><?php echo $row_rsPhone['phonenumber']; ?></td>
      <td><?php echo $row_rsPhone['hours']; ?></td>
      <td><?php echo $row_rsPhone['dept']; ?></td>
    </tr>
   <?php } ?>
<?php } while ($row_rsPhone = mysql_fetch_assoc($rsPhone)); ?>
  <?php mysql_data_seek($rsPhone,0); ?>
<br />
<br />
<?php } while ($row_rsSubContact = mysql_fetch_assoc($rsSubContact)); ?>

 

 

 

Link to comment
Share on other sites

You're running mysql_fetch_assoc() AFTER your loop (the down side of a do-while). On the first iteration you haven't fetched any data so no data will get displayed unless you're doing a mysql_fetch_assoc before the code snippet you've shown us.

 

Also keep in mind, any undefined variable is always going to be == to another undefined variable, because they're both === null.

Link to comment
Share on other sites

I actually forgot to post what I have at the top of the page (I'm doing this in Dreamweaver):

 

$colname_rsSubContact = "-1";
if (isset($_GET['contactname'])) {
  $colname_rsSublender = $_GET['contactname'];
}
mysql_select_db($database_contact, $contact);
$query_rsSubContact = sprintf("SELECT * FROM subcontact WHERE contactname = %s", GetSQLValueString($colname_rsSubContact, "text"));
$rsSubContact = mysql_query($query_rsSubContact, $contact) or die(mysql_error());
$row_rsSubContact = mysql_fetch_assoc($rsSubContact);
$totalRows_rsSubContact = mysql_num_rows($rsSubContact);

$colname_rsPhone = "-1";
if (isset($_GET['contactname'])) {
  $colname_rsLenders = $_GET['contactname'];
}
mysql_select_db($database_contact, $contact);
$query_rsPhone= sprintf("SELECT * FROM phone WHERE contactname = %s", GetSQLValueString($colname_rsPhone, "text"));
$rsPhone = mysql_query($query_rsPhone, $contact) or die(mysql_error());
$row_rsPhone = mysql_fetch_assoc($rsPhone);
$totalRows_rsPhone = mysql_num_rows($rsPhone);

 

I think that this is already calling the first record in the mysql database...right? Thanks for the quick response.

 

Link to comment
Share on other sites

When I have trouble with things echoing, I usually try to narrow down *exactly* where the error is occurring. There dirty way to do it is to include an "echo $variable_name;" command around your code, replacing $variable_name with your actual variable that you want to print.

 

My suggestion is to work top down. If "echo $row_rsPhone['phonenumber'];" isn't working, put "echo $row_rsPhone['phonenumber'];" right after the first $row_rsPhone = mysql_fetch_assoc($whatever); and see if it works there. If it doesn't, you know that the database isn't returning a value. If it does work, you know your value is either getting changed or loosing scope (and you just need to figure out where).

 

In the case of the latter, you can figure out where things are going wrong by moving the "echo $row_rsPhone['phonenumber'];" further down in the code anywhere where you think something might effect the value of $row_rsPhone['phonenumber'] or it's scope.

Link to comment
Share on other sites

I have figured out where the problem is ocurring, yet I still cannot figure out why. Maybe someone can help me. I put the

echo $row_rsPhone['phonenumber'];

right after the if parameters and it works fine

if ($row_rsSubContact['subContact'] == $row_rsPhone['subContact']) { echo $row_rsPhone['phonenumber']; 

 

However, when I put that same code after the if parameters in a table row, it doesn't display.

<?php do { ?>
   <tr>
      <td width="16%"><strong>Phone Number</strong></td>
      <td width="15%"><strong>Hours</strong></td>
      <td width="57%"><strong>Department</strong></td>
  </tr>

  <?php do { ?>
  <?php if ($row_rsSubContact['subContact'] == $row_rsPhone['subContact']) { echo $row_rsPhone['phonenumber']; ?> <br />
    <tr>
      <td><?php echo $row_rsPhone['phonenumber']; ?></td>
      <td><?php echo $row_rsPhone['hours']; ?></td>
      <td><?php echo $row_rsPhone['dept']; ?></td>
    </tr>
   <?php } ?>
<?php } while ($row_rsPhone = mysql_fetch_assoc($rsPhone)); ?>
  <?php mysql_data_seek($rsPhone,0); ?>
<?php } while ($row_rsSubContact = mysql_fetch_assoc($rsSubContact)); ?>

 

Keep in mind that this is only for the first time around in the repeat region. The rest of the times that the repeating region...repeats, it displays fine. I'm not understanding what makes that <td> field so intimidating for php or mysql. Thank you.

Link to comment
Share on other sites

Also, I just noticed that the top part of the table:

<table width="100%" border="0" cellpadding="0" cellspacing="0" class="style1">
    <tr>
      <td width="16%"><strong>Phone Number</strong></td>
      <td width="15%"><strong>Hours</strong></td>
      <td width="57%"><strong>Department</strong></td>
  </tr>

doesn't display either. This is real odd.

 

The other thing that is odd, is that I replaced the first row in the table with the statement "What the heck is going on?"  This showed up, however the row with Phone Number, Hours and Dept. did not. Furthermore, in the following "repeats" the phone numbers display properly, but the statement "What the heck is going on?" shows up after those phone numbers (this is odd because as far as the html goes, that statement comes before the 2nd row, where all of the phone numbers are displayed.)

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.