rashmi_k28 Posted September 8, 2008 Share Posted September 8, 2008 Hi, I have called a function $centre=updown(); $nodes=array('aaa','bbb'); I have to print $centre['aaa'][0]; $centre['bbb'][1]; When i use the code like this all the values are printed for all the values in the array $nodes for($i=0;$i<=count($centre);$i++){ foreach($nodes as $nod){ print $centre[$nod][$i]; } } If the nod='aaa' then only $centre[0] should be printed If th nod='bbb' then only $centre[1] should be printed. How to print the function based on this Link to comment https://forums.phpfreaks.com/topic/123208-print-the-function/ Share on other sites More sharing options...
Ken2k7 Posted September 8, 2008 Share Posted September 8, 2008 If the nod='aaa' then only $centre[0] should be printed If th nod='bbb' then only $centre[1] should be printed. But you're looping through the $nodes array. Wouldn't $nod be "aaa" on the first looping and be "bbb" on the second looping? <?php $count_centre = count($centre); $count_nodes = count($nodes); $s = $b = -1; while (++$s < $count_centre) { foreach ($nodes as $nod) { while (++$b < $count_nodes) { print $centre[$nod][$b]; } } } ?> Link to comment https://forums.phpfreaks.com/topic/123208-print-the-function/#findComment-636314 Share on other sites More sharing options...
rashmi_k28 Posted September 8, 2008 Author Share Posted September 8, 2008 Hi, The value is printed same for all the values in the array 'nodes' How to avoid it. If if the array contains aaa and bbb for aaa the value should be centre[0] and for bbb the value should be centre[1]. Please help me how to resolve it Link to comment https://forums.phpfreaks.com/topic/123208-print-the-function/#findComment-636405 Share on other sites More sharing options...
aschk Posted September 8, 2008 Share Posted September 8, 2008 Not knowing how you're setting up your array to begin with leave this open to error. $i=0; foreach($nodes as $nod){ print $centre[$nod][$i]; $i++; } Link to comment https://forums.phpfreaks.com/topic/123208-print-the-function/#findComment-636414 Share on other sites More sharing options...
rashmi_k28 Posted September 9, 2008 Author Share Posted September 9, 2008 The values is not getting changed based on the values in the array 'nodes'. The $centre[0] is printed for all the values of the array nodes . Link to comment https://forums.phpfreaks.com/topic/123208-print-the-function/#findComment-637254 Share on other sites More sharing options...
rashmi_k28 Posted September 9, 2008 Author Share Posted September 9, 2008 function updowntotal($center){ $centrevalues = array(); $sql = "select distinct(node) from `master_table`"; $nodes = array(); $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { array_push($nodes,$row[0]); } $q=0; foreach($nodes as $_) { $value = array(); $sql_net_perf = "select field1,field2,field3 from `table_name` where node = '".$_."' "; $result = mysql_query($sql_net_perf); if($row = mysql_fetch_array($result)) { $value[0]=$row[0].'('.$row[1].')'.'('.$row[2].')'; } $centrevalues[$nodes[$q++]]=$value; } return $centrevalues; } When I call the function $centreCom= updowntotal($center); and print based on $nodes array which is called separately $count_centre = count($centreCom); $count_nodes = count($nodes); $s = $b = -1; while (++$s < $count_centre) { foreach ($nodes as $nod) { while (++$b < $count_nodes) { print $centreCom[$nod][$b]; } } } For all the $nod $centreCom[0] is printed everywhere. How to avoid it Please help me Link to comment https://forums.phpfreaks.com/topic/123208-print-the-function/#findComment-637274 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.