stig1 Posted September 18, 2008 Share Posted September 18, 2008 Having a blank at the moment people. Wondering if someone could help me out here. Currently I have the following table built in mysql id name percent select 1 priceA 305 1 2 priceB 263 0 3 priceC 225 1 4 priceD 208.8 0 5 priceE 176.2 1 I run a query to only select the rows that have got select = 1 How do I put each row data into a different varaible name or extract the data out of the array I get from mysql_fetch_array function. I need to use each different percent value return outside of the while loop statement, that has 1 selected to build prices on the website. for example: $marginsSQL = "SELECT * FROM tbl_margins WHERE select = 1"; $marginsResult = mysql_query($marginsSQL) or die (mysql_error()); while ($margins = mysql_fetch_array($marginsResult)){ $percent = $products["percent"]; } Any solutions? Having a very big blank at the moment. Thanks guys Link to comment https://forums.phpfreaks.com/topic/124752-simple-problem-not-complex-well-i-dont-think-so/ Share on other sites More sharing options...
peranha Posted September 18, 2008 Share Posted September 18, 2008 Chang this line $percent = $products["percent"]; to this $percent = $margins["percent"]; Link to comment https://forums.phpfreaks.com/topic/124752-simple-problem-not-complex-well-i-dont-think-so/#findComment-644364 Share on other sites More sharing options...
stig1 Posted September 18, 2008 Author Share Posted September 18, 2008 I already changed that line! I made a typo. If I run echo inside the while they all print out correctly. However outside the while statement, I would like to be able to put each of the return records into a variable of there own. Eg MarginOne = $percent; // First returned value in the sql statement MarginTwo = $percent; // Second returned value in the sql statement MarginThree = $percent; // Third returned value in the sql statement All of them return the same value. I require them to be different! Link to comment https://forums.phpfreaks.com/topic/124752-simple-problem-not-complex-well-i-dont-think-so/#findComment-644372 Share on other sites More sharing options...
F1Fan Posted September 18, 2008 Share Posted September 18, 2008 Make $percent an array. $marginsSQL = "SELECT * FROM tbl_margins WHERE select = 1"; $marginsResult = mysql_query($marginsSQL) or die (mysql_error()); $percent = array(); while ($margins = mysql_fetch_array($marginsResult)){ $percent[] = $products["percent"]; } print_r($percent); Link to comment https://forums.phpfreaks.com/topic/124752-simple-problem-not-complex-well-i-dont-think-so/#findComment-644377 Share on other sites More sharing options...
stig1 Posted September 18, 2008 Author Share Posted September 18, 2008 Does that mean all i would have to do is type the following? $marginA = $percent[0]; $marginB = $percent[1]; $marginC = $percent[2]; Link to comment https://forums.phpfreaks.com/topic/124752-simple-problem-not-complex-well-i-dont-think-so/#findComment-644458 Share on other sites More sharing options...
F1Fan Posted September 18, 2008 Share Posted September 18, 2008 Yup. Link to comment https://forums.phpfreaks.com/topic/124752-simple-problem-not-complex-well-i-dont-think-so/#findComment-644461 Share on other sites More sharing options...
stig1 Posted September 22, 2008 Author Share Posted September 22, 2008 I have created the following function. // Default Margins function margins() { $margins = array(); $mSQL = "SELECT mMargin FROM tbl_margins WHERE mSelect = 1"; $mResult = mysql_query($mSQL) or die(mysql_error()); while ($m = mysql_fetch_array($mResult)) { $margins[] = $m["mMargin"]; } // print_r($margins); $mSingle = $margins[0]; $mBulk = $margins[1]; $mPallet = $margins[2]; } The function returns the correct values. However when i call the function margins(); and then try to echo out $mSingle, $mBulk, $mPallet there is no value defined. Any solutions? Link to comment https://forums.phpfreaks.com/topic/124752-simple-problem-not-complex-well-i-dont-think-so/#findComment-647388 Share on other sites More sharing options...
genericnumber1 Posted September 22, 2008 Share Posted September 22, 2008 when you use a function you have to return the values as they're in a different scope. http://us2.php.net/variables.scope function margins() { // ... return $margins; } list($mSingle, $mBulk, $mPallet) = margins(); Link to comment https://forums.phpfreaks.com/topic/124752-simple-problem-not-complex-well-i-dont-think-so/#findComment-647389 Share on other sites More sharing options...
stig1 Posted September 22, 2008 Author Share Posted September 22, 2008 Thanks! Works a treat Link to comment https://forums.phpfreaks.com/topic/124752-simple-problem-not-complex-well-i-dont-think-so/#findComment-647396 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.