Jump to content

loop within a loop not working :(


Presto-X

Recommended Posts

Hello guys I'm trying to get a loop with in a loop to work, I have a table that holds a list of courses and I have another table that holds a list of dates of upcoming courses. My code will display all of the courses as it should and then it will show all of the dates within the first course and it displays that there are no upcoming dates in all of the other courses but I know that I have dates for the others as well.

 

This is my mess of code I'm trying to get to work: (Yes I know it's mess sorry)

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_displaycourses = "-1";
if (isset($_GET['catid'])) {
  $colname_displaycourses = $_GET['catid'];
}
mysql_select_db($database_WildernessRescue, $WildernessRescue);
$query_displaycourses = sprintf("SELECT * FROM jos_courses WHERE catid = %s ORDER BY title ASC", GetSQLValueString($colname_displaycourses, "text"));
$displaycourses = mysql_query($query_displaycourses, $WildernessRescue) or die(mysql_error());
$row_displaycourses = mysql_fetch_assoc($displaycourses);
$totalRows_displaycourses = mysql_num_rows($displaycourses);

$colname_displaycoursedates = "-1";
if (isset($row_displaycourses['id'])) {
  $colname_displaycoursedates = $row_displaycourses['id'];
}
mysql_select_db($database_WildernessRescue, $WildernessRescue);
$query_displaycoursedates = sprintf("SELECT * FROM jos_courses_dates WHERE courseid = %s ORDER BY course_start_date ASC", GetSQLValueString($colname_displaycoursedates, "text"));
$displaycoursedates = mysql_query($query_displaycoursedates, $WildernessRescue) or die(mysql_error());
$row_displaycoursedates = mysql_fetch_assoc($displaycoursedates);
$totalRows_displaycoursedates = mysql_num_rows($displaycoursedates);

$colname_listcourses = "-1";
if (isset($_GET['catid'])) {
  $colname_listcourses = $_GET['catid'];
}
mysql_select_db($database_WildernessRescue, $WildernessRescue);
$query_listcourses = sprintf("SELECT id, title FROM jos_courses WHERE catid = %s ORDER BY title ASC", GetSQLValueString($colname_listcourses, "text"));
$listcourses = mysql_query($query_listcourses, $WildernessRescue) or die(mysql_error());
$row_listcourses = mysql_fetch_assoc($listcourses);
$totalRows_listcourses = mysql_num_rows($listcourses);
?>
<ol>
  <?php do { ?>
  <li style="font-weight:bold;"><a href="#<?php echo $row_listcourses['id']; ?>"><?php echo $row_listcourses['title']; ?></a></li>
  <?php } while ($row_listcourses = mysql_fetch_assoc($listcourses)); ?>
</ol>

<?php do { ?>
<a name="<?php echo $row_displaycourses['id']; ?>"></a>
<div class="dateTitle"><?php echo $row_displaycourses['title']; ?><?php if (($my->usertype == 'Super Administrator')or($my->usertype == 'Administrator')or($my->usertype == 'Manager')){
    echo '<a href="index.php?option=com_courses&catid='.$_GET['catid'].'&id='.$row_displaycourses['id'].'&Itemid=12&session=Deleting%20Course" class="red_button" onclick="return confirm(\'Deleting this Course cannot be undone! Are you certain you want to continue with the deletion?\')">Delete Course</a><a href="index.php?option=com_courses&catid='.$_GET['catid'].'&id='.$row_displaycourses['id'].'&Itemid=12&session=Update%20Course%20Information" class="red_button">Edit Course</a>';
} ?>
</div>
<div class="dateDescription"><?php echo $row_displaycourses['description']; ?></div>
<br />
<table width="100%" border="0" cellspacing="0" cellpadding="4">
  <tr style="font-weight:bold; font-size:9px; color:#999;">
    <td width="16%">Start / End Dates</td><td width="14%">Location</td><td width="14%">Availability</td><td width="8%">Tuition</td><td width="14%">Tools</td><td width="8%">Register</td><td width="50%"><?php if (($my->usertype == 'Super Administrator')or($my->usertype == 'Administrator')or($my->usertype == 'Manager')){ ?><a href="index.php?option=com_courses&courseid=<?php echo $row_displaycourses['id']; ?>&catid=<?php echo $_GET['catid'] ?>&Itemid=12&session=New Date Information"><div class="bodyedit" style="float:right; margin-bottom:-5px;">New Date</div></a><?php } ?></td>
  </tr>
  <?php
if (empty($row_displaycoursedates['id'])){ ?>
<tr class="<?php echo $even_odd; ?>">
  <td colspan="7"><img src="administrator/components/com_courses/images/error.png" align="left" style="margin:0px 4px;"> <em>Sorry, there are no upcoming dates scheduled for this course at this time, please try back later.</em></td>
</tr>
<?php
}else{

$even_odd = "";
do {
$even_odd = ( 'odd' != $even_odd ) ? 'odd' : 'even';
if ($row_displaycoursedates['course_availability'] == "*FULL*"){$availability_style = 'style="color:#990000; font-weight:bold;"';}
if ($row_displaycoursedates['course_availability'] == "*CANCELLED*"){$availability_style = 'style="color:#990000; font-weight:bold;"';}
if ($row_displaycoursedates['course_availability'] == "Available!"){$availability_style = 'style="color:#669900; font-weight:bold;"';}
?>
  <tr class="<?php echo $even_odd; ?>">
    <td class="date">
  <?php echo date('M d, Y', strtotime($row_displaycoursedates['course_start_date'])); ?><br />
  <?php echo date('M d, Y', strtotime($row_displaycoursedates['course_end_date'])); ?>
    </td>
    <td class="dateLocation" nowrap><?php echo $row_displaycoursedates['course_location']; ?></td>
    <td <?php echo $availability_style; ?> nowrap><?php echo $row_displaycoursedates['course_availability']; ?></td>
    <td class="dateTuition" nowrap>$<?php echo $row_displaycourses['price']; ?></td>
    <td nowrap>
      <?PHP if ($row_displaycoursedates['course_map']){ ?><a href="http://maps.google.com/maps?q=<?php echo $row_displaycoursedates['course_map']; ?>" onMouseover="tooltip('<b>Google Map:</b> Map this location with Google maps.')"; onMouseout="hidetooltip()" target="_blank"><img src="administrator/components/com_courses/images/page_white_world.png" border="0"></a> <?PHP }else{ ?><img src="administrator/components/com_courses/images/page_white_world_off.png" onMouseover="tooltip('<b>Google Map:</b> (Not Available).')"; onMouseout="hidetooltip()"><?PHP } ?>
      <?PHP if ($row_displaycoursedates['course_pdf']){ ?><a href="pdf/<?php echo $row_displaycoursedates['course_pdf']; ?>" onMouseover="tooltip('<b>PDF File:</b> Download a PDF document of this courses information.')"; onMouseout="hidetooltip()" target="_blank"><img src="administrator/components/com_courses/images/page_white_acrobat.png" border="0"></a> <?PHP }else{ ?><img src="administrator/components/com_courses/images/page_white_acrobat_off.png" onMouseover="tooltip('<b>PDF File:</b> (Not Available).')"; onMouseout="hidetooltip()"><?PHP } ?>
      <?PHP if ($row_displaycoursedates['course_other_info']){ ?><img src="administrator/components/com_courses/images/page_white_text.png" onMouseover="tooltip('<b>Notes:</b> <span style=color:#990000;><?php echo $row_displaycoursedates['course_other_info']; ?></span>')"; onMouseout="hidetooltip()"><?PHP }else{ ?><img src="administrator/components/com_courses/images/page_white_text_off.png" onMouseover="tooltip('<b>Notes:</b> (Not Available).')"; onMouseout="hidetooltip()"><?PHP } ?>
    </td>
    <td nowrap><a href="#<?php #echo '?option=com_courses&Itemid=12&catid='.$_GET['catid'].'&courseid='.$row_displaycoursedates['courseid'].'&dateid='.$row_displaycoursedates['id'].'&session=Course%20Registration'; ?>"><div class="register">Register</div></a></td>
    <td nowrap>
      <?php if (($my->usertype == 'Super Administrator') or ($my->usertype == 'Administrator') or ($my->usertype == 'Manager')){ ?>
        <a href="index.php?option=com_courses&catid=<?php echo $row_displaycourses['catid']; ?>&courseid=<?php echo $row_displaycoursedates['courseid']; ?>&id=<?php echo $row_displaycoursedates['id']; ?>&Itemid=12&session=Update%20Date%20Information"><div class="bodyedit">Edit</div></a><a href="index.php?option=com_courses&catid=<?php echo $row_displaycourses['catid']; ?>&id=<?php echo $row_displaycoursedates['id']; ?>&Itemid=12&session=Deleting%20Date"><div class="bodyedit">Delete</div></a>
      <?php } ?>
    </td>
  </tr>
  <?php } while ($row_displaycoursedates = mysql_fetch_assoc($displaycoursedates)); } ?>
</table>
<br />
<?php } while ($row_displaycourses = mysql_fetch_assoc($displaycourses)); ?>
<?php
mysql_free_result($displaycourses);
mysql_free_result($displaycoursedates);
mysql_free_result($listcourses);
?>

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/124604-loop-within-a-loop-not-working/
Share on other sites

It may help if I cut down the code to the first do{ to make it easer to read.

 

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_displaycourses = "-1";
if (isset($_GET['catid'])) {
  $colname_displaycourses = $_GET['catid'];
}
mysql_select_db($database_WildernessRescue, $WildernessRescue);
$query_displaycourses = sprintf("SELECT * FROM jos_courses WHERE catid = %s ORDER BY title ASC", GetSQLValueString($colname_displaycourses, "text"));
$displaycourses = mysql_query($query_displaycourses, $WildernessRescue) or die(mysql_error());
$row_displaycourses = mysql_fetch_assoc($displaycourses);
$totalRows_displaycourses = mysql_num_rows($displaycourses);

$colname_displaycoursedates = "-1";
if (isset($row_displaycourses['id'])) {
  $colname_displaycoursedates = $row_displaycourses['id'];
}
mysql_select_db($database_WildernessRescue, $WildernessRescue);
$query_displaycoursedates = sprintf("SELECT * FROM jos_courses_dates WHERE courseid = %s ORDER BY course_start_date ASC", GetSQLValueString($colname_displaycoursedates, "text"));
$displaycoursedates = mysql_query($query_displaycoursedates, $WildernessRescue) or die(mysql_error());
$row_displaycoursedates = mysql_fetch_assoc($displaycoursedates);
$totalRows_displaycoursedates = mysql_num_rows($displaycoursedates);

$colname_listcourses = "-1";
if (isset($_GET['catid'])) {
  $colname_listcourses = $_GET['catid'];
}
mysql_select_db($database_WildernessRescue, $WildernessRescue);
$query_listcourses = sprintf("SELECT id, title FROM jos_courses WHERE catid = %s ORDER BY title ASC", GetSQLValueString($colname_listcourses, "text"));
$listcourses = mysql_query($query_listcourses, $WildernessRescue) or die(mysql_error());
$row_listcourses = mysql_fetch_assoc($listcourses);
$totalRows_listcourses = mysql_num_rows($listcourses);
?>
<ol>
  <?php do { ?>
  <li style="font-weight:bold;"><a href="#<?php echo $row_listcourses['id']; ?>"><?php echo $row_listcourses['title']; ?></a></li>
  <?php } while ($row_listcourses = mysql_fetch_assoc($listcourses)); ?>
</ol>

 

So this is the first major do, it starts by course, the big red heading rows in the attached image.

<?php do { ?>
<a name="<?php echo $row_displaycourses['id']; ?>"></a>
<div class="dateTitle"><?php echo $row_displaycourses['title']; ?><?php if (($my->usertype == 'Super Administrator')or($my->usertype == 'Administrator')or($my->usertype == 'Manager')){
    echo '<a href="index.php?option=com_courses&catid='.$_GET['catid'].'&id='.$row_displaycourses['id'].'&Itemid=12&session=Deleting%20Course" class="red_button" onclick="return confirm(\'Deleting this Course cannot be undone! Are you certain you want to continue with the deletion?\')">Delete Course</a><a href="index.php?option=com_courses&catid='.$_GET['catid'].'&id='.$row_displaycourses['id'].'&Itemid=12&session=Update%20Course%20Information" class="red_button">Edit Course</a>';
} ?>
</div>
<div class="dateDescription"><?php echo $row_displaycourses['description']; ?></div>
<br />
<table width="100%" border="0" cellspacing="0" cellpadding="4">
  <tr style="font-weight:bold; font-size:9px; color:#999;">
    <td width="16%">Start / End Dates</td><td width="14%">Location</td><td width="14%">Availability</td><td width="8%">Tuition</td><td width="14%">Tools</td><td width="8%">Register</td><td width="50%"><?php if (($my->usertype == 'Super Administrator')or($my->usertype == 'Administrator')or($my->usertype == 'Manager')){ ?><a href="index.php?option=com_courses&courseid=<?php echo $row_displaycourses['id']; ?>&catid=<?php echo $_GET['catid'] ?>&Itemid=12&session=New Date Information"><div class="bodyedit" style="float:right; margin-bottom:-5px;">New Date</div></a><?php } ?></td>
  </tr>
  <?php
if (empty($row_displaycoursedates['id'])){ ?>
<tr class="<?php echo $even_odd; ?>">
  <td colspan="7"><img src="administrator/components/com_courses/images/error.png" align="left" style="margin:0px 4px;"> <em>Sorry, there are no upcoming dates scheduled for this course at this time, please try back later.</em></td>
</tr>
<?php
}else{

$even_odd = "";

 

And here is the start of the 2 do{ this is starting the loop for the dates, this is working for only the first coues...

do {
$even_odd = ( 'odd' != $even_odd ) ? 'odd' : 'even';
if ($row_displaycoursedates['course_availability'] == "*FULL*"){$availability_style = 'style="color:#990000; font-weight:bold;"';}
if ($row_displaycoursedates['course_availability'] == "*CANCELLED*"){$availability_style = 'style="color:#990000; font-weight:bold;"';}
if ($row_displaycoursedates['course_availability'] == "Available!"){$availability_style = 'style="color:#669900; font-weight:bold;"';}
?>
  <tr class="<?php echo $even_odd; ?>">
    <td class="date">
  <?php echo date('M d, Y', strtotime($row_displaycoursedates['course_start_date'])); ?><br />
  <?php echo date('M d, Y', strtotime($row_displaycoursedates['course_end_date'])); ?>
    </td>
    <td class="dateLocation" nowrap><?php echo $row_displaycoursedates['course_location']; ?></td>
    <td <?php echo $availability_style; ?> nowrap><?php echo $row_displaycoursedates['course_availability']; ?></td>
    <td class="dateTuition" nowrap>$<?php echo $row_displaycourses['price']; ?></td>
    <td nowrap>
      <?PHP if ($row_displaycoursedates['course_map']){ ?><a href="http://maps.google.com/maps?q=<?php echo $row_displaycoursedates['course_map']; ?>" onMouseover="tooltip('<b>Google Map:</b> Map this location with Google maps.')"; onMouseout="hidetooltip()" target="_blank"><img src="administrator/components/com_courses/images/page_white_world.png" border="0"></a> <?PHP }else{ ?><img src="administrator/components/com_courses/images/page_white_world_off.png" onMouseover="tooltip('<b>Google Map:</b> (Not Available).')"; onMouseout="hidetooltip()"><?PHP } ?>
      <?PHP if ($row_displaycoursedates['course_pdf']){ ?><a href="pdf/<?php echo $row_displaycoursedates['course_pdf']; ?>" onMouseover="tooltip('<b>PDF File:</b> Download a PDF document of this courses information.')"; onMouseout="hidetooltip()" target="_blank"><img src="administrator/components/com_courses/images/page_white_acrobat.png" border="0"></a> <?PHP }else{ ?><img src="administrator/components/com_courses/images/page_white_acrobat_off.png" onMouseover="tooltip('<b>PDF File:</b> (Not Available).')"; onMouseout="hidetooltip()"><?PHP } ?>
      <?PHP if ($row_displaycoursedates['course_other_info']){ ?><img src="administrator/components/com_courses/images/page_white_text.png" onMouseover="tooltip('<b>Notes:</b> <span style=color:#990000;><?php echo $row_displaycoursedates['course_other_info']; ?></span>')"; onMouseout="hidetooltip()"><?PHP }else{ ?><img src="administrator/components/com_courses/images/page_white_text_off.png" onMouseover="tooltip('<b>Notes:</b> (Not Available).')"; onMouseout="hidetooltip()"><?PHP } ?>
    </td>
    <td nowrap><a href="#<?php #echo '?option=com_courses&Itemid=12&catid='.$_GET['catid'].'&courseid='.$row_displaycoursedates['courseid'].'&dateid='.$row_displaycoursedates['id'].'&session=Course%20Registration'; ?>"><div class="register">Register</div></a></td>
    <td nowrap>
      <?php if (($my->usertype == 'Super Administrator') or ($my->usertype == 'Administrator') or ($my->usertype == 'Manager')){ ?>
        <a href="index.php?option=com_courses&catid=<?php echo $row_displaycourses['catid']; ?>&courseid=<?php echo $row_displaycoursedates['courseid']; ?>&id=<?php echo $row_displaycoursedates['id']; ?>&Itemid=12&session=Update%20Date%20Information"><div class="bodyedit">Edit</div></a><a href="index.php?option=com_courses&catid=<?php echo $row_displaycourses['catid']; ?>&id=<?php echo $row_displaycoursedates['id']; ?>&Itemid=12&session=Deleting%20Date"><div class="bodyedit">Delete</div></a>
      <?php } ?>
    </td>
  </tr>
  <?php } while ($row_displaycoursedates = mysql_fetch_assoc($displaycoursedates)); } ?>
</table>
<br />
<?php } while ($row_displaycourses = mysql_fetch_assoc($displaycourses)); ?>

 

Both loops end here.

 

<?php
mysql_free_result($displaycourses);
mysql_free_result($displaycoursedates);
mysql_free_result($listcourses);
?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.