scrubbicus Posted May 19, 2009 Share Posted May 19, 2009 So I have an e-mail form that I'm creating $form = new form('contact'); if(!isset($_POST['submit'])) { $form->displayForm(); } else { if($form->checkForm() == NULL || $form->checkForm() == true) { $form->displaySubmit(); } } So I have that to call the functions and I don't want to have to copy and paste everything in the function but within each of the functions is this loop. while($row = mysql_fetch_array($this->_results)) { } I first had this problem when I tried to generate the sent message but I fixed that by create an isset and !isset for both the form and the sent message. Now I'm trying to error check the form by looping through my table to get the $_POST form names then check if any of those are empty() and if nothing is empty then nothing gets returned false so displaySubmit() can be called. It does get to that but it doesn't generate the sent message because I'm guessing the checkForm() is already using that instance variable and looping it. Quote Link to comment https://forums.phpfreaks.com/topic/158798-im-unable-to-loop-the-same-variable-instance-twice/ Share on other sites More sharing options...
KevinM1 Posted May 19, 2009 Share Posted May 19, 2009 Show code. Quote Link to comment https://forums.phpfreaks.com/topic/158798-im-unable-to-loop-the-same-variable-instance-twice/#findComment-837528 Share on other sites More sharing options...
scrubbicus Posted May 19, 2009 Author Share Posted May 19, 2009 Okay here's the whole class <?php class form { private $_results; public function __construct($form) { $this->_results = mysql_query("SELECT * FROM forms WHERE module='$form' ORDER BY id ASC"); } public function displayForm() { $count = '1'; $num_cols = '2'; echo '<div id="div_form">'; echo '<form method="post" action="#">'; while($row = mysql_fetch_array($this->_results)) { echo '<div id="div_form" class="column'.$count.'">'; if($row['type'] == 'text') { echo '<div class="form">'; echo '<div class="form_header">'.ucwords($row['name']).'</div>'; echo '<div class="form_field"> <input type="text" name="'.strtolower($row['name']).'" maxlength="'.$row['maxlength'].'" /> </div>'; echo '</div>'; } elseif($row['type'] == 'select') { echo '<div class="form">'; echo '<div class="form_header">'.$row['name'].'</div>'; echo '<div class="form_field">'; echo '<select name="'.strtolower($row['name']).'">'; $parameters = explode(',',$row['parameters']); foreach($parameters as $i) { echo '<option name="'.$i.'">'.ucwords($i).'</option>'; } echo '</select></div></div>'; } elseif($row['type'] == 'textarea') { echo '<div class="form">'; echo '<div class="form_header">'.ucwords($row['name']).'</div>'; echo '<div class="form_field"> <textarea name="'.strtolower($row['name']).'"></textarea> </div>'; echo '</div>'; } if($count == $num_cols) { $count = '0'; } $count++; echo '</div>'; } echo '<div id="form" class="submit"> <input type="submit" name="submit" value="Send E-Mail" id="button" class="email" /> </div> </div>'; } public function checkForm() { while($row = mysql_fetch_array($this->_results)) { $value = $_POST[strtolower($row['name'])]; $field = strtolower($row['name']); if($field == 'category') { if($value == '-- Select One --') { die('<b>Please choose a category</b> <br /> Hit back on your browser.'); return false; } } elseif(empty($value)) { die('<b>Please fill out all required fields</b> <br /> Hit back on your browser.'); return false; } } } public function displaySubmit() { echo '<p class="header"> Thank You, '.$_POST['name'].' </p>'; echo '<p class="message">Thank you for submitting your e-mail. We will get back to you as soon as possible. You\'ll be receiving an auto-response. You may want to check your <b>spam</b> box it\'s been known to end up there.</p>'; echo '<p class="header"> Sent E-Mail Message </p>'; while($row = mysql_fetch_array($this->_results)) { echo '<b>'.ucwords($row['name']).':</b> '.htmlentities(mysql_real_escape_string($_POST[strtolower($row['name'])])).'<br />'; } } } Quote Link to comment https://forums.phpfreaks.com/topic/158798-im-unable-to-loop-the-same-variable-instance-twice/#findComment-837669 Share on other sites More sharing options...
waynew Posted May 19, 2009 Share Posted May 19, 2009 May God have mercy on our souls. Seriously though... what's the problem? Have you enabled PHP errors? Quote Link to comment https://forums.phpfreaks.com/topic/158798-im-unable-to-loop-the-same-variable-instance-twice/#findComment-837677 Share on other sites More sharing options...
scrubbicus Posted May 19, 2009 Author Share Posted May 19, 2009 I have enabled them but it's not giving me any errors it's just not looping through the displaySubmit() Forgive the PHP structure. It's my first try at trying to make a fully dynamic website. I'm trying to make the second version of my website to be kind of like a CMS. I figure developing one of those would throw a bunch of new challenges at me. Quote Link to comment https://forums.phpfreaks.com/topic/158798-im-unable-to-loop-the-same-variable-instance-twice/#findComment-837683 Share on other sites More sharing options...
sasa Posted May 20, 2009 Share Posted May 20, 2009 insert mysql_data_seek($this->_results, 0); before loop Quote Link to comment https://forums.phpfreaks.com/topic/158798-im-unable-to-loop-the-same-variable-instance-twice/#findComment-837855 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.