Jump to content

Grabbing Values From An Array


dzhax
 Share

Recommended Posts

I am working on a javascript and I know php is server side so I am trying to bring in the whole array then choose from it clientside.

function findlvl(expnow){	val2 = parseInt(expnow.value, 10);	if (isNaN(val))				return 'Unknown';	<?php 	$flvl2 = mysql_query("SELECT * FROM exptable");	if(mysql_num_rows($flvl2) > 0) {		while($f2 = mysql_fetch_array($flvl2)){			echo "if(val2 >= " . $f2['exp'] . " && val2 < " . fnextexp($f2['lvl']) . ") return '" . $f2['lvl'] . "';";		}	}?>}function fnextexp(cLvl){var nlvl = cLvl.value + 1;	<?php $fnlvl = mysql_query("SELECT * FROM exptable");	$fn1 = mysql_fetch_array($fnlvl); ?>}

im trying to make the fnextexp(cLvl) function to work it needs to return the 'exp' value of the next 'lvl' value in the table. So i set a variable that incermented 'cLvl' by 1. now I just need to "fetch" the 'exp' value from the array that coresponds with the 'nlvl' number that represents 'lvl' in the database.I know fnextexp is not nearly done but thats how far i got before i became clueless.If its too confusing let me know.

Link to comment
Share on other sites

fnextexp is a Javascript function, you're trying to execute it from PHP. PHP can't run a Javascript function. You can't put inline PHP code in a Javascript function and expect the PHP code to be executed when you run the Javascript, it doesn't work like that. It runs the PHP first. You need to have PHP get all of your data and write it out as a Javascript array, then work on the array in Javascript.

Link to comment
Share on other sites

If your server has the json_encode function defined that's the easiest way to turn a PHP array into a Javascript object. If not, you have to loop through it and write it all out. e.g.:

<?php$ar = array(  'one',  'two',  'three');echo 'var ar = new Array();';foreach ($ar as $a)  echo 'ar.push("' . $a . '");';?>

Edited by justsomeguy
Link to comment
Share on other sites

when you said the first thing i went ahead and looked up js arrays and just echoed it using the example on the main website. sry for not posting sooner.

function findlvl(expnow){	val2 = parseInt(expnow.value, 10);	if (isNaN(val))	  return 'Unknown';	<?php 	$flvl2 = mysql_query("SELECT * FROM exptable");	if(mysql_num_rows($flvl2) > 0) {		while($f2 = mysql_fetch_array($flvl2)){			$lvlnow1 = $f2['lvl'];			echo "if(val2 >= " . $f2['exp'] . " && val2 < nextexp(" . $lvlnow1 . ")) return '" . $f2['lvl'] . "'; ";		}	}?>}function nextexp(cLvl){var nlvl = cLvl + 1;var nexp = new Array();	<?php $fnlvl = mysql_query("SELECT * FROM exptable");		while($fn = mysql_fetch_array($fnlvl)){			echo 'nexp[' . $fn['lvl'] . '] = "' . $fn['exp'] . '"; ';		}	 ?>	return nexp[nlvl];}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...