offmike1776 Posted December 11, 2012 Share Posted December 11, 2012 Hello: I have been working on this code for some time. It is for a directory database for which I am retrieving information from two tables. From one table I am retrieving one entry while multiple entries are retrieved from another table. The problem is that I only the one entry to display only once followed by the display of the multiple entries following it. The way I have it set up now the entry I only want to appear at the top of the disp;ay repeats itself for every record it is associated with. Here is my code: { $library_search=$_POST ['Library_search']; $query= "select * from library RIGHT JOIN name USING (Library_ID) WHERE library_name like '%".$library_search."%'"; $query_results=mysql_query($query)or die (mysql_error()); $match_results=mysql_num_rows($query_results); } for ($i=0; $i < $match_results; $i++) { echo '<tr><td>'; $row=mysql_fetch_array($query_results); echo ($row ['university_name']),'<br/>'; echo ($row ['library_name']),'<br/>'; echo ($row ['address']),'<br/>'; echo ($row ['city']),'<br/>'; echo ($row ['state']),'<br/>'; echo ($row ['zip']),'<br/>'; echo ($row ['fax']),'<br/>'; echo ($row ['home_URL']),'<br/>'; echo ($row ['catalog_URL']),'<br/>'; echo ($row ['hours_URL']),'<br/>'; echo ($row ['special_access']),'<br/>'; echo ($row ['FirstName']), " "; echo ($row ['Middleinit'])," "; echo ($row ['LastName']),'<br/>' ; echo ($row ['PositionHeld']),'<br/>' ; echo ($row['OtherPosition']),'<br/>' ; echo ($row['Library_ID']),'<br/>' ; echo ($row['Address']), '<br/>'; echo ($row['City']), " "; echo ($row['State'])," ", " "; echo ($row['zip']),'<br/>'; echo ($row['Workphone']),'<br/>'; echo ($row['Residencephone']), '<br/>'; echo ($row['Email']),'<br/>'; echo ($row['WAALMember']), '<br/><br/>'; } Link to comment https://forums.phpfreaks.com/topic/271870-remove-redundant-iteration-of-entries-in-for-loop/ Share on other sites More sharing options...
Barand Posted December 11, 2012 Share Posted December 11, 2012 Check for a change of name and only output when it changes <?php $query_results=mysql_query($query)or die (mysql_error()); $prev = ''; while ($row=mysql_fetch_array($query_results)) { if ($prev != $row ['library_name']) { // new name ? echo "<tr><th>{$row['library_name']}</th></tr?"; // if yes, output new name $prev = $row['library_name']; // store new name } echo '<tr><td>'; echo ($row ['university_name']),'<br/>'; echo ($row ['address']),'<br/>'; echo ($row ['city']),'<br/>'; echo ($row ['state']),'<br/>'; echo ($row ['zip']),'<br/>'; echo ($row ['fax']),'<br/>'; echo ($row ['home_URL']),'<br/>'; echo ($row ['catalog_URL']),'<br/>'; echo ($row ['hours_URL']),'<br/>'; echo ($row ['special_access']),'<br/>'; echo ($row ['FirstName']), " "; echo ($row ['Middleinit'])," "; echo ($row ['LastName']),'<br/>' ; echo ($row ['PositionHeld']),'<br/>' ; echo ($row['OtherPosition']),'<br/>' ; echo ($row['Library_ID']),'<br/>' ; echo ($row['Address']), '<br/>'; echo ($row['City']), " "; echo ($row['State'])," ", " "; echo ($row['zip']),'<br/>'; echo ($row['Workphone']),'<br/>'; echo ($row['Residencephone']), '<br/>'; echo ($row['Email']),'<br/>'; echo ($row['WAALMember']), '<br/><br/>'; echo "</td></tr>" } ?> Link to comment https://forums.phpfreaks.com/topic/271870-remove-redundant-iteration-of-entries-in-for-loop/#findComment-1398768 Share on other sites More sharing options...
offmike1776 Posted December 12, 2012 Author Share Posted December 12, 2012 This looks like a good solution for this issue. I inserted it exactly as it is written but am getting a parse error at the very end of the code. I'll work on it. Link to comment https://forums.phpfreaks.com/topic/271870-remove-redundant-iteration-of-entries-in-for-loop/#findComment-1398919 Share on other sites More sharing options...
offmike1776 Posted December 12, 2012 Author Share Posted December 12, 2012 I was missing a closing bracket--this will get me on my way i believe. Thank you very much Barand. Link to comment https://forums.phpfreaks.com/topic/271870-remove-redundant-iteration-of-entries-in-for-loop/#findComment-1398922 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.