PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

populate a field if empty

  1. I need to populate every field on my form, even those fields that don't have any values submitted so that each record has a consistent number of rows in the database. What is the best way to do this?

    Posted 12 years ago on Thursday June 3, 2010 | Permalink
  2. This is a strange request, it is possible but you would have to do so by writing some custom PHP to populate the field values if the field is empty. How comfortable are you with PHP and working with WordPress API hooks?

    Posted 12 years ago on Thursday June 3, 2010 | Permalink
  3. Not that familiar with hooks, but I am comfortable with PHP. And maybe I'm approaching my problem from the wrong direction.

    What I'm trying to accomplish is to publicly display the form submissions in a row format. I've written a custom query that GROUP_CONCATs the value fields from one submission into one field and then I'm using PHP to explode that for my display. The problem I'm running into is that submissions have varying numbers of fields depending on what was submitted. I thought if I forced empty fields to have data, then I'd get consistent results when I'm building my rows.

    Is there a better way to approach the problem/need I'm having?

    Posted 12 years ago on Thursday June 3, 2010 | Permalink
  4. Here is an example of my custom query:

    $querystr = "
    SELECT wp_rg_lead.date_created,
    wp_rg_lead_detail.lead_id,
    wp_rg_lead_detail.form_id,

    GROUP_CONCAT(wp_rg_lead_detail.value ORDER BY wp_rg_lead_detail.field_number) AS resultsrow,
    wp_rg_lead.source_url,
    wp_rg_lead.user_agent

    FROM wp_rg_lead_detail INNER JOIN wp_rg_lead ON wp_rg_lead_detail.lead_id = wp_rg_lead.id
    WHERE ( wp_rg_lead_detail.form_id = '3' )
    GROUP BY wp_rg_lead_detail.lead_id
    ";

    Posted 12 years ago on Thursday June 3, 2010 | Permalink
  5. I believe I've solved my situation using a nested foreach statement. I've set up 2 queries, one that grabs the rows with lead_id, and one that loops through all rows where the the lead_id from query 1 matches the lead_id from query 2. This is probably a much better solution and doesn't require me to insert rows where there is no data.

    _________________________________________

    foreach ($myResultRows as $row):

    foreach ($myResultRows2 as $row2):

    if ($row->lead_id == $row2->lead_id ) {
    echo $row2->field_number . " | " . $row2->value . " | ";
    }

    endforeach;

    echo "

    ";

    endforeach;

    Posted 12 years ago on Friday June 4, 2010 | Permalink
  6. Glad you figured it our Brian!

    Posted 12 years ago on Friday June 4, 2010 | Permalink

This topic has been resolved and has been closed to new replies.