Jump to content

While statement in function


wrathican

Recommended Posts

is it possible to have a function that contains a while statement, like the following:

function search_kb($searchtype, $searchstring, $aaction, $baction){
if ($searchstring != "" && strlen($searchstring) >= 3) {
switch ($searchtype){
	case "alliance":
	$query = "SELECT * FROM kb3_alliances WHERE lower(all_name) LIKE lower('%".$searchstring."%') ORDER BY all_name";
	$result = mysql_query($query);
	$numrows = mysql_num_rows($result);
	if($numrows !=0){
		while($row = mysql_fetch_array($results,MYSQL_NUM)){
			//collect vars.
			$allianceid = $row[0];
			$alliancename = $row[1];
			//check against kos table for entry
			$queryskos = "SELECT * FROM kos_alliance WHERE all_id='".$allianceid."'";
			$resultskos = mysql_query($queryskos);
			$numrowsskos = mysql_num_rows($resultskos);
			$skosaccarr = mysql_fetch_array($resultskos);
			$skosaccess = $skosaccarr['all_access'];
		}
	}else{
		echo "No matches found.";
	}
	break;


	case "corp":

	break;


	case"pilot":

	break;
}
}else{
?>
<form id="search" action="<?php echo $faction; ?>" method="post">
<table><tr>
<td>Type:</td><td>Text: (3 letters minimum)</td>
</tr><tr>
<td><select id="searchtype" name="searchtype"><option value="alliance">Alliance</option><option value="corp">Corporation</option><option value="pilot">Pilot</option></select></td>
<td><input id="searchphrase" name="searchphrase" type="text" size="30"/></td>
<td><input type="submit" name="submit" value="Search"></td>
</tr></table>
</form>
<?php
}
}

 

i want to return the results found by $queryskos to the script so i can do different things with it. there might be 1 result, there may be 100. but each row needs to be outputted. how do i do that?

 

at the moment i have 7 versions of this function, which i feel are VERY inefficient. here is an example of one:

function kos_change()
{
//some stuff
$searchtype = $_POST['searchtype'];
$searchstring = $_POST['searchphrase'];
if ($searchstring != "" && strlen($searchstring) >= 3) {
//start the output table
echo "<table border='1'>";
//switch statement to decide which table to search
switch($searchtype)
{
	case "alliance":
	//create query
	$querys = "SELECT * FROM kb3_alliances WHERE lower(all_name) LIKE lower('%".$searchstring."%') ORDER BY all_name";
	$results = mysql_query($querys);
	$numrowss = mysql_num_rows($results);

	if ($numrowss == 0) {
		//no matches found in the kb db
		echo "<tr><td>No matches found</td></tr>.";
	}else{
		//while there is an entry display it
		while($row = mysql_fetch_array($results,MYSQL_NUM))
		{
			//collect vars.
			$allianceid = $row[0];
			$alliancename = $row[1];
			//check against kos table for entry
			$queryskos = "SELECT * FROM kos_alliance WHERE all_id='".$allianceid."'";
			$resultskos = mysql_query($queryskos);
			$numrowsskos = mysql_num_rows($resultskos);
			$skosaccarr = mysql_fetch_array($resultskos);
			$skosaccess = $skosaccarr['all_access'];
			//if statement decides wherether or not search term is kos, if kos no button is displayed, else button to make kos
			if(($numrowsskos <= 1) && ($skosaccess <= 1))
			{
				//not in kos db. show button
				echo '<tr><td>'.$alliancename.'</td><td><form action="?a=change&b=change" method="post" name="changeflag"><select name="change"><option value="1">KOS</option><option value="2">TheCitadel</option><option value="3">CVA</option></select><input type="hidden" name="allid" value="'.$allianceid.'" /><input type="hidden" name="type" value="alliance" /><input type="submit" name="submit" value="Add Comment" /></form></td></tr>';
			}elseif ($skosaccess != '1') {
				echo "<tr><td>".$alliancename."</td><td><font color='lime'><b>Alliance is Neutral</b></td></tr>";
			}
		}
	}
	break;

	case "corp":
	$querys = "SELECT * FROM kb3_corps WHERE lower(crp_name) LIKE lower('%".$searchstring."%') ORDER BY crp_name";
	$results = mysql_query($querys);
	$numrowss = mysql_num_rows($results);

	if ($numrowss == 0) {
		//no matches found in the kb db
		echo "No matches found.";
	}else{
		//while there is an entry display it
		while($row = mysql_fetch_array($results,MYSQL_NUM))
		{
			//collect vars.
			$allianceid = $row[0];
			$alliancename = $row[1];
			//check against kos table for entry
			$queryskos = "SELECT * FROM kos_corp WHERE corp_id='".$allianceid."'";
			$resultskos = mysql_query($queryskos);
			$numrowsskos = mysql_num_rows($resultskos);
			$skosaccarr = mysql_fetch_array($resultskos);
			$skosaccess = $skosaccarr['corp_access'];
			//if statement decides wherether or not search term is kos, if kos no button is displayed, else button to make kos
			if(($numrowsskos <= 1) && ($skosaccess <= 1))
			{
				//not in kos db. show button
				echo '<tr><td>'.$alliancename.'</td><td><form action="?a=change&b=change" method="post" name="changeflag"><select name="change"><option value="1">KOS</option><option value="2">TheCitadel</option><option value="3">CVA</option></select><input type="hidden" name="allid" value="'.$allianceid.'" /><input type="hidden" name="type" value="alliance" /><input type="submit" name="submit" value="Add Comment" /></form></td></tr>';
			}elseif ($skosaccess != '1') {
				echo "<tr><td>".$alliancename."</td><td><font color='lime'><b>Corporation is Neutral</b></td></tr>";
			}
		}
	}

	break;

	case "pilot":
	$querys = "SELECT * FROM kb3_pilots WHERE lower(plt_name) LIKE lower('%".$searchstring."%') ORDER BY plt_name";
	$results = mysql_query($querys);
	$numrowss = mysql_num_rows($results);

	if ($numrowss == 0) {
		//no matches found in the kb db
		echo "No matches found.";
	}else{
		//while there is an entry display it
		while($row = mysql_fetch_array($results,MYSQL_NUM))
		{
			//collect vars.
			$allianceid = $row[0];
			$alliancename = $row[1];
			//check against kos table for entry
			$queryskos = "SELECT * FROM kos_pilot WHERE pil_id='".$allianceid."'";
			$resultskos = mysql_query($queryskos);
			$numrowsskos = mysql_num_rows($resultskos);
			$skosaccarr = mysql_fetch_array($resultskos);
			$skosaccess = $skosaccarr['pil_access'];
			//if statement decides wherether or not search term is kos, if kos no button is displayed, else button to make kos
			if(($numrowsskos <= 1) && ($skosaccess <= 1))
			{
				//not in kos db. show button
				echo '<tr><td>'.$alliancename.'</td><td><form action="?a=change&b=change" method="post" name="changeflag"><select name="change"><option value="1">KOS</option><option value="2">TheCitadel</option><option value="3">CVA</option><option value="4">Admin</option></select><input type="hidden" name="allid" value="'.$allianceid.'" /><input type="hidden" name="type" value="alliance" /><input type="submit" name="submit" value="Add Comment" /></form></td></tr>';
			}elseif ($skosaccess != '1') {
				echo "<tr><td>".$alliancename."</td><td><font color='lime'><b>Pilot is Neutral</b></td></tr>";
			}
		}
	}

	break;
}
//end the output table
echo "</table>";
echo "<br /><br />Click <a href='?a=addc'>here</a> to search again.";
}else{
//html output
?>
<form id="search" action="?a=addc" method="post">
<table><tr>
<td>Type:</td><td>Text: (3 letters minimum)</td>
</tr><tr>
<td><select id="searchtype" name="searchtype"><option value="alliance">Alliance</option><option value="corp">Corporation</option><option value="pilot">Pilot</option></select></td>
<td><input id="searchphrase" name="searchphrase" type="text" size="30"/></td>
<td><input type="submit" name="submit" value="Search"></td>
</tr></table>
</form>
<?php
}
}

 

the only thing that changes within each version is the output that is generated

Link to comment
https://forums.phpfreaks.com/topic/93611-while-statement-in-function/
Share on other sites

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.