Jump to content

Simple Problem! Not complex, well I don't think so!


stig1

Recommended Posts

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

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!

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

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.