954,576 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

Im having trouble to use this function FillList ($sql, $selected=0) from my Mysql Class.. I want to implement it to a dropdown box in my page

this is the full MysqlClass.php
<?php
// Read the database name for the site
//PostgreSQL-PHP
include "mysqldb.php";

class MySqlClass {

//this are the global variables of the class
//you can access this variables once you have instantiated a class in your code ex. $class = new MyClass();
//to access the class global variables is: $class->$classGlobalVariables;
var $conn;
var $stmt;
var $error = null; //, $persistent, $result_proc, $result_parse;
// var $query_return_list, $query_fill, $query_select_rows, $query_select_row, $query_select;
// var $query_doProc, $query, $first_row, $first_rowresult, $next_row;
var $bind = array();
var $fresh_row_xy;

function Login($userid, $password, $dbname, $host)
{
error_reporting (E_ALL);

$this->conn = mysql_connect($host,$userid,$password) or die("cant access to the database");
if (!$this->conn) {
echo $this->error="cannot access on the server";
} else {
mysql_select_db($dbname) or die("no database found");
}
//this is the now the class globalvariables that holds the connection in our database: $this->$conn
return ($this->conn);
}

function DatabaseHandle ()
{
return ($this->conn);
}


// Execute is intended to be used to execute any database update statement
// (insert, update, delete, or stored procedure call)
function Execute ($sql)
{
$result = mysql_query($sql,$this->conn);

if(!$result) {
$this->error = mysql_error($this->conn);
} else {
$result = mysql_affected_rows();
}
return $result;
}

// The parse, Bind and DoProc fucntions are intended for executing sql with bind variables
// In this case you need to parse the statement, bind each variable and then execute it
function Parse ($sql)
{
mysql_real_escape_string($sql);
}

function Bind ($stmt, $var, $data) //
{
//this is the class global variable that holds the data binding : $this->bind
//to get access with this binded data, first you have to instantiate a class like $class = new MyClass();

$this->bind['statement'] = $stmt;
$this->bind['variable'] = $var;
$this->bind['data'] = $data;

//second thier must be a submitted parameters to this class method: Bind()
//like $class->Bind(param1, param2, param3), then to get the binded data $class->bind['statement'],$class->bind['variable'],$class->bind['data']
return $this->bind;
}

function DoProc ($stmt)
{
$this->query_doProc=new Query ($sql, $this->conn);
$this->result_proc=$this->query_doProc->subExecute();
$this->query->Free();
return $this->result_proc;
}

// Select returns a single field
function Select($sql)
{
$result = mysql_query($sql, $this->conn);
$row = mysql_fetch_array($result, MYSQL_NUM);
return $row[0];
}

// SelectRow returns all of the records in the row as an array
function SelectRow ($sql)
{
/*
$this->query_select_row=new Query ($sql, $this->conn);
$result = $this->query_select_row->subExecute();
$row = mysql_fetch_array($result);
$this->query_select_row->Free();
*/
$result = mysql_query($sql, $this->conn);
$row = mysql_fetch_array($result, MYSQL_NUM);
return $row;
}

// SelectRows will return a list containing all of the rows selected.
// This query is intended for single field selections
function SelectRows ($sql)
{
$data = array();
$cnt = 0;
$this->query_select_rows=new Query ($sql, $this->conn);
$result=$this->query_select_rows->subExecute();

while ($row = mysql_fetch_array($result)) {
$data[$cnt]=$row[0];
$cnt += 1;
}
$this->query_select_rows->Free();
return $data;
}

// 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 "$name";
} else {
echo "$name";
}
}

$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 .= "$name";
} else {
$data .= "$name";
}
}
$this->query_return_list->Free();
return $data;

}

// SelectFirstRow will return all of the fields in a row as an array like SelectRow
// but it will leave the query open so that addition rows can be selected using SelectNextRow
// When thee are no more rows found the FALSE will be returned
function SelectFirstRow($sql)
{
$this->stmt = mysql_query($sql, $this->conn);
return $this->SelectNextRow ();
/*
$row = mysql_fetch_array($this->stmt, MYSQL_NUM);
if ($row == FALSE) {
mysql_free_result($this->stmt);
$this->stmt = FALSE;
}
return $row;
*/
}

function SelectNextRow ()
{
$row = mysql_fetch_array($this->stmt, MYSQL_ASSOC);
if ($row == FALSE) {
mysql_free_result($this->stmt);
$this->stmt = FALSE;
}
return $row;
}


function SelectAll($sql)
{
$list = array();
$row = $this->SelectFirstRow($sql);
while ($row) {
$list[] = $row;
$row = $this->SelectNextRow();
}
return $list;
}

function CloseDB()
{
mysql_close($this->conn); //close the connection to free the memory
}

function GetList($sql)
{
$data = mysql_query($sql) or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
$name = $info[0];

Print "$name";
}

}

function GetAllRows ($sql)
{
$data = array();
$cnt = 0;
/* $this->query_select_rows=new Query ($sql, $this->conn);
$result=$this->query_select_rows->subExecute(); */

$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$data[$cnt]=$row[0];
$cnt += 1;
}
/*$this->query_select_rows->Free(); */
return $data;
}

}

?>

and I want to implement or replace this by using the function in this code<?php
$query="SELECT firstname,id FROM people";
$result = mysql_query ($query);
echo "Location ID";
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "$nt[id]";
/* Option values are added by looping through the array */
}
echo "";// Closing of list box
?>

Please help me

jay_412
Newbie Poster
24 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 
<?php 
  $query="SELECT id,firstname FROM people";
  echo "<select name='people' id='people'>";
  FillList($query);
  echo "</select>";
?>
pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 
<?php 
  $query="SELECT id,firstname FROM people";
  echo "<select name='people' id='people'>";
  FillList($query);
  echo "</select>";
?>

this code doesnt function to my page..can you explain it more?

jay_412
Newbie Poster
24 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

what doesn't work, can you be more specific. Ow, perhaps you forgot to call it as a member function.

$obj = new MySqlClass;
  $query="SELECT id,firstname FROM people";
  echo "<select name='people' id='people'>";
  $obj->FillList($query);
  echo "</select>";


FillList is designed to output the option list for a dropdown based on a query. At least that's how it's coded.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

hi.by the way I appreciate your effort to my problem
at the top of my page this code
<?php
session_start();
include ('php/VineDB.php');
$vdb = new VineyardDB;
$conn = $vdb->Reconnect (DB_DATABASE, DB_HOST, 'index.php');
I replace the $obj to $vdb because this my new class declared but the query doesnt work again

jay_412
Newbie Poster
24 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

VineyardDB is completely different from what you posted above. There is no way to tell why it doesn't work.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

yah,i gues so,, thank you for giving me that idea,,

jay_412
Newbie Poster
24 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

hi guys,, thank you for your help.. I really appreciate it,

jay_412
Newbie Poster
24 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You