|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
I have four fields, "name", "date", "task", and "title" - and am
trying to PHP code where if I click 'All', all rows for 'Bob' followed by all rows in a MySQL datebase for 'Jim' and then all rows for 'Joe' would be displayed alphabetically (by name), with all of Bob's rows grouped together and then all of Jim's and then all of Joe's, etc (versus the way they may reside in the MySQL table). My query works well for individual names and also for 'All' but it would look and be easier to read if I could group the rows by name and maybe list alphabetically. My 'All' code portion currently looks like this: $query = "SELECT * FROM $table WHERE 1 = 1 "; if($name != "All") $query .= "and name = '".$name."'"; $result = mysql_query($query); thanks for any ... John |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
jcage@lycos.com wrote:
> I have four fields, "name", "date", "task", and "title" - and am > trying to PHP code where if I click 'All', all rows for 'Bob' followed > by all rows in a MySQL datebase for 'Jim' and then all rows for 'Joe' > would be displayed alphabetically (by name), with all of Bob's rows > grouped together and then all of Jim's and then all of Joe's, etc > (versus the way they may reside in the MySQL table). > > My query works well for individual names and also for 'All' but it > would look and be easier to read if I could group the rows by name and > maybe list alphabetically. My 'All' code portion currently looks like > this: > > $query = > "SELECT * > FROM $table > WHERE 1 = 1 "; > if($name != "All") $query .= "and name = '".$name."'"; > > $result = mysql_query($query); > > > thanks for any ... > John > > You're asking about SQL, not PHP. Try comp.databases.mysql. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
jcage@lycos.com wrote:
> I have four fields, "name", "date", "task", and "title" - and am > trying to PHP code where if I click 'All', all rows for 'Bob' followed > by all rows in a MySQL datebase for 'Jim' and then all rows for 'Joe' > would be displayed alphabetically (by name), with all of Bob's rows > grouped together and then all of Jim's and then all of Joe's, etc > (versus the way they may reside in the MySQL table). > > My query works well for individual names and also for 'All' but it > would look and be easier to read if I could group the rows by name and > maybe list alphabetically. My 'All' code portion currently looks like > this: > > $query = > "SELECT * > FROM $table > WHERE 1 = 1 "; > if($name != "All") $query .= "and name = '".$name."'"; > > $result = mysql_query($query); > > > thanks for any ... > John > You're probably going to want to use one of the "joins". 'LEFT JOIN', 'INNER JOIN', etc. Then 'ORDER BY name DESC'. This will give you your results in alphabetical order. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Nov 2, 8:50 pm, Chris Gorospe <ch...@ekast.com> wrote:
> jc...@lycos.com wrote: > > I have four fields, "name", "date", "task", and "title" - and am > > trying to PHP code where if I click 'All', all rows for 'Bob' followed > > by all rows in a MySQL datebase for 'Jim' and then all rows for 'Joe' > > would be displayed alphabetically (by name), with all of Bob's rows > > grouped together and then all of Jim's and then all of Joe's, etc > > (versus the way they may reside in the MySQL table). > > > My query works well for individual names and also for 'All' but it > > would look and be easier to read if I could group the rows by name and > > maybe list alphabetically. My 'All' code portion currently looks like > > this: > > > $query = > > "SELECT * > > FROM $table > > WHERE 1 = 1 "; > > if($name != "All") $query .= "and name = '".$name."'"; > > > $result = mysql_query($query); > > > thanks for any ... > > John > > You're probably going to want to use one of the "joins". 'LEFT JOIN', > 'INNER JOIN', etc. Then 'ORDER BY name DESC'. This will give you your > results in alphabetical order. Any examples on how to integrate 'join' code for something like this in PHP? Thanks |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
jcage@lycos.com wrote:
> On Nov 2, 8:50 pm, Chris Gorospe <ch...@ekast.com> wrote: >> jc...@lycos.com wrote: >>> I have four fields, "name", "date", "task", and "title" - and am >>> trying to PHP code where if I click 'All', all rows for 'Bob' followed >>> by all rows in a MySQL datebase for 'Jim' and then all rows for 'Joe' >>> would be displayed alphabetically (by name), with all of Bob's rows >>> grouped together and then all of Jim's and then all of Joe's, etc >>> (versus the way they may reside in the MySQL table). >>> My query works well for individual names and also for 'All' but it >>> would look and be easier to read if I could group the rows by name and >>> maybe list alphabetically. My 'All' code portion currently looks like >>> this: >>> $query = >>> "SELECT * >>> FROM $table >>> WHERE 1 = 1 "; >>> if($name != "All") $query .= "and name = '".$name."'"; >>> $result = mysql_query($query); >>> thanks for any ... >>> John >> You're probably going to want to use one of the "joins". 'LEFT JOIN', >> 'INNER JOIN', etc. Then 'ORDER BY name DESC'. This will give you your >> results in alphabetical order. > > Any examples on how to integrate 'join' code for something like this > in PHP? Thanks > |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
jcage@lycos.com wrote:
> On Nov 2, 8:50 pm, Chris Gorospe <ch...@ekast.com> wrote: >> jc...@lycos.com wrote: >>> I have four fields, "name", "date", "task", and "title" - and am >>> trying to PHP code where if I click 'All', all rows for 'Bob' followed >>> by all rows in a MySQL datebase for 'Jim' and then all rows for 'Joe' >>> would be displayed alphabetically (by name), with all of Bob's rows >>> grouped together and then all of Jim's and then all of Joe's, etc >>> (versus the way they may reside in the MySQL table). >>> My query works well for individual names and also for 'All' but it >>> would look and be easier to read if I could group the rows by name and >>> maybe list alphabetically. My 'All' code portion currently looks like >>> this: >>> $query = >>> "SELECT * >>> FROM $table >>> WHERE 1 = 1 "; >>> if($name != "All") $query .= "and name = '".$name."'"; >>> $result = mysql_query($query); >>> thanks for any ... >>> John >> You're probably going to want to use one of the "joins". 'LEFT JOIN', >> 'INNER JOIN', etc. Then 'ORDER BY name DESC'. This will give you your >> results in alphabetical order. > > Any examples on how to integrate 'join' code for something like this > in PHP? Thanks > Actually, you don't even need to use JOIN. I was under the impression these people were in different tables. Just use the 'ORDER BY name DESC' with your 'select *', and that should get you want you're looking for. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
On Nov 2, 9:52 pm, Chris Gorospe <ch...@ekast.com> wrote:
> jc...@lycos.com wrote: > > On Nov 2, 8:50 pm, Chris Gorospe <ch...@ekast.com> wrote: > >> jc...@lycos.com wrote: > >>> I have four fields, "name", "date", "task", and "title" - and am > >>> trying to PHP code where if I click 'All', all rows for 'Bob' followed > >>> by all rows in a MySQL datebase for 'Jim' and then all rows for 'Joe' > >>> would be displayed alphabetically (by name), with all of Bob's rows > >>> grouped together and then all of Jim's and then all of Joe's, etc > >>> (versus the way they may reside in the MySQL table). > >>> My query works well for individual names and also for 'All' but it > >>> would look and be easier to read if I could group the rows by name and > >>> maybe list alphabetically. My 'All' code portion currently looks like > >>> this: > >>> $query = > >>> "SELECT * > >>> FROM $table > >>> WHERE 1 = 1 "; > >>> if($name != "All") $query .= "and name = '".$name."'"; > >>> $result = mysql_query($query); > >>> thanks for any ... > >>> John > >> You're probably going to want to use one of the "joins". 'LEFT JOIN', > >> 'INNER JOIN', etc. Then 'ORDER BY name DESC'. This will give you your > >> results in alphabetical order. > > > Any examples on how to integrate 'join' code for something like this > > in PHP? Thanks > > Actually, you don't even need to use JOIN. I was under the impression > these people were in different tables. > > Just use the 'ORDER BY name DESC' with your 'select *', and that should > get you want you're looking for. Hey, that's it... Thank You Sir... Appreciate the very much. :-) |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
On Fri, 02 Nov 2007 21:12:42 -0500, Jerry Stuckle wrote...
> >jcage@lycos.com wrote: >> I have four fields, "name", "date", "task", and "title" - and am >> trying to PHP code where if I click 'All', all rows for 'Bob' followed >> by all rows in a MySQL datebase for 'Jim' and then all rows for 'Joe' >> would be displayed alphabetically (by name), with all of Bob's rows >> grouped together and then all of Jim's and then all of Joe's, etc >> (versus the way they may reside in the MySQL table). >> >> My query works well for individual names and also for 'All' but it >> would look and be easier to read if I could group the rows by name and >> maybe list alphabetically. My 'All' code portion currently looks like >> this: >> >> $query = >> "SELECT * >> FROM $table >> WHERE 1 = 1 "; >> if($name != "All") $query .= "and name = '".$name."'"; >> >> $result = mysql_query($query); >> >> >> thanks for any ... >> John >> >> > >You're asking about SQL, not PHP. Try comp.databases.mysql. > I get alt.php.sql on my newsgroup list, and that's ful for SQL related questions too. Tom -- NewsGuy Free Trial Accounts Now a massive 20 Gigabytes of unrestricted downloads ! http://newsguy.com/ |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
Tom wrote:
> On Fri, 02 Nov 2007 21:12:42 -0500, Jerry Stuckle wrote... >> jcage@lycos.com wrote: >>> I have four fields, "name", "date", "task", and "title" - and am >>> trying to PHP code where if I click 'All', all rows for 'Bob' followed >>> by all rows in a MySQL datebase for 'Jim' and then all rows for 'Joe' >>> would be displayed alphabetically (by name), with all of Bob's rows >>> grouped together and then all of Jim's and then all of Joe's, etc >>> (versus the way they may reside in the MySQL table). >>> >>> My query works well for individual names and also for 'All' but it >>> would look and be easier to read if I could group the rows by name and >>> maybe list alphabetically. My 'All' code portion currently looks like >>> this: >>> >>> $query = >>> "SELECT * >>> FROM $table >>> WHERE 1 = 1 "; >>> if($name != "All") $query .= "and name = '".$name."'"; >>> >>> $result = mysql_query($query); >>> >>> >>> thanks for any ... >>> John >>> >>> >> You're asking about SQL, not PHP. Try comp.databases.mysql. >> > > > I get alt.php.sql on my newsgroup list, and that's ful for SQL related > questions too. > > > Tom But comp.databases.mysql is much better than alt.php.sql for MySQL questions. The problem is no database implements the SQL standards 100% or the same way. comp.databases.mysql has MySQL specific info - which, in this case, is 100% independent of the language being used to access MySQL, but 100% dependent on MySQL. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
On Nov 2, 8:52 pm, Chris Gorospe <ch...@ekast.com> wrote:
> Just use the 'ORDER BY name DESC' with your 'select *', and that should > get you want you're looking for. ---------------------------------------- $query = "SELECT DISTINCT names FROM $table Order by names"; $result = mysql_query($query); So.... :-) In the above code, 'Order by' works very well when not used in conjunction with other queryable items but how about when used in a query where other possible choices are included? How do I use 'Order by' in the query below where it will only act on 'names' when "All" is selected and otherwise, not really be active during the query as in the code below? Any thoughts? TIA $query = "SELECT * FROM $table WHERE 1 = 1 "; if($year != "All") $query .= "and year = '".$year."'"; if($status != "All") $query .= "and status = '".$status."'"; if($names != "All") $query .= "and names = '".$names."'"; $result = mysql_query($query); |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
jcage@lycos.com wrote:
> On Nov 2, 8:52 pm, Chris Gorospe <ch...@ekast.com> wrote: > >> Just use the 'ORDER BY name DESC' with your 'select *', and that should >> get you want you're looking for. > ---------------------------------------- > > $query = "SELECT DISTINCT names > FROM $table > Order by names"; > $result = mysql_query($query); > > So.... :-) > > In the above code, 'Order by' works very well when not used in > conjunction with other queryable items but how about when used in a > query where other possible choices are included? How do I use 'Order > by' in the query below where it will only act on 'names' when "All" is > selected and otherwise, not really be active during the query as in > the code below? Any thoughts? TIA > > > $query = "SELECT * > FROM $table > WHERE 1 = 1 "; > if($year != "All") $query .= "and year = '".$year."'"; > if($status != "All") $query .= "and status = '".$status."'"; > if($names != "All") $query .= "and names = '".$names."'"; > $result = mysql_query($query); > > This isn't a PHP question. Try comp.database.mysql when asking mysql questions. That's where the SQL experts hang out. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
On Nov 8, 5:25 am, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> jc...@lycos.com wrote: > > On Nov 2, 8:52 pm, Chris Gorospe <ch...@ekast.com> wrote: > > >> Just use the 'ORDER BY name DESC' with your 'select *', and that should > >> get you want you're looking for. > > ---------------------------------------- > > > $query = "SELECT DISTINCT names > > FROM $table > > Order by names"; > > $result = mysql_query($query); > > > So.... :-) > > > In the above code, 'Order by' works very well when not used in > > conjunction with other queryable items but how about when used in a > > query where other possible choices are included? How do I use 'Order > > by' in the query below where it will only act on 'names' when "All" is > > selected and otherwise, not really be active during the query as in > > the code below? Any thoughts? TIA > > > $query = "SELECT * > > FROM $table > > WHERE 1 = 1 "; > > if($year != "All") $query .= "and year = '".$year."'"; > > if($status != "All") $query .= "and status = '".$status."'"; > > if($names != "All") $query .= "and names = '".$names."'"; > > $result = mysql_query($query); > > This isn't a PHP question. > > Try comp.database.mysql when asking mysql questions. That's where the > SQL experts hang out. > > -- > ================== > Remove the "x" from my email address > Jerry Stuckle > JDS Computer Training Corp. > jstuck...@attglobal.net > ================== will do. thanks for the tip. |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
$query = "SELECT DISTINCT names
FROM $table Order by names"; $result = mysql_query($query); In the code above, 'Order by' works very well when not used in conjunction with other query-able items but how about when used in a query where other possible choices are included? How do I use 'Order by' in the query below where it will only act on 'names' when "All" is selected and otherwise, not really be active during the query as in the code below? I'm using PHP to access MySQL. Thanks $query = "SELECT * FROM $table WHERE 1 = 1 "; if($year != "All") $query .= "and year = '".$year."'"; if($status != "All") $query .= "and status = '". $status."'"; if($names != "All") $query .= "and names = '".$names."'"; $result = mysql_query($query); |
|
|
|
#14 |
|
Messages: n/a
Hébergeur: |
On 8 Nov, 14:13, jc...@lycos.com wrote:
> $query = "SELECT DISTINCT names > FROM $table > Order by names"; > $result = mysql_query($query); > > In the code above, 'Order by' works very well when not used in > conjunction with other query-able items but how about when used in a > query where other possible choices are included? How do I use 'Order > by' in the query below where it will only act on 'names' when "All" > is > selected and otherwise, not really be active during the query as in > the code below? I'm using PHP to access MySQL. Thanks > > $query = "SELECT * > FROM $table > WHERE 1 = 1 "; > if($year != "All") $query .= "and year = '".$year."'"; > if($status != "All") $query .= "and status = '". > $status."'"; > if($names != "All") $query .= "and names = '".$names."'"; > $result = mysql_query($query); ORDER BY names the whole WHERE clause is superfluous in this query. |
|
|
|
#15 |
|
Messages: n/a
Hébergeur: |
"Captain Paralytic" <paul_lautman@yahoo.com> wrote in message news:1194534736.193031.304830@q5g2000prf.googlegro ups.com... > On 8 Nov, 14:13, jc...@lycos.com wrote: >> $query = "SELECT DISTINCT names >> FROM $table >> Order by names"; >> $result = mysql_query($query); >> >> In the code above, 'Order by' works very well when not used in >> conjunction with other query-able items but how about when used in a >> query where other possible choices are included? How do I use 'Order >> by' in the query below where it will only act on 'names' when "All" >> is >> selected and otherwise, not really be active during the query as in >> the code below? I'm using PHP to access MySQL. Thanks >> >> $query = "SELECT * >> FROM $table >> WHERE 1 = 1 "; >> if($year != "All") $query .= "and year = '".$year."'"; >> if($status != "All") $query .= "and status = '". >> $status."'"; >> if($names != "All") $query .= "and names = '".$names."'"; >> $result = mysql_query($query); > > ORDER BY names just a note about legibility here... $sql[] = " SELECT * FROM " . $table . " WHERE 1 = 1 "; if ($year != 'ALL') { $sql[] = " AND year = '" . $year . "'"; } if ($status != 'ALL') { $sql[] = " AND status = '" . $status. "'"; } if ($names != 'ALL') { $sql[] = " AND names = '" . $names. "'"; } $sql = implode("\r\n", $sql); that avoide the problem of forgetting to put a space between the if'd conditions/criterion. note, i put the spaces before 'and' simply so that when i go to debug the current sql being run, it echos nice and formatted in the browser. |
|
|
|
#16 |
|
Messages: n/a
Hébergeur: |
On Nov 8, 4:34 pm, "Steve" <no....@example.com> wrote:
> "Captain Paralytic" <paul_laut...@yahoo.com> wrote in message > > news:1194534736.193031.304830@q5g2000prf.googlegro ups.com... > > > > > On 8 Nov, 14:13, jc...@lycos.com wrote: > >> $query = "SELECT DISTINCT names > >> FROM $table > >> Order by names"; > >> $result = mysql_query($query); > > >> In the code above, 'Order by' works very well when not used in > >> conjunction with other query-able items but how about when used in a > >> query where other possible choices are included? How do I use 'Order > >> by' in the query below where it will only act on 'names' when "All" > >> is > >> selected and otherwise, not really be active during the query as in > >> the code below? I'm using PHP to access MySQL. Thanks > > >> $query = "SELECT * > >> FROM $table > >> WHERE 1 = 1 "; > >> if($year != "All") $query .= "and year = '".$year."'"; > >> if($status != "All") $query .= "and status = '". > >> $status."'"; > >> if($names != "All") $query .= "and names = '".$names."'"; > >> $result = mysql_query($query); > > > ORDER BY names > > just a note about legibility here... > > $sql[] = " > SELECT * > FROM " . $table . " > WHERE 1 = 1 > "; > if ($year != 'ALL') > { > $sql[] = " AND year = '" . $year . "'";} > > if ($status != 'ALL') > { > $sql[] = " AND status = '" . $status. "'";} > > if ($names != 'ALL') > { > $sql[] = " AND names = '" . $names. "'";} > > $sql = implode("\r\n", $sql); > > that avoide the problem of forgetting to put a space between the if'd > conditions/criterion. note, i put the spaces before 'and' simply so that > when i go to debug the current sql being run, it echos nice and formatted in > the browser. You and your formatting again You won't give up until you educateall newbies to proper formatting, eh? ![]() Cheers |
|
|
|
#17 |
|
Messages: n/a
Hébergeur: |
"Darko" <darko.maksimovic@gmail.com> wrote in message news:1194548559.891114.33150@v23g2000prn.googlegro ups.com... > On Nov 8, 4:34 pm, "Steve" <no....@example.com> wrote: >> "Captain Paralytic" <paul_laut...@yahoo.com> wrote in message >> >> news:1194534736.193031.304830@q5g2000prf.googlegro ups.com... >> >> >> >> > On 8 Nov, 14:13, jc...@lycos.com wrote: >> >> $query = "SELECT DISTINCT names >> >> FROM $table >> >> Order by names"; >> >> $result = mysql_query($query); >> >> >> In the code above, 'Order by' works very well when not used in >> >> conjunction with other query-able items but how about when used in a >> >> query where other possible choices are included? How do I use 'Order >> >> by' in the query below where it will only act on 'names' when "All" >> >> is >> >> selected and otherwise, not really be active during the query as in >> >> the code below? I'm using PHP to access MySQL. Thanks >> >> >> $query = "SELECT * >> >> FROM $table >> >> WHERE 1 = 1 "; >> >> if($year != "All") $query .= "and year = '".$year."'"; >> >> if($status != "All") $query .= "and status = '". >> >> $status."'"; >> >> if($names != "All") $query .= "and names = '".$names."'"; >> >> $result = mysql_query($query); >> >> > ORDER BY names >> >> just a note about legibility here... >> >> $sql[] = " >> SELECT * >> FROM " . $table . " >> WHERE 1 = 1 >> "; >> if ($year != 'ALL') >> { >> $sql[] = " AND year = '" . $year . "'";} >> >> if ($status != 'ALL') >> { >> $sql[] = " AND status = '" . $status. "'";} >> >> if ($names != 'ALL') >> { >> $sql[] = " AND names = '" . $names. "'";} >> >> $sql = implode("\r\n", $sql); >> >> that avoide the problem of forgetting to put a space between the if'd >> conditions/criterion. note, i put the spaces before 'and' simply so that >> when i go to debug the current sql being run, it echos nice and formatted >> in >> the browser. > > You and your formatting again You won't give up until you educate> all newbies to > proper formatting, eh? ![]() he, he, he, he! well, actually the formatting is just a bonus. some db's, even though they strip out spaces themselves when parsing, will caugh up errors if the clauses run together...which happens the way the op wrote it...where 1=1and year='2007'and status=... building $sql as an array and then imploding it back on itself avoids the problem and also makes for more clearly defined if () cases. but, i'm still chuckling. that was good. |
|
|
|
#18 |
|
Messages: n/a
Hébergeur: |
Steve wrote:
> "Captain Paralytic" <paul_lautman@yahoo.com> wrote in message > news:1194534736.193031.304830@q5g2000prf.googlegro ups.com... >> On 8 Nov, 14:13, jc...@lycos.com wrote: >>> $query = "SELECT DISTINCT names >>> FROM $table >>> Order by names"; >>> $result = mysql_query($query); >>> >>> In the code above, 'Order by' works very well when not used in >>> conjunction with other query-able items but how about when used in >>> a query where other possible choices are included? How do I use >>> 'Order by' in the query below where it will only act on 'names' >>> when "All" is >>> selected and otherwise, not really be active during the query as in >>> the code below? I'm using PHP to access MySQL. Thanks >>> >>> $query = "SELECT * >>> FROM $table >>> WHERE 1 = 1 "; >>> if($year != "All") $query .= "and year = '".$year."'"; >>> if($status != "All") $query .= "and status = '". >>> $status."'"; >>> if($names != "All") $query .= "and names = '".$names."'"; >>> $result = mysql_query($query); >> >> ORDER BY names > > just a note about legibility here... > > $sql[] = " > SELECT * > FROM " . $table . " > WHERE 1 = 1 > "; > if ($year != 'ALL') > { > $sql[] = " AND year = '" . $year . "'"; > } > if ($status != 'ALL') > { > $sql[] = " AND status = '" . $status. "'"; > } > if ($names != 'ALL') > { > $sql[] = " AND names = '" . $names. "'"; > } > $sql = implode("\r\n", $sql); > > that avoide the problem of forgetting to put a space between the if'd > conditions/criterion. note, i put the spaces before 'and' simply so > that when i go to debug the current sql being run, it echos nice and > formatted in the browser. Please learn to post replies to the correct post. I do not need advise on formatting, the OP may do! |
|
|
|
#19 |
|
Messages: n/a
Hébergeur: |
"Paul Lautman" <paul.lautman@btinternet.com> wrote in message news:5phaqhFrfrejU1@mid.individual.net... > Steve wrote: >> "Captain Paralytic" <paul_lautman@yahoo.com> wrote in message >> news:1194534736.193031.304830@q5g2000prf.googlegro ups.com... >>> On 8 Nov, 14:13, jc...@lycos.com wrote: >>>> $query = "SELECT DISTINCT names >>>> FROM $table >>>> Order by names"; >>>> $result = mysql_query($query); >>>> >>>> In the code above, 'Order by' works very well when not used in >>>> conjunction with other query-able items but how about when used in >>>> a query where other possible choices are included? How do I use >>>> 'Order by' in the query below where it will only act on 'names' >>>> when "All" is >>>> selected and otherwise, not really be active during the query as in >>>> the code below? I'm using PHP to access MySQL. Thanks >>>> >>>> $query = "SELECT * >>>> FROM $table >>>> WHERE 1 = 1 "; >>>> if($year != "All") $query .= "and year = '".$year."'"; >>>> if($status != "All") $query .= "and status = '". >>>> $status."'"; >>>> if($names != "All") $query .= "and names = '".$names."'"; >>>> $result = mysql_query($query); >>> >>> ORDER BY names >> >> just a note about legibility here... >> >> $sql[] = " >> SELECT * >> FROM " . $table . " >> WHERE 1 = 1 >> "; >> if ($year != 'ALL') >> { >> $sql[] = " AND year = '" . $year . "'"; >> } >> if ($status != 'ALL') >> { >> $sql[] = " AND status = '" . $status. "'"; >> } >> if ($names != 'ALL') >> { >> $sql[] = " AND names = '" . $names. "'"; >> } >> $sql = implode("\r\n", $sql); >> >> that avoide the problem of forgetting to put a space between the if'd >> conditions/criterion. note, i put the spaces before 'and' simply so >> that when i go to debug the current sql being run, it echos nice and >> formatted in the browser. > > Please learn to post replies to the correct post. I do not need advise on > formatting, the OP may do! first of all, it's a thread, dude. second, it's usenet. like it, or lump it. ![]() |
|
|
|
#20 |
|
Messages: n/a
Hébergeur: |
On 8 Nov, 20:45, "Paul Lautman" <paul.laut...@btinternet.com> wrote:
> Steve wrote: > > "Captain Paralytic" <paul_laut...@yahoo.com> wrote in message > >news:1194534736.193031.304830@q5g2000prf.googlegr oups.com... > >> On 8 Nov, 14:13, jc...@lycos.com wrote: > >>> $query = "SELECT DISTINCT names > >>> FROM $table > >>> Order by names"; > >>> $result = mysql_query($query); > > >>> In the code above, 'Order by' works very well when not used in > >>> conjunction with other query-able items but how about when used in > >>> a query where other possible choices are included? How do I use > >>> 'Order by' in the query below where it will only act on 'names' > >>> when "All" is > >>> selected and otherwise, not really be active during the query as in > >>> the code below? I'm using PHP to access MySQL. Thanks > > >>> $query = "SELECT * > >>> FROM $table > >>> WHERE 1 = 1 "; > >>> if($year != "All") $query .= "and year = '".$year."'"; > >>> if($status != "All") $query .= "and status = '". > >>> $status."'"; > >>> if($names != "All") $query .= "and names = '".$names."'"; > >>> $result = mysql_query($query); > > >> ORDER BY names > > > just a note about legibility here... > > > $sql[] = " > > SELECT * > > FROM " . $table . " > > WHERE 1 = 1 > > "; > > if ($year != 'ALL') > > { > > $sql[] = " AND year = '" . $year . "'"; > > } > > if ($status != 'ALL') > > { > > $sql[] = " AND status = '" . $status. "'"; > > } > > if ($names != 'ALL') > > { > > $sql[] = " AND names = '" . $names. "'"; > > } > > $sql = implode("\r\n", $sql); > > > that avoide the problem of forgetting to put a space between the if'd > > conditions/criterion. note, i put the spaces before 'and' simply so > > that when i go to debug the current sql being run, it echos nice and > > formatted in the browser. > > Please learn to post replies to the correct post. I do not need advise on > formatting, the OP may do! Definitely not, but some advice on spelling (and punctuation) wouldn't go amiss. He he he ;-) |
|
|
|
#21 |
|
Messages: n/a
Hébergeur: |
"strawberry" <zac.carey@gmail.com> wrote in message news:1194560072.097968.186940@s15g2000prm.googlegr oups.com... > On 8 Nov, 20:45, "Paul Lautman" <paul.laut...@btinternet.com> wrote: >> Steve wrote: >> > "Captain Paralytic" <paul_laut...@yahoo.com> wrote in message >> >news:1194534736.193031.304830@q5g2000prf.googlegr oups.com... >> >> On 8 Nov, 14:13, jc...@lycos.com wrote: >> >>> $query = "SELECT DISTINCT names >> >>> FROM $table >> >>> Order by names"; >> >>> $result = mysql_query($query); >> >> >>> In the code above, 'Order by' works very well when not used in >> >>> conjunction with other query-able items but how about when used in >> >>> a query where other possible choices are included? How do I use >> >>> 'Order by' in the query below where it will only act on 'names' >> >>> when "All" is >> >>> selected and otherwise, not really be active during the query as in >> >>> the code below? I'm using PHP to access MySQL. Thanks >> >> >>> $query = "SELECT * >> >>> FROM $table >> >>> WHERE 1 = 1 "; >> >>> if($year != "All") $query .= "and year = '".$year."'"; >> >>> if($status != "All") $query .= "and status = '". >> >>> $status."'"; >> >>> if($names != "All") $query .= "and names = '".$names."'"; >> >>> $result = mysql_query($query); >> >> >> ORDER BY names >> >> > just a note about legibility here... >> >> > $sql[] = " >> > SELECT * >> > FROM " . $table . " >> > WHERE 1 = 1 >> > "; >> > if ($year != 'ALL') >> > { >> > $sql[] = " AND year = '" . $year . "'"; >> > } >> > if ($status != 'ALL') >> > { >> > $sql[] = " AND status = '" . $status. "'"; >> > } >> > if ($names != 'ALL') >> > { >> > $sql[] = " AND names = '" . $names. "'"; >> > } >> > $sql = implode("\r\n", $sql); >> >> > that avoide the problem of forgetting to put a space between the if'd >> > conditions/criterion. note, i put the spaces before 'and' simply so >> > that when i go to debug the current sql being run, it echos nice and >> > formatted in the browser. >> >> Please learn to post replies to the correct post. I do not need advise on >> formatting, the OP may do! > > Definitely not, but some advice on spelling (and punctuation) wouldn't > go amiss. He he he ;-) sorry, if you look at my posts, they're splintered with errors on all counts. hell, i even leave out words altogether sometimes...damn fingersjust can't keep up. |
|
|
|
#22 |
|
Messages: n/a
Hébergeur: |
On 8 Nov, 21:40, "Steve" <no....@example.com> wrote:
| first of all, it's a thread, dude. Err no. A thread is a linked collection of posts on a particular subject. When you post a reply you do so to a previous post within a thread, dude. | second, it's usenet. like it, or lump it. It is not usenet's fault. Usenet is quite capable of attaching responses correctly. What is needed is a bit of intelligence on the part of the user, dude. |
|
|
|
#23 |