Printing from two table within a loop

Posted on 2006-04-02
Last Modified: 2006-11-18
Help please .....

I am printing data using a loop form one table ... this wirks fine .. now I need to print data based on the record I'm printing at the time .. my code pulls data from the second table but it pulls the same data for every record ...  I'm sure I need a loop within the loop but I can't get it to work .. HELLLLLLLLLLLLP !!!

here is my existing code

$query = "SELECT * FROM horses WHERE status = 'accepted' ORDER BY lot_num";
$results = mysql_query($query)
or die(mysql_error());

<table cellpadding="10" cellspacing="0" border="0">
<tr><td><b>Lot</b></td><td><b>Horse</b></td><td><b>Bid</b></td><td><b>Status</b></td><td><b>Closing Time</b></td></tr>
while ($row = mysql_fetch_array($results)) {
echo "<tr>\n";
echo "<td valign=top rowspan=2>" . $row['lot_num'] . "</td>\n";
echo "<td valign=top rowspan=2><a href='details.php?horse_name=" . $row['horse_name'] . "'>" . $row['horse_name'] . "<br>\n";
echo "<img src='foalpics/" .$row['main_pic']. "' width='75px' alt='" . $row['main_pic'] . "'></td>\n";
echo "<td valign=top height=15>$" . $row['bid'] . "</td>\n";
echo "<td valign=top height=15>" . $row['bidding_status'] . "</td>\n";
echo "<td valign=top height=15>". $row['closing_date'] . " " . $row['closing_time'] . "</td>\n";
echo "</tr>\n";

$pedquery = "SELECT * FROM pedigree, horses WHERE pedigree.horseID = '" . $_SESSION['horseID'] . "' AND horses.horseID = '" . $_SESSION['horseID'] . "'";
$pedresults = mysql_query($pedquery)
     or die(mysql_error());
$ped = mysql_fetch_array($pedresults);

echo "<tr><td colspan=3 height=55>" . $ped['S_Name'] . " X "  . $ped['DS_Name'].  " X " . $ped['DDS_Name'] . " </td></tr>\n";

Question by:koochy
    LVL 17

    Accepted Solution

    I think I see it now.
    In each loop, your going through and performing the $pedquery.  The same query every single time because your taking the information from the $_SESSION every time.  Did you want the 2nd query to base its information off of the first query?  Your gonna need to change that 2nd query if you want it to be different each time.

    Joe P

    Author Comment

    I need to pick the pedigree information based on each horse so the horse id from the first query.

    Author Comment

    Ah ha ... you pointed me directly where I needed to go ... all I had to do was take out the session ans replace it with the row variable ... thank you thankyou ..I was smacking my head and just couldn't see it ...
    LVL 17

    Expert Comment

    No problem =)
    Your code looked so clean and efficient that I thought it would be something simple like that.
    I like to see clean code like you have there, keep it up.

    Joe P

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to dynamically set the form action using jQuery.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now