Jump to content


Photo

little bit more help with a query...


  • Please log in to reply
3 replies to this topic

#1 glennn.php

glennn.php
  • Validating
  • PipPipPip
  • Advanced Member
  • 251 posts
  • LocationAtlanta, GA USA

Posted 21 April 2006 - 07:35 AM

ok, here goes:


$sql = "SELECT CustomField1, CustomField2 , CustomField15, CustomField16 FROM oemp_members WHERE CustomField2 LIKE 'n%' OR CustomField15 LIKE 'n%' ORDER BY CustomField2 ASC";

$result = mysql_query($sql) or die("Query failed");
$lines = mysql_num_rows ( $result );

for ( $i=0;$i<$lines;$i++ )
{
    $row = mysql_fetch_array($result);
    print "<a href=\"http://${row[3]}\">${row[0]} ${row[1]}</a><br>";
}

Sometimes CustomField15 will contain data. Sometimes CustomField1 and 2 will contain data (sometimes all three...?)

what i'd love to be able to print is

IF there's data in CF15 (regardless of whether 1 and 2), then print 15,

else

IF there's no data in 15, then print 1 and 2.

i attempted if else staements above my print function up there, but it's likely in the wrong place; i was printing 15 regardless...

can someone help? thanks much... how ya doin', Darkness? :o) that's your code up there... :o)




#2 gizmola

gizmola
  • Administrators
  • Advanced Member
  • 4,664 posts
  • LocationLos Angeles, CA USA

Posted 21 April 2006 - 08:31 AM

[!--quoteo(post=367123:date=Apr 21 2006, 12:35 AM:name=glennn.php)--][div class=\'quotetop\']QUOTE(glennn.php @ Apr 21 2006, 12:35 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
ok, here goes:
$sql = "SELECT CustomField1, CustomField2 , CustomField15, CustomField16 FROM oemp_members WHERE CustomField2 LIKE 'n%' OR CustomField15 LIKE 'n%' ORDER BY CustomField2 ASC";

$result = mysql_query($sql) or die("Query failed");
$lines = mysql_num_rows ( $result );

for ( $i=0;$i<$lines;$i++ )
{
    $row = mysql_fetch_array($result);
    print "<a href=\"http://${row[3]}\">${row[0]} ${row[1]}</a><br>";
}


[/quote]

Based on your description this is an either/or situation. First suggestion: use mysql_fetch_assoc(). Then you get a simple associative array keyed by column name. Right after this fetch check your condition:


if  (!empty($row['CustomField15'])) {
  $url = $row['CustomField15'];
} else {
  $url = $row['CustomField1'].... etc.;
}


As I don't fully understand what would be in CustomField1 that would make you want to concat it with CustomField2, i'll leave off here, and if there's something else that I"m missing, feel free to provide more detail.

#3 glennn.php

glennn.php
  • Validating
  • PipPipPip
  • Advanced Member
  • 251 posts
  • LocationAtlanta, GA USA

Posted 21 April 2006 - 08:51 AM

beautiful - almost...

since i didn't know where to put mysql_fetch_assoc() i just did this:

for ( $i=0;$i<$lines;$i++ )
{
    $row = mysql_fetch_array($result);
    if  (!empty($row['CustomField15'])) 
    {
  $url = $row['CustomField15'];
    } else {
  $url = $row['CustomField1'];
}
    print "$url<br>";
}

and i got what i wanted; almost. you're right, i gotta concat those two fields from the query, not later...

fields 1 and 2 are first name, last name - field 15 is nickname, if they so choose... since i'm printing a member's list, if they use a nick then i'll alphebetize by that - if they don't, then by last name...

so now i just have to CONCAT fields 1 and 2 in the query and i'm good to go. thanks for your help...




#4 adgold

adgold
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 21 April 2006 - 04:28 PM

That's exactly what I needed to know today, too.
Thanks!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users