djs1 Posted October 21, 2009 Share Posted October 21, 2009 I'm still very new to PHP, and running in to a problem when trying to execute a do/while loop inside another one. What I'm trying to achieve is that I want to display all of the records in one table that match my SQL Query defined as 'Recordset1'. And, while that is happening, I want to cross-reference my records in a 2nd table, and if the description in Table 2 Matches the Description in Table 1, print a results message right next to the description from 'Recordset1'. My problem isn't setting up the SQL queries - I'm currently able to get the correct results returned to me, as long as I print the results in a separate table... my problem is getting the results message to print RIGHT NEXT TO the results of 'Recordset1', because the 'Recordset1' results are inside of do/while loop. There is probably a very simple solution to this, and there's a good possibility that I'm approaching this the wrong way... I just can't wrap my head around it yet... If anyone has any input, it would be very appreciated! Thanks! -d. <?php do { if(isset($row_Recordset1['description'])) { ?> <tr> <td> <?php echo $row_Recordset1['description']?> <?php do{ $R1 = $row_Recordset1['description']; $R2 = $row_Recordset2['description']; if ($R1== $R2) { print("It's a Match<br />"); } else print("No Match<br />") }while ----????????????? ---- ?> </td> </tr> <?php }else break; } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?> Link to comment https://forums.phpfreaks.com/topic/178531-multiple-do-while-statements/ Share on other sites More sharing options...
kickstart Posted October 21, 2009 Share Posted October 21, 2009 Hi Think we need to see the queries and what you expect to be put out. Normally you can combine queries like that into a single query that will be more efficient. All the best Keith Link to comment https://forums.phpfreaks.com/topic/178531-multiple-do-while-statements/#findComment-941577 Share on other sites More sharing options...
garethhall Posted October 21, 2009 Share Posted October 21, 2009 You will need to set the second query with in first loop. Say I want to list the team with a member Sample <?php $sql = "SELECT * FROM members"; $rs = mysql_query($sql, $con); while($rw = mysql_fetch_assoc($rs)){ echo $rw['MemberName']; $sqlT = "SELECT * FROM team WHERE teamID = ".$rw['memberTeamID']; $rsT = mysql_query($sqlT,$con); while($rwT = mysql_fetch_assoc($rsT)){ echo "Part of team: ".$rwT['teamName']."<br/>"; } mysql_free_result($rsT) } mysql_free_result($rs); ?> Link to comment https://forums.phpfreaks.com/topic/178531-multiple-do-while-statements/#findComment-941589 Share on other sites More sharing options...
djs1 Posted October 22, 2009 Author Share Posted October 22, 2009 Thank you both for getting back to me. Here's my current code and more details on what I'm trying to accomplish: <?php session_start(); $user = $_SESSION['_amember_user']; $userlogin = $user[login]; $title = $_GET['title']; ?> <?php require_once('../../Connections/xxxxxx.php'); ?> <?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; } } // GET Video Info $colname_Recordset1 = "-1"; if (isset($_GET['location'])) { $colname_Recordset1 = $_GET['location']; } mysql_select_db($database_dsxpro, $dsxpro); $query_Recordset1 = sprintf("SELECT * FROM tutorials WHERE location = %s ORDER BY type, description ASC", GetSQLValueString($colname_Recordset1, "text")); $Recordset1 = mysql_query($query_Recordset1, $dsxpro) or die(mysql_error()); $row_Recordset1 = mysql_fetch_assoc($Recordset1); $totalRows_Recordset1 = mysql_num_rows($Recordset1); // GET PDF Files mysql_select_db($database_dsxpro, $dsxpro); $query_Recordset2 = sprintf("SELECT * FROM pdf WHERE video_id = ".$row_Recordset1['filename'].""; $Recordset2 = mysql_query($query_Recordset2, $dsxpro) or die(mysql_error()); $row_Recordset2 = mysql_fetch_assoc($Recordset2); $totalRows_Recordset2 = mysql_num_rows($Recordset2); // GET Favorites determined by Session Username $colname_Favorites = "-1"; if (isset($user)) { $colname_Favorites = $userlogin; } mysql_select_db($database_dsxpro, $dsxpro); $query_Favorites = sprintf("SELECT * FROM favorites WHERE username = %s", GetSQLValueString($colname_Favorites, "text")); $Favorites = mysql_query($query_Favorites, $dsxpro) or die(mysql_error()); $row_Favorites = mysql_fetch_assoc($Favorites); $totalRows_Favorites = mysql_num_rows($Favorites); //Clean up Description $searchArray = array("SA","UF", "PP", "TS", "FX", "FAX", "HD"); //REPLACE WITH $replaceArray = array(" "," ", " ", " ", " ", " ", " "); //Toggle Table Row Color $toggle=0; $tdtoggle = $toggle?'odd':'even'; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title><?php echo $title;?></title> <style type="text/css"> <!-- Body { Background: transparent; } --> </style> <link href="css/mem.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="mainpage"> <div id="mainpage_content"> <h1><?php echo $title;?></h1> <?php echo $about; //Open table print("<br /><table width='515' border='0' cellpadding='0' cellspacing='0'> <tr width='515'><td> title placeholder </td></tr>"); // Begin Repeating Recordset do { if(isset($row_Recordset1['filename'])) { ?> <tr class="<?php echo $toggle?'odd':'even';$toggle = !$toggle; ?>"> <td colspan="4" width="515"> <!-- Video Type / Description [Populating from RECORDSET 1] --> <div id="videolisting"> <div id="vidinfo"> <a class="<?php echo $row_Recordset1['type']; ?>" href="videoplayer/videoplayer.php?filename=<?php echo $row_Recordset1['filename']; ?>&description=<?php echo $row_Recordset1['description']; ?>&type=<?php echo $row_Recordset1['type']; ?>&location=<?php echo $row_Recordset1['location']; ?>" target="_blank"><?php print str_replace($searchArray, $replaceArray, $row_Recordset1['description']); ?></a> </div> <!-- If User has Already Added this Video to their Favorites, Update Icon Here [Populating from FAVORITES] --> <!-- If Tutorial has an Accompanying PDF, Link Here [Populating from RECORDSET2] --> So up to this point the "Recordset1" Query is pulling Video titles from the Tutorials Database ... The 'Recordset2' Query is selecting PDF files whose ID matches the video name from 'Recorcordset1' ... And the "Favorites" Query is selecting only video titles that match the username of the logged in user. What I'm trying to add next is here within the "Recordset1" Repeating Recordset Loop: <div id="pdf"> If a PDF exists, print "something" else "print something else" </div> <div id="fav"> If a video title from the "Favorites" Query matches a video title from the "Recordset1" Query, print "something", else print "something else". </div> </div> </td> </tr> <?php }else break; } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?> That's where I'm getting lost. Any suggestions? Thanks!! Link to comment https://forums.phpfreaks.com/topic/178531-multiple-do-while-statements/#findComment-941927 Share on other sites More sharing options...
kickstart Posted October 22, 2009 Share Posted October 22, 2009 Hi I would revise you SQL to something like this:- $query_Recordset1 = sprintf("SELECT * FROM tutorials a LEFT OUTER JOIN pdf b ON a.filename = b.video_id LEFT OUTER JOIN favourites c ON filename = c.filename AND c.username = $colname_Favorites WHERE location = %s ORDER BY type, description ASC", GetSQLValueString($colname_Recordset1, "text")); Then you can just loop all round one main loop (this assumes that filename is unique and there will not be mulitple favourites for the same user and filename). Something like this (made up column names, hopefully gives you the idea):- $prevVideo = ''; while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)) { if ($prevVideo != $row_Recordset1['filename'] { if ($prevVideo != '') echo '</tr>'; ?> <tr class="<?php echo $toggle?'odd':'even';$toggle = !$toggle; ?>"> <td colspan="4" width="515"> <a class="<?php echo $row_Recordset1['type']; ?>" href="videoplayer/videoplayer.php?filename=<?php echo $row_Recordset1['filename']; ?>&description=<?php echo $row_Recordset1['description']; ?>&type=<?php echo $row_Recordset1['type']; ?>&location=<?php echo $row_Recordset1['location']; ?>" target="_blank"><?php print str_replace($searchArray, $replaceArray, $row_Recordset1['description']); ?></a> </td> <?php } else { echo '<td></td>'; } echo '<td>'.$row_Recordset1['pdfDocDetails'].'</td><td>'.$row_Recordset1['MarkerForFavourite'].'</td>'; $prevVideo = $row_Recordset1['filename']; } All the best Keith Link to comment https://forums.phpfreaks.com/topic/178531-multiple-do-while-statements/#findComment-941988 Share on other sites More sharing options...
djs1 Posted October 22, 2009 Author Share Posted October 22, 2009 Great idea! I updated my SQL query, based on your suggestion, and I'm now getting the correct data populating the table... however, this part of the code: echo '<td>'.$row_Recordset1['pdfDocDetails'].'</td><td>'.$row_Recordset1['MarkerForFavourite'].'</td>'; is populating 2 times for each row. any idea why? Thanks! Link to comment https://forums.phpfreaks.com/topic/178531-multiple-do-while-statements/#findComment-942116 Share on other sites More sharing options...
kickstart Posted October 22, 2009 Share Posted October 22, 2009 Hi Couple of typos in the code I wrote, but nothing in the logic I can see that would cause that. Can you post the code you are now using? All the best Keith Link to comment https://forums.phpfreaks.com/topic/178531-multiple-do-while-statements/#findComment-942217 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.