Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/170125-solved-array-help/
Share on other sites

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)

Link to comment
https://forums.phpfreaks.com/topic/170125-solved-array-help/#findComment-897454
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/170125-solved-array-help/#findComment-897463
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/170125-solved-array-help/#findComment-897476
Share on other sites

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();

Link to comment
https://forums.phpfreaks.com/topic/170125-solved-array-help/#findComment-897482
Share on other sites

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';
				}
		}
}

Link to comment
https://forums.phpfreaks.com/topic/170125-solved-array-help/#findComment-897497
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/170125-solved-array-help/#findComment-897504
Share on other sites

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 ( ) ) 

Link to comment
https://forums.phpfreaks.com/topic/170125-solved-array-help/#findComment-897505
Share on other sites

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';
				}
		}
}

Link to comment
https://forums.phpfreaks.com/topic/170125-solved-array-help/#findComment-897509
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/170125-solved-array-help/#findComment-897531
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.