0
$resultUpdate = Nemesis::select("*", $table, "id = '{$id}'");
if (!$resultUpdate) {
    self::show_error(QUERY_ERROR);
} elseif ($resultUpdate->num_rows > 0) {
    $out .= '<div class="form-desc">' . $formDesc . '</div>';
} else {
    self::show_error(QUERY_EMPTY);
}
$array = array_values($array);
print_r($array);
$out .= '<form action="' . $_SERVER['PHP_SELF'] . '?id=' . $id . '&table=' . $table . '" method="post" class="form-horizontal" ' . $formAppend . '>';
while ($row = $resultUpdate->fetch_assoc()) {
    foreach ($row as $fieldname => $value) {
        if (in_array($fieldname, $array)) {
            $out .= generateInputField($fieldname, $value);
        }
    }
    foreach ($row as $fieldname => $value) {
        if (in_array($fieldname, $array)) {
            $out .= generateTextarea($fieldname, $value, $cke);
        }
    }
    foreach ($row as $fieldname => $value) {
        if (in_array($fieldname, $array)) {
            $out .= generateImgField($fieldname, $value);
        }
    }
}
$arr = array("last_modified"=>"input", "published"=>"input", "content"=>"textarea");
echo $automate->createArrayForm('projects', 'update', 'Some form desc', '178514825', $arr, true);

Right now all fields are outputting in every foreach when only inputs should output in the generateInputField section for example. I know this is because I need to check if the fieldtype (input, textarea) key matches with one of the values marked as input or textarea for values of the $fieldname. But I am not sure how.

I am pretty sure I have to filter the array so only values with input go into a separate array like arrayInput in which I can use as the second argument in in_array.

9
  • Does the input or textarea come from user input or are you generating it?
    – stackErr
    Commented Aug 6, 2013 at 17:46
  • it comes from the array as seen $arr
    – Alex
    Commented Aug 6, 2013 at 17:47
  • Can you try and explain it better? its really hard to understand what you are trying to do
    – stackErr
    Commented Aug 6, 2013 at 17:50
  • ok so basically i want to use the array to generate form fields, textarea, input .etc... from there i want to match the values from a database to the fields. which i have already done. but the issue is that i dont know how to make sure only array values of input from $arr are matched with generateInput .etc
    – Alex
    Commented Aug 6, 2013 at 17:52
  • Didn't you just ask the same question about this today? I was sure you accepted an answer. Commented Aug 6, 2013 at 18:05

2 Answers 2

1

If I understand you and your code correctly...

This code

while ($row = $resultUpdate->fetch_assoc()) {
    foreach ($row as $fieldname => $value) {
        if (in_array($fieldname, $array)) {

will always return true for all your rows thats why you are getting your current output.

Instead you should be doing this:

while ($row = $resultUpdate->fetch_assoc()) {
    foreach ($row as $fieldname => $value) {
        if ($fieldname == 'input') {
            $out .= generateInputField($fieldname, $value);
        } elseif($fieldname == 'textarea') {
            $out .= generateTextarea($fieldname, $value, $cke);
        } elseif ($fieldname == 'img') {
            $out .= generateImgField($fieldname, $value);
        }
        else{ $out = $out;}
    }
}
0

With alot of help from stackers...

/** 
 * Create Form With Array
 * 
 * Creates a form based on an array. If $do == update, we match fieldnames with values
 * 
 * @param string $table name of database table
 * @param string $do whether the form is an insert or update
 * @param string $formDesc form description to be echoed
 * @param array $array associative array of type (keys), and fieldnames (values)
 * @param bool $markFields whether or not to add headers for inputs, textareas .etc during insert
 * @param bool $formBrackets whether or not to prepend and append form brackets
 * @return $out html form
 *
 */
public function createArrayForm($table, $do, $formDesc = '', $id, $array, $markFields = false, $formBrackets = true) {
    if (!isset($table) && !isset($do)) {
        self::show_error('One or more parameters are missing in ' . __FUNCTION__);
    } elseif ($table == 'update' && !isset($id)) {
        self::show_error('For this form to be built, and ID must be set. Missing parameter `ID` in ' . __FUNCTION__);
    }
    if (!is_array($array)) {
        self::show_error('For this form to be built, an array must be given. Missing parameter `array` in ' . __FUNCTION__);
    }
    $result = array();
    // create two dimensional array to preserve keys that
    // otherwise would be lost with array_flip
    foreach($array as $k => $v) {
        if (array_key_exists($v, $result)) {
            $result[$v][] = $k;
        } else {
            $result[$v] = array($k);        
        }
    }
    // make sure we do not have any duplicates
    $result = super_unique($result);
    // we just need the array_values for matching with in_array
    // however we do not want to run array_values on null
    // so we check to see if the $result is a valid array first
    // if not, we just output a blank array so in_array doesn't complain
    $arrayInput = is_array($result['input']) ?  array_values($result['input']) : array();
    $arrayTextarea = is_array($result['textarea']) ?  array_values($result['textarea']) : array();
    $arrayImages = is_array($result['images']) ?  array_values($result['images']) : array();
    $out = $formBrackets == true ? '<form action="' . $_SERVER['PHP_SELF'] . '?id=' . $id . '&table=' . $table . '" method="post" class="form-horizontal" ' . $formAppend . '>' : NULL;
    if($do == 'insert') {
        $out .= isset($formDesc) ? '<div class="form-desc">' . $formDesc . '</div>' : NULL;
        $out .= $markFields && in_array('input', $array) ? '<h3>Input Fields</h3>' : NULL;
        foreach ($arrayInput as $fieldname) {
            $out .= generateInputField($fieldname);
        }
        $out .= $markFields && in_array('textarea', $array) ? '<h3>Content Fields</h3>' : NULL;
        foreach ($arrayTextarea as $fieldname) {
            $out .= generateTextarea($fieldname, $cke);
        }
        $out .= $markFields && in_array('image', $array) ? '<h3>Images Fields</h3>' : NULL;
        foreach ($arrayImages as $fieldname) {
            $out .= generateImgField($fieldname);
        }
    } elseif ($do == 'update') {
        $resultUpdate = Nemesis::select("*", $table, "id = '{$id}'");
        if (!$resultUpdate) {
            self::show_error(QUERY_ERROR);
        } elseif ($resultUpdate->num_rows > 0) {
            $out .= isset($formDesc) ? '<div class="form-desc">' . $formDesc . '</div>' : NULL;
        } else {
            self::show_error(QUERY_EMPTY);
        }
        while ($row = $resultUpdate->fetch_assoc()) {
            foreach ($row as $fieldname => $value) {
                if (in_array($fieldname, $arrayInput)) {
                    $out .= generateInputField($fieldname, $value);
                }
            }
            foreach ($row as $fieldname => $value) {
                if (in_array($fieldname, $arrayTextarea)) {
                    $out .= generateTextarea($fieldname, $value, $cke);
                }
            }
            foreach ($row as $fieldname => $value) {
                if (in_array($fieldname, $arrayImages)) {
                    $out .= generateImgField($fieldname, $value);
                }
            }
        }
    } else {
        self::show_error('Missing array or `do` argument in function ' . __FUNCTION__);
    }
    $out .= form_hidden('user_data', '1');
    $out .= form_hidden('id', $do == 'update' ? $id : self::generateID());
    $out .= $formBrackets == true ? form_close() : NULL;
    return $out;
}

Usage:

$arr = array("last_modified"=>"input", "published"=>"input", "project_content"=>"textarea", "project_content"=>"textarea");
echo $automate->createArrayForm('projects', 'insert', 'Some form desc', '123', $arr, true);
echo $automate->createArrayForm('projects', 'update', 'Some form desc', '178514825', $arr, true);

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.