alstonan25
alstonan25

Reputation: 23

Fetch data from different tables using while loop and if statement

I am attempting to fetch data from my database using 8 different tables named:

inventory descriptorid typeid conditionid statusid locationid stationid users

I believe my query is working correctly the problem is using my $row variable. I am only getting the results from data in the inventory table. Can Someone explain how I can fetch my data from each table?

The code is below:

<?php
    // get the records from the database
    if ($result = $con->query("
    SELECT inventory.InventoryID, descriptorid.dename, typeid.tyname, 
           inventory.Serial, inventory.ServiceTag, inventory.CityTag, 
           conditioncid.conname, statusid.statusname, locationid.locname, 
           stationid.stationname, users.Fname, users.Lname, 
           inventory.PurchaseDate, inventory.POnumber 
    FROM inventory 
    LEFT JOIN descriptorid 
      ON inventory.Descriptor = descriptorid.dename 
    LEFT JOIN typeid 
      ON inventory.Type = typeid.tyname 
    LEFT JOIN conditioncid 
      ON inventory.ConditionC = conditioncid.conname 
    LEFT JOIN statusid 
      ON inventory.Status = statusid.statusname 
    LEFT JOIN locationid 
      ON inventory.Location = locationid.locname 
    LEFT JOIN stationid 
      ON inventory.Station = stationid.stationname 
    LEFT JOIN users 
      ON inventory.cuserFname = users.Fname 
     AND inventory.cuserLname = users.Lname "))
    {
    // display records if there are records to display
        if ($result->num_rows > 0)
        {
            // display records in a table
            echo "<table border id='myTable' class='tablesorter' >";

            // set table headers
            echo "<thead><th>ID</th><th>Descriptor</th><th>Type</th><th>Serial</th><th>ServiceTag</th><th>CityTag</th><th>Condition</th><th>Status</th><th>Location</th><th>Station</th><th>CurrentUser</th><th>Lastname</th><th>PurchaseDate</th><th>POnumber</th><th></th><th></th></thead>";


            echo "<tbody";

            while ($row = $result->fetch_object())
            {
                // dump whats returned
                // print_r($row);

                echo "<tr>";
                echo "<td>" . $row->InventoryID . "</td>";
                echo "<td>" . $row->Descriptor . "</td>";
                echo "<td>" . $row->Type . "</td>";
                echo "<td>" . $row->Serial . "</td>";
                echo "<td>" . $row->ServiceTag . "</td>";
                echo "<td>" . $row->CityTag . "</td>";
                echo "<td>" . $row->ConditionC . "</td>";
                echo "<td>" . $row->Status . "</td>";
                echo "<td>" . $row->Location . "</td>";
                echo "<td>" . $row->Station . "</td>";
                echo "<td>" . $row->cUserFname . "</td>";
                echo "<td>" . $row->cUserLname . "</td>";
                echo "<td>" . $row->PurchaseDate . "</td>";
                echo "<td>" . $row->POnumber . "</td>";
                echo "<td><a href='invrecords.php?InventoryID=" . $row->InventoryID . "'><img src='img/default/button_mini_ticket_edit.gif'></a></td>";
                echo '<td><a href="javascript:void(0);" onclick="confirmation(' . $row->InventoryID . ');"><img src="img/default/button_mini_delete.gif"></a>';
                echo "</tr>";

            }
            echo "</tbody>";
            echo "</table>";

Upvotes: 0

Views: 1569

Answers (3)

Drudge
Drudge

Reputation: 528

Are you sure that you want use LEFT JOIN instead of INNER JOIN? That means that if you have table A and B you only take elements which are located in A but not necessarily in B. That means if there is not a matching partner for an element of the left table, you would have a row containg an element of table A in one column and null in the other column. This might be one reason for having a resultset only containing an element of the inventory table. As far as I understand you want a result containing the elements joined on equal attributes, so try to replace LEFT JOIN with INNER JOIN.

SQL Joins

Image source: http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins

Upvotes: 1

alstonan25
alstonan25

Reputation: 23

Ok so the issue was instead of matching the third option (dename, tyname,etc.) on my secondary tables with the inventory I should've been matching it with the ID of that said table. Now when I query the third option with my row variable I can select any option from my secondary table (deid,tyid). It now outputs the desired results and hopefully I can create a drop down list using a similar query.Thanks for all the ideas.

Upvotes: 0

hellcode
hellcode

Reputation: 2698

You should name your row vars as you select them (as you did it with the columns of the inventory table):

Wrong:

        echo "<td>" . $row->Descriptor . "</td>";
        echo "<td>" . $row->Type . "</td>";
        echo "<td>" . $row->ConditionC . "</td>";
        echo "<td>" . $row->Status . "</td>";
        echo "<td>" . $row->Location . "</td>";
        echo "<td>" . $row->Station . "</td>";
        echo "<td>" . $row->cUserFname . "</td>";
        echo "<td>" . $row->cUserLname . "</td>";

Use instead:

        echo "<td>" . $row->dename . "</td>";
        echo "<td>" . $row->tyname. "</td>";
        echo "<td>" . $row->conname. "</td>";
        echo "<td>" . $row->statusname. "</td>";
        echo "<td>" . $row->locname. "</td>";
        echo "<td>" . $row->stationname. "</td>";
        echo "<td>" . $row->Fname. "</td>";
        echo "<td>" . $row->Lname. "</td>";

Upvotes: 1

Related Questions