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
https://forums.phpfreaks.com/topic/156269-solved-help-with-creating-a-function/
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";
?>

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] );

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

Archived

This topic is now archived and is closed to further replies.

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