Jump to content

[SOLVED] Getting 1st item from many


phpscott

Recommended Posts

I (100% newbie and creator of ugly code while learning) am trying to output data to a web page that will show me the latest (date) activity for a specific field:

 

The source info would be as follows:

id, date, username, notes

1, 1/1/07, bill, broken screen

2, 1/1/07, mary, format hd

3, 1/2/07, bill, returned

4, 1/3/07, jane, lost document

5, 1/5/07, mary, returned

6. 1/6/07, mary, lost document

 

I want to get the latest activity for each username. I would like the output to be:

3, 1/2/07, bill, returned

4, 1/3/07, jane, lost document

6. 1/6/07, mary, lost document

 

I have seen the distinct option but every example I have found shows it working on just 1 field.

SO, I have tried making an array that would store the usernames as I process them and compare them to the previous ones used, skipping over the output loop if one is found.  The code that I made does not seem to make this happen:

//restrict data

    echo '<hr align="center" width="50%" size="1" noshade><table cellpadding=2 border=1>';

echo " <tr>

    <td>date </td>

    <td>asset</td>

    <td>location type</td>

    <td>techname</td>

    <td>userid</td>

  </tr>";

$check[] = "startdata"; 

    while($rows = mysql_fetch_row($result2)) {

$donotuse="use";

foreach ($check as $value){

if($value == $rows[1]){

$donotuse="donotuse" ;

// echo "$value is $donotuse <br>";

} // end if

} // end foreach

if($donotuse="use")

{

        echo "<tr>";

        echo "<td>" . $rows[0]."</td>";

        echo "<td>" . $rows[1]."</td>";

        echo "<td>" . $rows[2]."</td>";

        echo "<td>" . $rows[3]."</td>";

        echo "<td>" . $rows[4]."</td>";

        echo "</tr>";

$check[] = $rows[1];

} //ending if

$check[] = $rows[1]; // overkill

    } // ending while

    echo "</table>";

 

Link to comment
Share on other sites

The source info would be as follows:

id, date, username, notes

1, 1/1/07, bill, broken screen

2, 1/1/07, mary, format hd

3, 1/2/07, bill, returned

4, 1/3/07, jane, lost document

5, 1/5/07, mary, returned

6. 1/6/07, mary, lost document

 

I want to get the latest activity for each username. I would like the OUTPUT to be:

3, 1/2/07, bill, returned

4, 1/3/07, jane, lost document

6. 1/6/07, mary, lost document

What I feel like I am missing is that select DISTINCT Username only returns 1 column when I am wanting all 3:

--> Date, Username, Notes. 

 

I need to look up TOP to understand it but it seems that would also give me 1 result when in my example I am expecting 3 results.

--> the last ticket for each username

Link to comment
Share on other sites

The source info would be as follows:

id, date, username, notes

1, 1/1/07, bill, broken screen

2, 1/1/07, mary, format hd

3, 1/2/07, bill, returned

4, 1/3/07, jane, lost document

5, 1/5/07, mary, returned

6. 1/6/07, mary, lost document

 

I want to get the latest activity for each username. I would like the OUTPUT to be:

3, 1/2/07, bill, returned

4, 1/3/07, jane, lost document

6. 1/6/07, mary, lost document

What I feel like I am missing is that select DISTINCT Username only returns 1 column when I am wanting all 3:

--> Date, Username, Notes. 

 

I need to look up TOP to understand it but it seems that would also give me 1 result when in my example I am expecting 3 results.

--> the last ticket for each username

 

What is it that you want to order by what, in your database? Explain fully and I will try and help.

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.