SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Apr 2007
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using results in another query before echo

    I'm trying to:
    $query="SELECT fname, lname FROM employees WHERE employee_id=$employee_id";

    echo "fname lname";

    Right now the page displays the employee ID, but I need it to display the employee's name.


    Code:
    $query="SELECT ad_order.company company, job_log.employee_id employee_id, job_log.time time, job_log.action action FROM ad_order LEFT JOIN (job_log) ON (job_log.ad_id=ad_order.id) WHERE ad_order.cust_id=$table_id ORDER BY company,time";
    $result=mysql_query($query);
    while ($row = mysql_fetch_assoc($result))
    
    {
    $company = $row['company'];
    $time = $row['time'];
    $employee_name = $row['employee_id'];
    	if ($employee_name=="")
    	$employee_name = "No Entry";
    	else
    	$employee_name = $row['employee_id'];
    $action = $row['action'];
    	if ($action=="")
    	$action = "No Entry";
    	else
    	$action = $row['action'];
    $showtime = date('m/d/y-h:m:s',$time);
    	if ($showtime=="12/31/69-06:12:00")
    	$showtime = "No Entry";
    	else
    	$showtime = date('m/d/y-h:m:s',$time);
    
    echo "$company - $employee _name - $showtime - $action";
    }

  2. #2
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't just select it in the initial query?

    Code:
    SELECT ad_order.company company
         , employees.employee_id employee_id
         , employees.fname employee_fname
         , employees.lname emplyoee_lname
         , job_log.time time
         , job_log.action action 
      FROM ad_order 
      LEFT 
      JOIN job_log
        ON job_log.ad_id=ad_order.id
      LEFT
      JOIN employees
        ON employees.employee_id = job_log.employee_id
     WHERE ad_order.cust_id=$table_id
     ORDER 
        BY ad_order.company
         , job_log.time
    MySQL v5.1.58
    PHP v5.3.6

  3. #3
    SitePoint Addict
    Join Date
    Apr 2007
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    fname and lname are from a third table and I'm just a noob

    I was able to get it to work with this just before the echo

    [/code]$query2 = "SELECT * FROM employees WHERE employee_id = '$employee_name'";
    * * * *$result2 = mysql_query($query2) or die(mysql_error());
    * * * *while($row2 = mysql_fetch_assoc($result2))
    * * * *{
    * * * * * * * * $employee_name2 = $row2['fname']." ".$row2['lname'];
    * * * *}[/code]

    Thanks everyone!!

  4. #4
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you try my query? If there is only 1 employee name per employee_id (seems logical), you could do it without using the nested queries and improve the code's performance and readability.
    MySQL v5.1.58
    PHP v5.3.6


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •