Jump to content
Larry Ullman's Book Forums

Search the Community

Showing results for tags 'arguments passed by reference'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • Single Editions
    • Modern Javascript: Develop and Design
    • The Yii Book
    • Effortless Flex 4 Development
    • Building a Web Site with Ajax: Visual QuickProject
    • Ruby: Visual QuickStart Guide
    • C++ Programming: Visual QuickStart Guide
    • C Programming: Visual QuickStart Guide
    • Adobe AIR: Visual QuickPro Guide
  • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (5th Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (4th Edition)
    • PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide (3rd Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (2nd Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (1st Edition)
  • PHP for the Web: Visual QuickStart Guide
    • PHP for the Web: Visual QuickStart Guide (5th Edition)
    • PHP for the Web: Visual QuickStart Guide (4th Edition)
    • PHP for the Web: Visual QuickStart Guide (3rd Edition)
    • PHP for the World Wide Web: Visual QuickStart Guide (2nd Edition)
    • PHP for the World Wide Web: Visual QuickStart Guide (1st Edition)
  • Effortless E-commerce with PHP and MySQL
    • Effortless E-Commerce with PHP and MySQL (2nd Edition)
    • Effortless E-Commerce with PHP and MySQL
  • PHP Advanced: Visual QuickPro Guide
    • PHP Advanced and Object-Oriented Programming: Visual QuickPro Guide (3rd Edition)
    • PHP 5 Advanced: Visual QuickPro Guide (2nd Edition)
    • PHP Advanced: Visual QuickPro Guide
  • MySQL: Visual QuickStart Guide
    • MySQL: Visual QuickStart Guide (2nd Edition)
    • MySQL: Visual QuickStart Guide (1st Edition)
  • Other
    • Announcements
    • Newsletter, Blog, and Other Topics
    • Forum Issues
    • Social

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


Found 1 result

  1. Hello, First, my very best wishes to everyone for this new year 2012. … And my first question for 2012! I'm trying to enlarge the create_form_input() function from chapter 4 so that it also takes care of labels. I've managed to do practically everything I want it to do, except that I would like the $label_text argument to have a default value, and I haven't found a solution yet. The idea is that the label text would be based on the $name variable in this way: $label_text = ucwords(str_replace('_', ' ', $name)); If there was only English to be taken into account, I could just hardcode it into the function. There would be no $label_text parameter, and I would just write: <label for="'.$name.'">'.ucwords(str_replace('_', ' ', $name)).'</label> so that if the value of $name is 'first_name', the label text is 'First Name'. But, since I want this function to handle the label text whatever language is used, I need a $label_text argument. I first thought of making ucwords(str_replace('_', ' ', $name)) the default value for the $label_text parameter, but I don't know how to achieve this since the PHP manual states that "The default value must be a constant expression, not (for example) a variable, a class member or a function call." I then thought of passing this function parameter by reference, and the beginning of the function now looks like this: function create_form_input2($name, $type, $size_rows, $maxlength_cols, $label_class, &$label_text, $input_class, $errors, $span_class='') { $value = false; if (isset($_POST[$name])) $value = $_POST[$name]; $label_text = ucwords(str_replace('_', ' ', $name)); … It works if I keep the variable for this parameter: create_form_input2('first_name', 'text', '30', '30', 'left', $label_text, 'margin_left', $erreurs); This is what I get: But if I try using a string, I get the following error message: Fatal error: Cannot pass parameter 6 by reference in …ecom1/index.php5 on line 27 Line 27 being: create_form_input2('first_name', 'text', '30', '30', 'above', 'Prénom', 'below', $erreurs); which means that I still don't understand "references" . If someone could help me find a solution, I'd be really grateful. Below: first, a few CSS classes I created so that I could either have the label and the input on the same line (as on the screen shot above), or the input below the label. Then, my attempt at rewriting the function. Then the form. ############### # CSS classes # ############### form label.left, form span.left { position: absolute; width: 8em; font-size: 1em; text-align: right; } form input.margin_left, form textarea.margin_left { margin-left: 8.7em; text-align: left; } form label.above, form input.below, form textarea.below, form span.above { display: block; } ####################### # The function itself # ####################### function create_form_input2($name, $type, $size_rows, $maxlength_cols, $label_class, &$label_text, $input_class, $errors, $span_class='') { $value = false; if (isset($_POST[$name])) $value = $_POST[$name]; // Label text based on $name. $label_text = ucwords(str_replace('_', ' ', $name)); if ($value && get_magic_quotes_gpc()) $value = stripslashes($value); // Check the input type. if (($type == 'text') || ($type == 'password')) { // Create the input. echo '<p><label class="'.$label_class.'" for="'.$name.'"> '.$label_text.'</label>'; echo '<input type="'.$type.'" name="'.$name.'" id="'.$name.'" size="'.$size_rows.'" maxlength="'.$maxlength_cols.'" '; // Add the input's value, if applicable. if ($value) echo 'value="'.htmlspecialchars($value).'"'; echo ' class="'.$input_class; // Check for an error. if (array_key_exists($name, $errors)) { echo ' error" /> <span class="error '.$span_class.'">' .$errors[$name].'</span></p>'; } else { echo '" /></p>'; } } // Check if the input type is a textarea. elseif ($type == 'textarea') { echo '<p><label class="'.$label_class.'" for="'.$name.'">' .$label_text.'</label>'; // Display the error first, so that it's more obvious. if (array_key_exists($name, $errors)) echo '<br /> <span class="error '.$span_class.'">'.$errors[$name].'</span>'; // Create the textarea. echo '<textarea name="'.$name.'" id="'.$name.'" rows="'.$size_rows.'" cols="'.$maxlength_cols.'" class="'.$input_class; // Add the 'error' class, if applicable. if (array_key_exists($name, $errors)) { echo ' error">'; } else { echo '">'; } // Add the value to the textarea. if ($value) echo $value; echo '<textarea></p>'; } // End of primary IF-ELSE. } // End of the create_form_input() function. ######## # Form # ######## <form method="post" action="" accept-charset="utf-8"> <?php $erreurs = array ('first_name' => 'Please enter your first name.', 'last_name' => 'Please enter your last name.', 'comment' => 'Please enter your comment.'); // The first three function calls work and produce the screen shot above. create_form_input2('first_name', 'text', '30', '30', 'left', $label_text, 'margin_left', $erreurs); create_form_input2('last_name', 'text', '30', '30', 'left', $label_text, 'margin_left', $erreurs); create_form_input2('comment', 'textarea', '10', '70', 'left', $label_text, 'margin_left', $erreurs, 'left'); // This is what triggers an error message: create_form_input2('first_name', 'text', '30', '30', 'above', 'Prénom', 'below', $erreurs); create_form_input2('last_name', 'text', '30', '30', 'above', 'Nom', 'below', $erreurs); create_form_input2('comment', 'textarea', '10', '70', 'above', 'Échos', 'below', $erreurs, 'above'); ?> <input type="submit" name="submit" value="Submit!" class="formbutton clear" /> </form> With thanks for your help,
×
×
  • Create New...