Jump to content

little bit more help with a query...


glennn.php

Recommended Posts

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,

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)


Link to comment
Share on other sites

[!--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.
Link to comment
Share on other sites

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...


Link to comment
Share on other sites

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.