Chuck Le Butt
Chuck Le Butt

Reputation: 48758

PHP/MySQL: How to get multiple values from a PHP database method

Sorry for the incredibly newbie question, but I can see myself drifting into bad practices if I don't ask.

I have a PHP method that I want to return all the values of a given database column, in order to place the contents in a dropdown menu for a HTML form. I could obviously construct the whole HTML in the PHP method and return that as a string, but I imagine this is pretty bad practice.

Since PHP methods can only return one value, I imagine I'll need to call the method several times to populate the dropdown menu, or pass an array from the method.

What would be a good solution to this (presumably) common problem? Thanks.

Upvotes: 1

Views: 7099

Answers (5)

Totoro
Totoro

Reputation: 1294

You can have arrays in arrays, so if you have a table with several columns you could assign them to an array as separate arrays:

$all_results = array();
foreach($rowInDatabase as $key => $value){
    // each row will be an array with a key of column name and value of column content depending how you get the data from the DB.
    $colname = $key;
    $colVal = $value; //this is an array
    $all_results[$colname] = $colVal; //append the current row to the array
    }

}

code like this will populate your array with an array per row of the table so if there are ten rows and five columns you could get row 2 column 3 with $all_results[1][2]; (as they start from 0).

Upvotes: 0

BenOfTheNorth
BenOfTheNorth

Reputation: 2872

Not quite sure I understand what you want to do fully, but you could always pass the result back from the method, and then loop through it in your HTML.

Your method would be something like:

public function my_method()
{
     $result = $db->query($sql_here);
     return $result;
}

And then your HTML would be

<select>

<?

    $result = $class->my_method();

    while($row = $result->fetch_assoc())
    {
        echo '<option>'.$row['some_col'].'</option>';
    }

?>

</select>

Upvotes: -1

David Houde
David Houde

Reputation: 4778

There is no PHP function to do this, so you will have to form an array from the results.

$column = array()
$query = mysql_query("SELECT * FROM table ORDER BY id ASC");
while($row = mysql_fetch_array($query)){
    $column[] = $row[$key]
}

Then pass $column to your view(HTML)

foreach($column as $value)
{
   echo "<li>" . $value . "</li>";
}

Upvotes: 0

Iznogood
Iznogood

Reputation: 12843

Well, an array is one value, containing tons of other values. So just have your method return a array of results.

edit: as Hammerstein points out you could use objects but its as good/bad as arrays depending on context. Very similar.

Upvotes: 6

tbddeveloper
tbddeveloper

Reputation: 2447

You could use an object as your return type. So;

class MyReturnValue 
{
  public $Value1;
  public $Value2;
}

function getMyValues() 
{
  $results = GetDatabaseValues( ); // Assume this returns an associative array

  $result = new MyReturnValue();
  $result.Value1 = $results["Value1"];
  $result.Value2 = $results["Value2"];
}

Then in your code, you can refer to $result.Value1 etc.

Upvotes: 0

Related Questions