For loop using functions : Repeating code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

For loop using functions : Repeating code

Post by facets »

Hi,

I have 20 functions which call sqlqueries and creates drop down menus.
// Code to add to repeating table contents :

Code: Select all

<? func1() ?>

function func1() {

	$sql_query = mysql_query("SELECT suitType, suitabilityId FROM ausuitability");
	echo "<select name=\"suitabilityId\">";
    echo "<option value=\"\">-- Select Option --</option>"; 
    	while(list($suitName, $suitabilityId)=mysql_fetch_array($sql_query)) {
		$suitName = stripslashes($suitName);
		echo "<option value=\"$suitabilityId\">$suitName</option>";
	}
	echo "</select>";
	mysql_free_result($sql_query);
}
So should something like the following work?

Code: Select all

$dsc=array('1st','2nd');
$named = array(func1(),func1());

for($x = 0; $x<count($dsc); $x++)
{
    if($x % 2) 
    {
	    echo "<tr class=\"row1\">\n";
    } 
    else 
    {
	    echo "<tr class=\"row2\">\n";
    }
	
  echo "<td width=\"200px\" colspan=\"2\" valign=\"top\">".$dsc[$x]."</td>\n";
  echo "<td width=\"200px\" colspan=\"2\">".$named[$x]."</td></tr>\n";
}
Any ideas?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Ideas on what?

Could ou finish asking the question please? :P
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

:) Sure..

I can't work out how to call the function in the for loop.
It puts the drop down menu above the table rather than in it.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

that sounds like a html problem, make sure your table is constructed properly in your html
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

in func1 you are echoing out the dropdown list instead of returning it, so $named doesn't have a value;
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

The functions are being called on this line :

Code: Select all

$named = array(necklabel(),icebucket())
rather than in the call for $named "

Code: Select all

echo "<td width=\"200px\" colspan=\"2\">".$named[$x]."</td></tr>\n";
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

ok.. after a quick check.. $named echo's the word array.

so how could I do this?
can a function be included in an array like that or am I over complicating things.
(Perhaps for my skill level)

tks, wil
leenoble_uk
Forum Contributor
Posts: 108
Joined: Fri May 03, 2002 10:33 am
Location: Cheshire
Contact:

Post by leenoble_uk »

You need to change your function to stop it echoing out the results, and instead return them to the place from which they were called:

Code: Select all

function func1() {
 
    $sql_query = mysql_query("SELECT suitType, suitabilityId FROM ausuitability");
    $output = "<select name=\"suitabilityId\">";
    $output .= "<option value=\"\">-- Select Option --</option>"; 
        while(list($suitName, $suitabilityId)=mysql_fetch_array($sql_query)) {
        $suitName = stripslashes($suitName);
        $output .= "<option value=\"$suitabilityId\">$suitName</option>";
    }
    $output .= "</select>";
    mysql_free_result($sql_query);
return $output;
}
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

Spot on!
thank you.
Post Reply