0

I have this php function which looks up rows from a database table

if (!function_exists("ContactLookup")) {
    function ContactLookup($column, $clause, $value, $limit1=0, $limit2=1)
    {
        global $conn;
        $ContactLookup_Sql= "select * from contacts where $column $clause $value LIMIT $limit1, $limit2";
        $ContactLookup_Rs = mysql_query($ContactLookup_Sql,$conn);
        while ($ContactLookup_Result = mysql_fetch_array($ContactLookup_Rs)) {
            $foremame =$ContactLookup_Result["forename"];
            $surname = $ContactLookup_Result["surname"];
        }
    }
}

This just echoes the results but if I wanted to put the results into a select element how would I do this

Within the while loop would I create the variables then echo the variables when I call the function? Or is there another way around this?

Like:

<?php ContactLookup("company_sequence", "=", $result["contact"],"0","10"); ?>
><select name="contactsequence" id="contactsequence">
    <option value=""><?php echo $forename; ?></option>
        </select
5
  • Functions typically should only return or set data, not echo it. Did you try the code you showed as an example? What is the problem? Commented Dec 31, 2013 at 0:07
  • From my above example I just get an empty select box. Check my edit, I created them as variables and not echoed them Commented Dec 31, 2013 at 0:08
  • Your function doesn't return anything Commented Dec 31, 2013 at 0:09
  • Should I do return $forename = $ContactLookup_Result... ? Commented Dec 31, 2013 at 0:10
  • You need to set and return an array, because from how you're calling that function, it could return up to 10 name pairs. Commented Dec 31, 2013 at 0:29

2 Answers 2

1

Make your function return an array

function ContactLookup ($column, $clause, $value, $limit1=0, $limit2=1) {
    global $conn;
    $ContactLookup_Sql= "select * from contacts where $column $clause $value LIMIT $limit1, $limit2";
    $ContactLookup_Rs = mysql_query($ContactLookup_Sql,$conn);

    $results = [];
    while($ContactLookup_Result = mysql_fetch_array($ContactLookup_Rs))
    {
        $results[] = [
           'forename'=> $ContactLookup_Result["forename"], 
           'surname'=> $ContactLookup_Result["surname"]
        ];
    }
    return $results;
}

Then you can loop through it:

<?php
$names = ContactLookup("company_sequence", "=", $result["contact"],"0","10"); 
echo '<select name="contactsequence" id="contactsequence">';
foreach($names AS $name){
   echo '<option value="">'.$name['forename'].'</option>';
}
echo '</select>';
Sign up to request clarification or add additional context in comments.

3 Comments

This is also just returning one row
Then your query only returns one row. Print out the query and run it in MySQL manually, what do you get?
Try a var_dump on $results do you see more than one result?
1

Change the function to return the results as a variable, rather than echo them.

function ContactLookup ($column, $clause, $value, $limit1=0, $limit2=1) {
    global $conn;
    $ContactLookup_Sql= "select * from contacts where $column $clause $value LIMIT $limit1, $limit2";
    $ContactLookup_Rs = mysql_query($ContactLookup_Sql,$conn);
    $results=array();
    while($ContactLookup_Result = mysql_fetch_array($ContactLookup_Rs))
    {
        $results[] = array('forename'=>$ContactLookup_Result["forename"],'surname'=>$ContactLookup_Result["surname"];
    }
    return $results;
}

Then in your display loop,

<?php $contacts=ContactLookup("company_sequence", "=", $result["contact"],"0","10"); ?>
<select name="contactsequence" id="contactsequence">
    foreach($contacts as $k=>$contact){?> 
       <option value="<?php echo $k;?>"><?php echo $contact['forename']; ?></option>
        <?php 
        }
   </select>

1 Comment

This is just returning one row from the database in my select element

Your Answer

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