• Resolved paolofrulio

    (@paolofrulio)


    Hello and thank you for this great plugin.

    I have a question about dynamically populating a select with data retrieved from the database.

    After a series of queries, I arrive at the table where I find the user’s name, surname and email.

    The purpose is to populate the select with something like this

    <option value="'.$email.'">'.$first_name.' '.$last_name.'</option>'

    This is my code

    $result = $wpdb->get_results("SELECT * FROM mytable WHERE id = $customer_id");
                    foreach($result as $row) 
                    {		
                    $first_name = $row->firstName;
                    $last_name = $row->lastName;
                    $email .=  $row->email;
    		return '<option value="'.$email.'">'.$first_name.' '.$last_name.'</option>';
                    }

    But with return i’ll get only one result, how can i get and populate select with alle result?

    Thank you

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Tessa (they/them), AuRise Creative

    (@tessawatkinsllc)

    Capture the string from the foreach loop in a single variable and then return that, like this:

    $options = ''; // String variable to return
    $result = $wpdb->get_results("SELECT * FROM mytable WHERE id = $customer_id");
    foreach ($result as $row) {
        $first_name = $row->firstName;
        $last_name = $row->lastName;
        $email .=  $row->email;
        $options .= '<option value="' . $email . '">' . $first_name . ' ' . $last_name . '</option>'; // Append option HTML to the $options variable
    }
    return $options; // Return $options after the loop

    For security purposes, I’d also highly recommend using sanitizing and escaping functions. Alternatively, if you return a JSON encoded array, DTX can do that for you (see documentation).

    Thread Starter paolofrulio

    (@paolofrulio)

    Hello and thanks for you answer.

    I have try your code, but it return and populate the select with only 1 result.
    (If i try to echo instead return, the script give 2 result, so i am sure to have more then 1 result).

    Thank you

    Plugin Author Tessa (they/them), AuRise Creative

    (@tessawatkinsllc)

    Instead of using echo, can you use var_dump($row) inside the foreach loop to check how many times it’s looping? I sometimes wrap it in <pre> element tags to display it better like:

    $options = ''; // String variable to return
    $result = $wpdb->get_results("SELECT * FROM mytable WHERE id = $customer_id");
    foreach ($result as $row) {
        echo '<pre>'; var_dump($row); echo '</pre>';
        $first_name = $row->firstName;
        $last_name = $row->lastName;
        $email .=  $row->email;
        $options .= '<option value="' . $email . '">' . $first_name . ' ' . $last_name . '</option>'; // Append option HTML to the $options variable
    }
    return $options; // Return $options after the loop

    If it’s looping correctly, you should see both objects on the page and their values.

    The only other thing I can think of is maybe the email address is empty for one of your entries?

    And not that it really should make a difference, but I tend to write the values in SQL strings surrounded by double quotes, in case for some reason there’s spaces in whatever I’m using in the where clause, but again, this part is all preference and shouldn’t really affect how you’re making the request.

    $where = sprintf('id="%s"', esc_attr($customer_id));
    $sql = "SELECT * FROM mytable WHERE {$where}";
    $rows = $wpdb->get_results($sql);
    if(is_array($rows)) {
        foreach($rows as $row) {
            echo('<pre>'); var_dump($row); echo('</pre>');
        }
    }
    Plugin Author Tessa (they/them), AuRise Creative

    (@tessawatkinsllc)

    Hello, I’m marking this as resolved as I haven’t heard back from you in a while. If the issue still exists, please create a new one. Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Retrieve data from db and show in dynamic select’ is closed to new replies.