Jump to content

Remove Redundant Iteration Of Entries In "for" Loop


offmike1776

Recommended Posts

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
Share on other sites

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>"
}
?>

Edited by Barand
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.