Jump to content

[SOLVED] Help with creating a function


damian0612

Recommended Posts

Hi

 

I have the following php code:

 

<?php
// Today's news             
$result = mysql_query("
SELECT *
FROM feedItems 
ORDER BY ItemAddedTime DESC
LIMIT 5", $connection); 
if(!$result){ die("Database selection failed: " . mysql_error()); } 
             
$num_rows = mysql_num_rows($result); 
If($num_rows > 0){
echo "<table width='100%'>";
while($row = mysql_fetch_array($result)){
if ( $row[3] == "http://[webpagename].com" ) {$linksource="BBC News";
} elseif($row[3] == "http://[webpagename].com"){$linksource="A";
} elseif($row[3] == "http://[webpagename].com"){$linksource="B";
} elseif($row[3] == "http://[webpagename].com"){$linksource="C";
} elseif($row[3] == "http://[webpagename].com"){$linksource="D";

...There are roughly 30 else if statements here

} else {$linksource="Source Unknown";
}
$mydate = date('H:i',strtotime($row[0]));
echo "<tr><td>$mydate <A href=$row[2]>$row[1]</A> $linksource</td></tr>";} 
echo"</table>";
} 
?>

 

I have the same if statement where it checks row[3] to find the $linksource on a few of my webpages.  This part of the code is updated regularly with new links.  I would like to turn that part of the code into a function but am struggling as a beginner how to do this, I can't seem to get it to work properly.  So, this whole part would become a function:

 

if ( $row[3] == "http://[webpagename].com" ) {$linksource="BBC News";
} elseif($row[3] == "http://[webpagename].com"){$linksource="A";
} elseif($row[3] == "http://[webpagename].com"){$linksource="B";
} elseif($row[3] == "http://[webpagename].com"){$linksource="C";
} elseif($row[3] == "http://[webpagename].com"){$linksource="D";

...There are roughly 30 else if statements here

} else {$linksource="Source Unknown";
}

 

When I try to put it as a function it doesn't work so I must be doing something wrong and was wondering if it was because I was try to pass an array into the function?

 

Please can someone help me with this?

 

Thanks

Damian

Link to comment
Share on other sites

You mean something like this?

 

<?php
function aFunctionName($str, $webpages, $linksources)
{
    for ($i = 0; $i < sizeof($webpages); $i++) {
        if (!strcmp($str, $webpage)) {
            return $linksources[$i];
        }
    }
    return "Source Unknown";
}
?>

 

btw, what is [webpagename] here? Maybe we can even simplify it further

 

<?php
if ( $row[3] == "http://[webpagename].com" ) {$linksource="BBC News";
?>

Link to comment
Share on other sites

I would build a function using switch, like this.  It is much cleaner and faster than nested ifs.  Normally you would use break; after a case: however you are returning immediately. 

 


function findLinkSource( $row3 ) {
  switch( $row3 ) { 
    case "http://[webpagename].com": return 'A';
    case "http://[webpagename].com": return 'B';
    case "http://[webpagename].com": return 'C';
    ... check all you need ...
    default:
  }
}
// call function like this
$linksource=findLinkSource( $row[3] );

Link to comment
Share on other sites

I would build a function using switch, like this.  It is much cleaner and faster than nested ifs.  Normally you would use break; after a case: however you are returning immediately. 

 


function findLinkSource( $row3 ) {
  switch( $row3 ) { 
    case "http://[webpagename].com": return 'A';
    case "http://[webpagename].com": return 'B';
    case "http://[webpagename].com": return 'C';
    ... check all you need ...
    default:
  }
}
// call function like this
$linksource=findLinkSource( $row[3] );

 

works perfectly, thanks

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.