Page 1 of 1

For loop using functions : Repeating code

Posted: Tue May 31, 2005 7:15 am
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?

Posted: Tue May 31, 2005 8:06 am
by Chris Corbyn
Ideas on what?

Could ou finish asking the question please? :P

Posted: Tue May 31, 2005 8:12 am
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.

Posted: Tue May 31, 2005 8:16 am
by shiznatix
that sounds like a html problem, make sure your table is constructed properly in your html

Posted: Tue May 31, 2005 8:16 am
by phpScott
in func1 you are echoing out the dropdown list instead of returning it, so $named doesn't have a value;

Posted: Tue May 31, 2005 8:19 am
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";

Posted: Tue May 31, 2005 8:24 am
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

Posted: Tue May 31, 2005 8:31 am
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;
}

Posted: Tue May 31, 2005 8:57 am
by facets
Spot on!
thank you.