Jump to content

[SOLVED] How to use this function FillList ($sql, $selected=0)


arjay_comsci

Recommended Posts

function FillList ($sql, $selected=0)
{

$row = array();
$cnt = 0;
$this->query_fill=new Query ($sql, $this->conn);
$result=$this->query_fill->subExecute();
while ($row = mysql_fetch_row($result)) {

$id = $row[0];
$name = $row[1];

$cnt += 1;
if ($selected == $id) {
echo "<OPTION value=$id selected>$name</OPTION>";
} else {
echo "<OPTION value=$id>$name</OPTION>";
}
}

$this->query_fill->Free(); 
return $cnt; 

hi mark,,I really appreciate your effort to my problem..but i want to detail my problem to you..

 

this is My MysqlClass.php-- there are so many many functions on it but I only include the Function for combo box.

// FillList will echo to the page the selected records as options for a selection box

// The 1st field selected should be the id and the 2nd should be the description

function FillList ($sql, $selected=0)

{

 

$row = array();

$cnt = 0;

$this->query_fill=new Query ($sql, $this->conn);

$result=$this->query_fill->subExecute();

while ($row = mysql_fetch_row($result)) {

 

$id = $row[0];

$name = $row[1];

 

$cnt += 1;

if ($selected == $id) {

echo "<OPTION value=$id selected>$name</OPTION>";

} else {

echo "<OPTION value=$id>$name</OPTION>";

}

}

 

$this->query_fill->Free();

return $cnt;

 

}

 

// ReturnList does the same thing as FillList except that it returns the list

// as a string insted of echoing to the page.

function ReturnList ($sql, $selected=0)

{

 

$row = array();

$cnt = 0;

$this->query_return_list=new Query ($sql, $this->conn);

$result=$this->query_return_list->subExecute();

while ($row = mysql_fetch_array($result)) {

 

$id = $row[0];

$name = $row[1];

$cnt += 1;

if ($selected == $id) {

$data .= "<OPTION value=$id selected>$name</OPTION>";

} else {

$data .= "<OPTION value=$id>$name</OPTION>";

}

}

$this->query_return_list->Free();

return $data;

 

}

I want to implemetn this to my weeklyevent.php

and this is my originasl code without using the function FilList

<?php

  $query="SELECT firstname,id FROM people";

  $result = mysql_query ($query);

echo "<select name=people id='people' value=''>Location ID</option>";

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt

echo "<option value=$nt[id]>$nt[id]</option>";

/* Option values are added by looping through the array */

}

echo "</select>";// Closing of list box

  ?>

Please help me!

Why have two copies of the function, one of which echoes, the other that returns the data? Get rid of the FillList() function completely, just use ReturnList.

 

$query="SELECT id,firstname FROM people";
echo '<select name="people" id="people">';
echo ReturnList($query);
echo "</select>";

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.