A loop and not a loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jamrop
Forum Commoner
Posts: 80
Joined: Fri May 16, 2003 5:38 pm

A loop and not a loop

Post by jamrop »

Hey , i am trying to display some info from my database., I am querying the database from 2 tables. The relationship is member_id.
I want to try display the user details and then the adverts that they have placed. the problem is, i am not sure how to split it up. One record from members table, and 1 or many from the advert table. When i do it , it either just displays the member details and 1 advert, or if i do a while loops, i get a table of member details with ever advert. Hard to explain. Is there a way to split it up?

Code

Code: Select all

<?php

//sql command
$search="select advert.advert_id,advert.member_id,  advert.title, advert.description,


       							advert.condition, advert.price, advert.display,members.member_id, member_type, members.username,
								
								members.password,
	   
	   							members.business_name, members.business_add1, members.business_add2, members.business_town,
	   
	   							members.contact , members.location, members.datejoined, members.lastlog,

								members.email, members.home_tel, members.mobile_tel


	   							from advert, members


	   							where members.member_id = '$member_id'

	   							and members.member_id=advert.member_id"; 
	   
	   							$rs=mysql_query($search,$db)or die("Cannot query the database.<br>" . mysql_error());}


?>

<?php 

//check if there is a record
		  $rowCheck = mysql_num_rows($rs);



			if($rowCheck != 0){ 


		  

		  
// get data into easy format 
		  while ($r = mysql_fetch_array($rs))

				{

					$advert_id=$r["advert_id"];

					$category=$r["category"];

					$type=$r["type"];

					$title=$r["title"];

					$description=$r["description"];

					$condition=$r["condition"];

					$price=$r["price"];

					$display=$r["display"];

					$valid=$r["valid"];

					$date = strtotime($r["date"]); 

    				$date = date("d m  y", $date); 

					$member_id=$r["member_id"];

					$approveuser=$r["username"];
					
					$password=$r["password"];

					$approve_type=$r["member_type"];

					$location=$r["location"];

					$contact=$r["contact"];

					$email=$r["email"];

					$mobile_tel=$r["mobile_tel"];

					$home_tel=$r["home_tel"];

					$datejoined=$r["datejoined"];

					$lastlog= strtotime($r["lastlog"]);
					 				$lastlog = date("d M  y", $lastlog); 
						

//display the records			
					?>
									

 
<p><br><table width="100%" border="1" cellspacing="1" cellpadding="1" bordercolor="#9999FF">
  <tr> 
    <td width="25%" bgcolor="#99CCFF">Member Id</td>
    <td width="25%"><?php echo $member_id ?></td>
    <td width="23%" bgcolor="#99CCFF">Username</td>
    <td width="27%"><?php echo $approveuser ?></td>
  </tr>
  <tr> 
    <td bgcolor="#99CCFF">Member Type</td>
    <td><?php echo $approve_type ?></td>
    <td bgcolor="#99CCFF"></td>
    <td></td>
  </tr>
  <tr bgcolor="#FFFFFF"> 
    <td colspan="4">&nbsp;</td>
  </tr>
  <tr> 
    <td bgcolor="#99CCFF">Contact Name</td>
    <td><?php echo $contact ?></td>
    <td bgcolor="#99CCFF">Location</td>
    <td><?php echo $location ?></td>
  </tr>
  <tr> 
    <td bgcolor="#99CCFF">&nbsp;</td>
    <td>&nbsp;</td>
    <td bgcolor="#99CCFF">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr> 
    <td bgcolor="#99CCFF">Home Tel:</td>
    <td><?php echo $home_tel ?></td>
    <td bgcolor="#99CCFF">Mobile:</td>
    <td><?php echo $mobile_tel ?></td>
  </tr>
  <tr bgcolor="#FFFFFF"> 
    <td colspan="4">&nbsp;</td>
  </tr>
  <tr> 
    <td bgcolor="#99CCFF">Date Joined</td>
    <td><?php echo $datejoined ?></td>
    <td bgcolor="#99CCFF">Last logged in</td>
    <td><?php echo $lastlog ?></td>
  </tr>
  <tr> 
    <td bgcolor="#99CCFF">&nbsp;</td>
    <td colspan="2"><a href="mailto:<?php echo $email; ?>"><?php echo $email ?></td>
    <td>&nbsp;</td>
  </tr>
</table>
</p>
<p>&nbsp;</p>
<p><br>
</p>
<fieldset>
	<legend>
	Member Details:<a href="javascript:PopUp('memdetail.php?member_id=<?php echo $member_id  ?> & username=<?php echo $approveuser ?>')"> 
							<?php echo "$member_id        $approveuser"; ?></a>
	</legend>


<table width="100%" border="0" cellspacing="0" cellpadding="0"  >
  <tr> 
    <td width="10%"  height="64"   align="left" bgcolor=""  > 
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr> 
          <td height="15" bgcolor="#CCCCFF"> <div align="center"><strong>Advert 
              Ref </strong></div></td>
        </tr>
        <tr> 
          <td><div align="center"><font color="#FF0000"><?php echo $advert_id ?></font></div></td>
        </tr>
        <tr> 
          <td bgcolor="#CCCCFF">&nbsp; </td>
        </tr>
      </table></td>
    <td width="4%"  align="left" bgcolor=""  >&nbsp;</td>
    <td width="71%"  align="left" bgcolor=""  >
      <div align="center">
        <p class="box2"><b><?php echo $title ?></b></p>
      </div></td>
    <td width="4%" height="64"  align="left" bgcolor=""  ><b> </b></td>
    <td width="11%" height="64"  align="left" bgcolor="" > 
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td bgcolor="#CCCCFF"><div align="center"><strong>Date</strong></div></td>
        </tr>
        <tr>
          <td>
            <div align="center"><?php echo $date ?></div></td>
        </tr>
        <tr>
          <td bgcolor="#CCCCFF">&nbsp;</td>
        </tr>
      </table></td>
  </tr>
  <tr> 
    <td width="10%" height="163"  align="left" bgcolor=""   > <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr> 
          <td bgcolor="#99CCFF"><div align="center"><strong>Category</strong></div></td>
        </tr>
        <tr> 
          <td><div align="center"><font color="#660033"><?php echo $category ?></font></div></td>
        </tr>
        <tr> 
          <td>&nbsp;</td>
        </tr>
        <tr> 
          <td><div align="center"> <a href="category.php?advert_id=<?php echo $advert_id?>"> 
              <?php echo $type ?></a></div></td>
        </tr>
        <tr> 
          <td bgcolor="#99CCFF">&nbsp;</td>
        </tr>
        <tr> 
          <td>&nbsp;</td>
        </tr>
        <tr> 
          <td bgcolor="#FFCCFF"> <div align="center"><strong>Email</strong></div></td>
        </tr>
        <tr> 
          <td><div align="center"><a href="mailto:<?php echo $email; ?>"><?php echo $email ?> </a></div></td>
        </tr>
        <tr> 
          <td bgcolor="#FFCCFF">&nbsp; </td>
        </tr>
      </table></td>
    <td width="4%"  align="left"    >&nbsp;</td>
    <td width="71%"  align="left"    ><div align="center"><b><?php echo $description ?></b></div></td>
    <td width="4%" height="163"  align="left"    >&nbsp;</td>
    <td width="11%" height="163"  align="left" valign="middle"    > 
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td bgcolor="#99CCFF"><div align="center"><strong>Price</strong></div></td>
        </tr>
        <tr>
          <td><div align="center">£<?php echo $price ?> </div></td>
        </tr>
        <tr>
          <td bgcolor="#99CCFF">&nbsp;</td>
        </tr>
      </table></td>
  </tr>
</table>

</fieldset>


<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr> 
    <td width="1%">&nbsp;</td>
    <td width="33%"><a href="delad.php?advert_id=<?php echo $advert_id  ?> ">
	<img src="../classfied/cancel.gif" width="30" height="23" border="0"></a></td>
    <td width="56%"><a href="update.php?advert_id=<?php echo $advert_id  ?> ">
					<img src="../classfied/pen.gif" width="30" height="23" border="0"></a></td>
    <td width="10%"><a href="apprad.php?advert_id=<?php echo $advert_id  ?> ">
	<img src="../classfied/tick.gif" width="30" height="23" border="0"></a></td>
  </tr>
</table><?php } ?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td class="small"><div align="center">
        <?php




} else { echo "No records "; }

?>
I have cut some html code out, so it does not get confusing.

MAny thanks
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

while(....)
{
if(!isset($done_member))
{
// member legend table here
$done_member = true;
}
// advert tables here
}


either that or split the queries and select the member details first as a single call and then while loop the adverts.
jamrop
Forum Commoner
Posts: 80
Joined: Fri May 16, 2003 5:38 pm

Post by jamrop »

thanks
that worked a treat
Post Reply