1

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.

1
  • 1
    Although your question indeed newbish and the problem indeed a very common one, there are 2 brilliant assumptions in your question making it not that bad: 1. only one person out of 1000 here have an idea of user-defined methods. Others mind PHP program as a plain linear solid block of code. 2. your bad practice remark is absolutely true. Commented Feb 20, 2012 at 13:56

5 Answers 5

6

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Sure. The is no other/better way to do it.
What about Ben Griffith's method?
Already edited my answer. But objects would be as good as arrays depending on context. Usually array's are plenty enough.
Sorry, I was referring to Ben's answer. Is that bad practice?
@Django Ben's method is just wrong. It is the same bad practice you referred to. there should be no SQL related code by the time uyou started output. ARRAY is what you were asking for. That's all. It is not a rocket science to discuss.
0

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.

1 Comment

he needs to iterate values and don't need methods
0

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>";
}

3 Comments

if you were wrapping this code in a function, your answer were the best.
Just a work of caution - loading result sets into an array and then processing it is fine for small numbers of records, but when you're dealing with large volumes you end up iterating through the resultset to build the array, consuming precious memory and then iterating through the array to build the html. The method does work quite nicely for small volumes like list boxes though - just my 2p worth..
Yep, this is pretty much what I've done, except I'm trying to use PDO.
0

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).

Comments

-1

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>

5 Comments

This is stated by the OP as a BAD PRACTICE. You better avoid it in your code.
I couldn't quite understand what he was asking. It sounded to me like he didn't want to construct the HTML inside the method, and that was the issue.
That's very true, Ben. I never explicitly stated this solution was bad practice, but the Col. is right... There should be no SQL where you've put it. Thanks, though.
Okay, it is me who stating this as a bad practice.
Lol, I just figured the bad practice he was referring to was having HTML generated inside the method. While you're here - what's really wrong with using the object instead of an array? (This is a genuine question - not being sarcastic)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.