damian0612 Posted April 30, 2009 Share Posted April 30, 2009 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 More sharing options...
ignace Posted April 30, 2009 Share Posted April 30, 2009 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 https://forums.phpfreaks.com/topic/156269-solved-help-with-creating-a-function/#findComment-822665 Share on other sites More sharing options...
damian0612 Posted April 30, 2009 Author Share Posted April 30, 2009 I have just substitued [webpage] for the actual web address, for example: If row [3] = http://www.bbcnews.co.uk then $linksource = "BBC News" Link to comment https://forums.phpfreaks.com/topic/156269-solved-help-with-creating-a-function/#findComment-822669 Share on other sites More sharing options...
timt Posted April 30, 2009 Share Posted April 30, 2009 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 https://forums.phpfreaks.com/topic/156269-solved-help-with-creating-a-function/#findComment-822684 Share on other sites More sharing options...
damian0612 Posted April 30, 2009 Author Share Posted April 30, 2009 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 https://forums.phpfreaks.com/topic/156269-solved-help-with-creating-a-function/#findComment-822993 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.