Welcome Guest, Not a member yet? Register   Sign In
getting data from two different tables problem
#1

[eluser]haris244808[/eluser]
Ok firstly i have 2 tables

i insert a data to table 'files' from the input->post, except that in came from i insert the user_id from the session (which i setted when the user logged in)....

now i want to show all the 'files' data together with the 'users' first_name and last_name but depending from the came_from field....


#2

[eluser]TheFuzzy0ne[/eluser]
I'm sorry, but I'm confused... What is the 'came_from' field for, and what is the 'user_id' field for? Aren't they the same thing?

What should the came_from() method do that the show_files() method doesn't do?

I'm unclear on what's not working as expected. Please could you clarify?
#3

[eluser]haris244808[/eluser]
[quote author="TheFuzzy0ne" date="1364122520"]I'm sorry, but I'm confused... What is the 'came_from' field for, and what is the 'user_id' field for? Aren't they the same thing?

What should the came_from() method do that the show_files() method doesn't do?

I'm unclear on what's not working as expected. Please could you clarify?[/quote]

user_id holds the id of the user(the user id of the one to whom you sent, so later i can show the files depending on the user login) when he creates a file...
but this file can be forwarded to someone else later...so when it is forwarded it needs to hold the user id and update the came_from only (with the user_id of the one who forwarded that file)

so came_from will hold the id of the user who send the file and later if the file is forwarded will be updated to that users id (i store the id from the sesssion of the current user )

but the user_id will not change...it will be created when you create a file....(i store the id from the dropdown menu, where you can choose from a registered users)


and finaly with show_files i want to show all the file data BUT TOGETHER WITH THE first_name AND last_name OF THE USER WHERE came_from = users.id


#4

[eluser]TheFuzzy0ne[/eluser]
Thanks for the explanation. i now understand what you're doing much more clearly. However, I have one more question. What result are you getting from the query you're executing? I've looked at it briefly, and I can't see why it should work.

Also, I would suggest that you go with more transparent field names. Why not use uploader_id and recipient_id? They pretty much speak for themselves.

The same goes for your method names. Maybe something like get_received_files() and get_uploaded_files() might be a bit more clear?

get_received_files() would be files that have been sent to the specified user, and get_uploaded_files() would be for files that have been uploaded by a user. Just a suggestion... Smile

Also, if you're expecting more than a single result, you should return result() not row().
#5

[eluser]TheFuzzy0ne[/eluser]
Sorry, it's a Sunday... My brain just clicked. Is this what you're after?
Code:
function came_from($user_id=0)
{
    $q = $this->db->query('SELECT
            r.first_name AS recipient_first_name,
            r.last_name AS recipient_last_name,
            s.first_name AS sender_first_name,
            s.last_name AS sender_last_name
        FROM files
        INNER JOIN users AS s ON files.came_from = s.id
        INNER JOIN users AS r ON files.user_id = r.id
        WHERE came_from = ?', array($user_id));

    if ($q->num_rows > 0) {
        return $q->result();
    }
    
    return false;
}
#6

[eluser]haris244808[/eluser]
[quote author="TheFuzzy0ne" date="1364130605"]Sorry, it's a Sunday... My brain just clicked. Is this what you're after?
[code]

firstly thnx for helping me in naming methods and fields...
#7

[eluser]TheFuzzy0ne[/eluser]
Can you give me an example of the table you want to output? If you can provide the column names and a few rows of data, I'll have a better idea of how you're trying to display your data.
#8

[eluser]haris244808[/eluser]
[quote author="TheFuzzy0ne" date="1364136815"]Can you give me an example of the table you want to output? If you can provide the column names and a few rows of data, I'll have a better idea of how you're trying to display your data.[/quote]

ok
#9

[eluser]TheFuzzy0ne[/eluser]
It will need some modification, but this should get you started.

Note: We're only using one loop.
Code:
<table>
    <thead>
        <tr>
            <td>
                Subject
            </td>
            <td>
                Description
            </td>
            <td>
                Created By
            </td>
            <td>
                Came From
            </td>
        </tr>
    <thead>
    <tbody>&lt;?php foreach ($files_query as $entry): ?&gt;

        <tr>
            <td>
                &lt;?php echo htmlentities($entry->subject); ?&gt;
            </td>
            <td>
                &lt;?php echo htmlentities($entry->description); ?&gt;
            </td>
            <td>
                &lt;?php echo htmlentities($entry->uploader_first_name.' '.$entry->uploader_last_name; ?&gt;
            </td>
            <td>
                &lt;?php echo htmlentities($entry->recipient_first_name.' '.$entry->recipient_last_name; ?&gt;
            </td>
        </tr>&lt;?php endforeach; ?&gt;

    </tbody>
</table>

I've picked different field names, so it's up to you to name them correctly.
#10

[eluser]haris244808[/eluser]
[quote author="TheFuzzy0ne" date="1364141470"]It will need some modification, but this should get you started.

Note: We're only using one loop.
Code:
<table>
    <thead>
        <tr>
            <td>
                Subject
            </td>
            <td>
                Description
            </td>
            <td>
                Created By
            </td>
            <td>
                Came From
            </td>
        </tr>
    <thead>
    <tbody>&lt;?php foreach ($files_query as $entry): ?&gt;

        <tr>
            <td>
                &lt;?php echo htmlentities($entry->subject); ?&gt;
            </td>
            <td>
                &lt;?php echo htmlentities($entry->description); ?&gt;
            </td>
            <td>
                &lt;?php echo htmlentities($entry->uploader_first_name.' '.$entry->uploader_last_name; ?&gt;
            </td>
            <td>
                &lt;?php echo htmlentities($entry->recipient_first_name.' '.$entry->recipient_last_name; ?&gt;
            </td>
        </tr>&lt;?php endforeach; ?&gt;

    </tbody>
</table>

I've picked different field names, so it's up to you to name them correctly.[/quote]

but files_query contains only the data from the 'files' table :

i need also full name of creator and sender to show to the same field Sadall data together in the table)




Theme © iAndrew 2016 - Forum software by © MyBB