Jump to content

Question: Default Values of Functions


Lamez

Recommended Posts

I am working on a function that makes a list out of an existing array, this website was pre-made, and I cannot locate the list for the life of me. How can I make a default value in function based off of a variable?

 

<?php
function makeSportList($name = "", $sportList = $sport_list){
$list = '<select name="sport" id="sport">';
$list .= '<option value="-1">All Sports</option>';
for ($i=0;$i<=count($sportList);$i++){
	if(md5(strtolower($name)) == md5(strtolower($sportList[$i]))){
		$sel = "SELECTED";
	}else{
		$sel = "";
	}
	$list .= '<option value="'.$sportList[$i].'" '.$sel.'>'.$sportList[$i].'</option>';
}
return $list.'</select>';
}
?>

 

The manual says nothing of this. :/

 

-thanks!

Link to comment
Share on other sites

Sorry, you could but it is a very poor design choice.

 

<?php
function makeSportList($name = "", $sportList = null) {
   if (is_numm($sportList)) {
     global $sport_list;
     $sportList = $sport_list;
   }
   $list = '<select name="sport" id="sport">';
   $list .= '<option value="-1">All Sports</option>';
   for ($i=0;$i<=count($sportList);$i++){
      if(md5(strtolower($name)) == md5(strtolower($sportList[$i]))){
         $sel = "SELECTED";
      }else{
         $sel = "";
      }
      $list .= '<option value="'.$sportList[$i].'" '.$sel.'>'.$sportList[$i].'</option>';
   }
   return $list.'</select>';
}
?>

 

Terrible idea.

Link to comment
Share on other sites

Thanks for the (not needed) help, I guess I had a brainfart (again), here is how I fixed it:

 

<?php
function makeSportList($sportList, $name = ""){
$list = '<select name="sport" id="sport">';
$list .= '<option value="-1">All Sports</option>';
for ($i=0;$i<=count($sportList);$i++){
	if(md5(strtolower($name)) === md5(strtolower($sportList[$i]))){
		$sel = "SELECTED";
	}else{
		$sel = "";
	}
	$list .= '<option value="'.$sportList[$i].'" '.$sel.'>'.$sportList[$i].'</option>';
}
return $list.'</select>';
}
echo makeSportList($sport_list);
echo "<br />";
echo makeSportList($sport_list, "football");
?>

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.