Jump to content

Inner join 2 tables?


phpstuck

Recommended Posts

The code below returns results when a person loans a book to someone. It provides an array result that shows which person has which books currently signed out.What I am trying to do is bring info from another table into the results.

 

The primary table this works off of returns the name of the borrower and the UPC number of the book they have borrowed as well as the date it was checked out. I have another table named BOOK where the same UPC code is located but it has the title of the book associated with it.

 

In the table named LOANBOOK the UPC is called "loanupc"

In the table named BOOK the UPC is called just "upc"

 

Can I run a query with INNER JOIN that would put the title of the book in the returned results based on matching those UPC codes.

 

Here is the code for what I have which returns only info from the LOANBOOK table.

 

<?PHP

include_once 'db.php';
include_once 'header.html';
include_once 'reportheader.html';

$sql = mysql_query(
"ALTER TABLE loanbook ORDER BY name ASC");
echo "<hr>";
$deflist=mysql_query(
        "SELECT loanupc, name, dateout, datein FROM loanbook WHERE datein IS NULL");
while ($all = mysql_fetch_array($deflist)) {
   $results[$all['name']][] = array ('loanupc' => $all['loanupc'], 'dateout' => $all['dateout'] );
}

//print_r($results);
error_reporting(0);
echo '<center><TABLE id=AutoNumber22 style="BORDER-COLLAPSE: collapse" borderColor=#000000 
      height=12 cellSpacing=3 cellPadding=3 width=600 border=1>
      <TBODY>
      <TR><TD>Books Currently loaned out</TR></TD><TR><TD>';

foreach ($results as $catName => $catData)
{
   print('<center><TABLE id=AutoNumber20 style="BORDER-COLLAPSE: collapse" borderColor=#000000 
      bgcolor=green height=12 cellSpacing=3 cellPadding=3 width=600 border=1>
      <TBODY>
      <TR><TD>
	<b><font face=arial size=2 color=white>'.$catName.'</b><br/></font></td></tr></table></center>'."\n");
   foreach ($catData as $itemNum => $itemData)
   
   {
error_reporting(1);
      // if you want to access the row data in this loop, use the following method:
      print('<center><TABLE id=AutoNumber21 style="BORDER-COLLAPSE: collapse" borderColor=#000000 
      height=12 cellSpacing=3 cellPadding=3 width=600 border=0>
      <TBODY>
      <TR><TD><font face=arial size=2>
<A HREF="editproduct.php?upc='.$itemData['loanupc'].'">edit</A> - ' .
      $itemData['loanupc'].', '.$itemData['dateout'].'<br/></td></tr></table></center></font>'."\n"); 
      // etc. (you must code the field names in hard coded this way)
      //foreach ($itemData as $fieldName => $value)
      
   }
}
echo '</td></tr></table></center>';

include_once 'footer.html';

?>

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.