dennismonsewicz Posted August 13, 2009 Share Posted August 13, 2009 I have the following foreach statement: $modOne = $_POST['modelone']; $modTwo = $_POST['modeltwo']; $modThree = $_POST['modelthree']; $array = Array($modOne, $modTwo , $modThree); foreach($array as $r) { $man_db =& JFactory::getDBO(); $man_db->setQuery("SELECT * FROM tblName WHERE model = '$r'"); $man_rows = $man_db->loadObjectList(); foreach($man_rows as $row) { $deduct = $row->deductible; if($deduct == '89.99') { $prem = '6.00'; } elseif($deduct == '39.99') { $prem = '4.49'; } } } It is only hitting the first variable in the array... how can I make this go through each array var? Quote Link to comment Share on other sites More sharing options...
jake2891 Posted August 13, 2009 Share Posted August 13, 2009 do your second and thirds posts contain data? if not the array will only have one value to loop through. Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted August 13, 2009 Author Share Posted August 13, 2009 yeah they do... I did a print_r on the array and the post data shows up Quote Link to comment Share on other sites More sharing options...
jake2891 Posted August 13, 2009 Share Posted August 13, 2009 can you show me the print out Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted August 13, 2009 Author Share Posted August 13, 2009 Array ( [0] => C211PTT [1] => C211RED [2] => C211PTT ) Quote Link to comment Share on other sites More sharing options...
jake2891 Posted August 13, 2009 Share Posted August 13, 2009 thanks, can you give me a print_r of $man_rows Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted August 13, 2009 Author Share Posted August 13, 2009 Array ( [0] => stdClass Object ( [rowid] => 2 [manufacturer] => AUDIOVOX [model] => C211PTT [deductible] => 89.99 [published] => 1 ) Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted August 13, 2009 Author Share Posted August 13, 2009 for some reason its stopping at the last post entry... Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted August 13, 2009 Share Posted August 13, 2009 ok, so exactly how many times does your foreach run. it should be 3 times right? is it running 3 times, 2 times, once? What happens when it runs, and what do you expect to happen? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted August 13, 2009 Author Share Posted August 13, 2009 ok... I have some post variables that are being passed to the page... $modOne = $_POST['modelone']; $modTwo = $_POST['modeltwo']; $modThree = $_POST['modelthree']; $array = array($modOne, $modTwo, $modThree); and the foreach should then iterate through each value and give something back out of the SQL DB foreach($array as $r) { $man_db = JFactory::getDBO(); $v = mysql_real_escape_string($r); $man_db->setQuery("SELECT * FROM tblName WHERE model = '$v'"); but it is only returning results from the very last entry in the Array ($array) Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted August 13, 2009 Share Posted August 13, 2009 im assuming that the results returned you are talking about are stored in the variable $prem? that seems to be the only variable that gets any data stored after the initial query in the first foreach loop. if that is true, than that is your problem. You will need to store the data from each loop in an array. The variable will get overwritten with new data every time until the last iteration, and that is why the data from the last entry in the array is the only data you are seeing. Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted August 13, 2009 Author Share Posted August 13, 2009 hmmm... ok, can we step back and disect a little bit? I think I may not be following here is an original foreach loop: $modOne = $_POST['modelone']; $modTwo = $_POST['modeltwo']; $modThree = $_POST['modelthree']; $array = array($modOne, $modTwo, $modThree); foreach($array as $r) { $man_db = JFactory::getDBO(); $v = mysql_real_escape_string($r); $man_db->setQuery("SELECT * FROM tblName WHERE model = '$v'"); $man_rows = $man_db->loadObjectList(); } as you can see I have each $_POST var set to another variable and then all of those are captured in an array, but when I do a print_r on the man_rows I only get the results from the very last entry in the array. Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted August 13, 2009 Share Posted August 13, 2009 when do you do print_r. if its after the foreach then the variables will have been overwritten each time the foreach runs, and will only have the results from the last entry in the array Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted August 13, 2009 Share Posted August 13, 2009 as you can see I have each $_POST var set to another variable and then all of those are captured in an array, but when I do a print_r on the man_rows I only get the results from the very last entry in the array. You're not pushing values onto the man_rows array. Instead, you're overwriting the value stored in man_rows because you're using simple assignment. Try instead: $man_rows[] = $man_db->loadObjectList(); Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted August 13, 2009 Author Share Posted August 13, 2009 ok so I did that and now I get nothing $modOne = $_POST['modelone']; $modTwo = $_POST['modeltwo']; $modThree = $_POST['modelthree']; $array = array($modOne, $modTwo, $modThree); foreach($array as $r) { $man_db = JFactory::getDBO(); $v = mysql_real_escape_string($r); $man_db->setQuery("SELECT * FROM tblName WHERE model = '$v'"); $man_rows[] = $man_db->loadObjectList(); foreach($man_rows as $row) { $deduct = $row->deductible; if($deduct == '89.99') { $prem = '6.00'; } elseif($deduct == '39.99') { $prem = '4.49'; } } } Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted August 13, 2009 Share Posted August 13, 2009 manrows is an array, not an object, and you are treating it as an object in the second foreach $deduct = $row->deductible; should probably be something like $deduct = $row['deductible']; assuming that $row is an associative array Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted August 13, 2009 Share Posted August 13, 2009 manrows is an array, not an object, and you are treating it as an object in the second foreach $deduct = $row->deductible; should probably be something like $deduct = $row['deductible']; assuming that $row is an associative array Good catch. If that doesn't fix it, print_r on man_rows, just so we can see if its being filled. Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted August 13, 2009 Author Share Posted August 13, 2009 this is the output from $man_rows[]: Array ( [0] => Array ( [0] => stdClass Object ( [rowid] => 11 [manufacturer] => VERIZON WIRELESS [model] => XV6900 [deductible] => 89.99 [published] => 1 ) ) [1] => Array ( ) [2] => Array ( ) ) Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted August 13, 2009 Author Share Posted August 13, 2009 updated code: $modOne = $_POST['modelone']; $modTwo = $_POST['modeltwo']; $modThree = $_POST['modelthree']; $array = array($modOne, $modTwo, $modThree); foreach($array as $r) { $man_db = JFactory::getDBO(); $v = mysql_real_escape_string($r); $man_db->setQuery("SELECT * FROM tblName WHERE model = '$v'"); $man_rows[] = $man_db->loadObjectList(); foreach($man_rows as $row) { $deduct = $row['deductible']; if($deduct == '89.99') { $prem = '6.00'; } elseif($deduct == '39.99') { $prem = '4.49'; } } } Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted August 13, 2009 Share Posted August 13, 2009 is that output what you expect? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted August 13, 2009 Author Share Posted August 13, 2009 the output? yes... but its not echoing out for some reason Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted August 13, 2009 Share Posted August 13, 2009 thats because you have no echo statements in your code... or at least not any that I can see Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted August 13, 2009 Author Share Posted August 13, 2009 this is the whole script (well almost ALL of it) $modOne = $_POST['modelone']; $modTwo = $_POST['modeltwo']; $modThree = $_POST['modelthree']; $array = array($modOne, $modTwo, $modThree); foreach($array as $r) { $man_db = JFactory::getDBO(); $v = mysql_real_escape_string($r); $man_db->setQuery("SELECT * FROM tblName WHERE model = '$v'"); $man_rows[] = $man_db->loadObjectList(); foreach($man_rows as $row) { $deduct = $row['deductible']; if($deduct == '89.99') { $prem = '6.00'; } elseif($deduct == '39.99') { $prem = '4.49'; } } } $buildTable = '<tr>'; $buildTable .= '<th>Wireless Phone Number:</th>'; $buildTable .= '<th>Premium:</th>'; $buildTable .= '<th>Deductible:</th>'; $buildTable .= '</tr>'; $buildTable .= '<tr>'; $buildTable .= '<td>' . $numOne . '</td>'; $buildTable .= '<td>' . $prem . '</td>'; $buildTable .= '<td>' . $deduct . '</td>'; $buildTable .= '</tr>'; if($numTwo && !empty($numTwo)) { $buildTable .= '<tr>'; $buildTable .= '<td>' . $numTwo . '</td>'; $buildTable .= '<td>' . $prem . '</td>'; $buildTable .= '<td>' . $deduct . '</td>'; $buildTable .= '</tr>'; } if($numThree && !empty($numThree)) { $buildTable .= '<tr>'; $buildTable .= '<td>' . $numThree . '</td>'; $buildTable .= '<td>' . $prem . '</td>'; $buildTable .= '<td>' . $deduct . '</td>'; $buildTable .= '</tr>'; } I am echoing out $buildTable later on down the page Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted August 13, 2009 Share Posted August 13, 2009 Where are your <table> tags? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted August 13, 2009 Author Share Posted August 13, 2009 they are down in the HTML part of the script... So the table is like this: <table width="600" cellpadding="5" cellspacing="0" rules="all" frame="box" border="1" id="resultsBox"> <?=$buildTable;?> </table> Quote Link to comment 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.