glennn.php Posted April 21, 2006 Share Posted April 21, 2006 ok, here goes:[code]$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>";}[/code]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,elseIF 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) Quote Link to comment https://forums.phpfreaks.com/topic/8025-little-bit-more-help-with-a-query/ Share on other sites More sharing options...
gizmola Posted April 21, 2006 Share Posted April 21, 2006 [!--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) [snapback]367123[/snapback][/div][div class=\'quotemain\'][!--quotec--]ok, here goes:[code]$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>";}[/code][/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:[code]if (!empty($row['CustomField15'])) { $url = $row['CustomField15'];} else { $url = $row['CustomField1'].... etc.;}[/code]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. Quote Link to comment https://forums.phpfreaks.com/topic/8025-little-bit-more-help-with-a-query/#findComment-29269 Share on other sites More sharing options...
glennn.php Posted April 21, 2006 Author Share Posted April 21, 2006 beautiful - almost...since i didn't know where to put mysql_fetch_assoc() i just did this:[code]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>";}[/code]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... Quote Link to comment https://forums.phpfreaks.com/topic/8025-little-bit-more-help-with-a-query/#findComment-29277 Share on other sites More sharing options...
adgold Posted April 21, 2006 Share Posted April 21, 2006 That's exactly what I needed to know today, too.Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/8025-little-bit-more-help-with-a-query/#findComment-29369 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.