Jump to content

Recommended Posts

I am displaying some records using a JOIN, the following Select statement works like a charm:

 

$result = "SELECT regjobs.jobtitle, applyjob.empnum, applyjob.startdate, applyjob.username, regjobs.jobid, regjobs.district, regjobs.timetype	 ". 
"FROM applyjob, regjobs ".
"WHERE (regjobs.jobid = applyjob.jobtitle)";

 

Ideally I want the client to be able to choose how to Order the results... but none of the following attempts work at all!

 

$result = "SELECT regjobs.jobtitle, applyjob.empnum, applyjob.startdate, applyjob.username, regjobs.jobid, regjobs.district, regjobs.timetype, regjobs.id	 ". 
"FROM applyjob, regjobs ".
"WHERE (regjobs.jobid = applyjob.jobtitle) ".
[color=orange]//"ORDER BY " .$cat.);[/color]
[color=orange]//"ORDER BY timetype");[/color]
"ORDER BY regjobs.timetype");

 

Any ideas how I can dynamically Order this Select?

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/87790-php-select-with-join-order-by-problem/
Share on other sites

Sure, there will be a line about the query results like

 

Sort By Order | Job Number | Job Title | Start Date | Username | District | Work Load

 

<p>Sort By                                                       
<a href="jobstatus2.php?cat=id">Order</a> | 
<a href="jobstatus2.php?cat=jobid">Job Number</a> | 
<a href="jobstatus2.php?cat=jobtitle">Job Title</a> | 
<a href="jobstatus2.php?cat=startdate">Start Date</a> | 
<a href="jobstatus2.php?cat=username">Username</a> | 
<a href="jobstatus2.php?cat=district">District</a> | 
<a href="jobstatus2.php?cat=timetype">Work Load</a></p>

 

 

But these don't work. I wanted to see if I could make it work by hardcoding the column name like applyjob.timetype (aka Work Load), but even that simple thing doesn't work.

 

I have also tried ASC and DESC, but they make no difference.

 

The screen just renders as white - no code in it.

 

:( Poor me.

 

What happens if they don't hit anything??

 

<?php

if(isset($_GET['cat'])){
$cat = $_GET['cat'];
} else {
$cat = "timetype";
}

$sql = "SELECT regjobs.jobtitle, applyjob.empnum, applyjob.startdate, applyjob.username, regjobs.jobid, regjobs.district, regjobs.timetype
          FROM applyjob JOIN regjobs ON
          applyjob.jobtitle = regjobs.jobid
          ORDER BY $cat";
?>

 

Ray

He's is using php to create his sql statement. Those are not php functions. you don't have to keep your sql statement all on the same line. As long as you don't close the double quotes, php ignores the whitespaces and line breaks. Just formatted like that for organization. Doesn't it look much better!?!?! :)

 

 

Craygo, that is some great code, I put it in and the page at least renders now... I do get an error:

 

You have an error in your SQL syntax near 'ON applyjob.jobtitle = regjobs.jobid ORDER BY id' at line 1

 

I hope to figure it out and will post the fix when I find it...

 

I have never had so much help on any forum before!

Brilliant, I got it working... the thing missing was INNER JOIN. Thanks all,

 

$result = "SELECT applyjob.jobtitle, regjobs.jobtitle, applyjob.empnum, applyjob.startdate, applyjob.username, regjobs.jobid, regjobs.district, regjobs.timetype, regjobs.id 
FROM applyjob INNER JOIN regjobs 
ON applyjob.jobtitle = regjobs.jobid 
ORDER BY $cat";

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.