Begbie Posted November 5, 2009 Share Posted November 5, 2009 Hello, I am new with php and I have a problem with listing data from array. I have function getAllDates() where I collect all dates from my DB and I have function getChefBar($datum, $schicht) where I get value ("bar") for specific day that I need. These two functions are working correct. So I tried to make another function (function getEarnedMoneyForDay ($datum)) where I want to get value that I need ("bar") for all days, but I have a very little knowledge and I am failing to do so. When I type this getChefBar($dates[$datum]) I am getting nothing and my intention is to get value for every day. So if please someone can tell me how to get this value form array. Thanks in advance <?php function getAllDates(){ $selectAllDates = "SELECT s.datum, s.schicht FROM schichtumsatz s ORDER BY s.datum ASC, schicht DESC"; $dates = mysql_query($selectAllDates); $row_dates = mysql_fetch_array($dates); $result=array(); while ($row_dates) { $result[] = $row_dates["datum"]; $row_dates = mysql_fetch_array($dates); } return $result; } function getChefBar($datum, $schicht){ $chef_data="SELECT s.datum, s.schicht, s.bargeld, s.kassiert, s.belege, s.gutschein, s.rechnung, s.sonstiges, s.umsatz FROM schichtumsatz s WHERE s.datum = ".$datum." AND s.schicht = '".$schicht."' ORDER BY s.datum ASC "; $chef_query = mysql_query($chef_data); while($row_chef = mysql_fetch_array($chef_query) ){ $eingegeben = $row_chef["bargeld"] + $row_chef["belege"] + $row_chef["gutschein"] + $row_chef["rechnung"] + $row_chef["sonstiges"]; $bar = $eingegeben - $row_chef["gutschein"] - $row_chef["rechnung"] - $row_chef["sonstiges"] - $row_chef["belege"]; } return $bar; } function getEarnedMoneyForDay($datum){ $schichtA = 'A'; $schichtV='V'; $dates = getAllDates(); for($i=0;$i<$count;$i++){ echo $count .' '; $partresult = getChefBar($dates[$datum],$schichtA); $result = $result + $partresult; } return $result; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/180484-get-data-from-array/ Share on other sites More sharing options...
waynew Posted November 5, 2009 Share Posted November 5, 2009 I made as many changes as I could. I speak English so I obviously had a problem with some parts. There's also some comments that you should read in your last function. <?php function getAllDates(){ $selectAllDates = "SELECT s.datum, s.schicht FROM schichtumsatz s ORDER BY s.datum ASC, schicht DESC"; $dates = mysql_query($selectAllDates) or trigger_error(mysql_error()); $result = array(); while($row = mysql_fetch_assoc($dates)){ $result[] = $row_dates["datum"]; } return $result; } function getChefBar($datum, $schicht){ $chef_data="SELECT s.datum, s.schicht, s.bargeld, s.kassiert, s.belege, s.gutschein, s.rechnung, s.sonstiges, s.umsatz FROM schichtumsatz s WHERE s.datum = '".$datum."' AND s.schicht = '".$schicht."' ORDER BY s.datum ASC "; $chef_query = mysql_query($chef_data) or trigger_error(mysql_error()); $bar = 0; //default while($row_chef = mysql_fetch_array($chef_query) ){ $eingegeben = $row_chef["bargeld"] + $row_chef["belege"] + $row_chef["gutschein"] + $row_chef["rechnung"] + $row_chef["sonstiges"]; $bar = $eingegeben - $row_chef["gutschein"] - $row_chef["rechnung"] - $row_chef["sonstiges"] - $row_chef["belege"]; } return $bar; } function getEarnedMoneyForDay($datum){ $schichtA = 'A'; $schichtV='V'; $dates = getAllDates(); $result = ""; //default //NOTE: where are you getting $count from??? for($i=0;$i<$count;$i++){ echo $count .' '; $partresult = getChefBar($dates[$datum],$schichtA); $result = $result + $partresult; } return $result; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/180484-get-data-from-array/#findComment-952140 Share on other sites More sharing options...
Begbie Posted November 6, 2009 Author Share Posted November 6, 2009 First of all thank you on your replay. Now I have realized that I basically don't need this "for" loop only if I could get these data from array. Please check my code and see if I have missed something or even typed some stupid mistake Thanks in advance ;-) <?php function getAllDates(){ $selectAllDates = "SELECT s.datum, s.schicht FROM schichtumsatz s ORDER BY s.datum ASC, schicht DESC"; $dates = mysql_query($selectAllDates) or trigger_error(mysql_error()); $result = array(); while($row = mysql_fetch_assoc($dates)){ $result[] = $row_dates["datum"]; } return $result; } function getChefBar($datum, $schicht){ $chef_data="SELECT s.datum, s.schicht, s.bargeld, s.kassiert, s.belege, s.gutschein, s.rechnung, s.sonstiges, s.umsatz FROM schichtumsatz s WHERE s.datum = '".$datum."' AND s.schicht = '".$schicht."' ORDER BY s.datum ASC "; $chef_query = mysql_query($chef_data) or trigger_error(mysql_error()); $bar = 0; //default while($row_chef = mysql_fetch_array($chef_query) ){ $eingegeben = $row_chef["bargeld"] + $row_chef["belege"] + $row_chef["gutschein"] + $row_chef["rechnung"] + $row_chef["sonstiges"]; $bar = $eingegeben - $row_chef["gutschein"] - $row_chef["rechnung"] - $row_chef["sonstiges"] - $row_chef["belege"]; } return $bar; } function getEarnedMoneyForDay($datum){ $schichtA = 'A'; $schichtV='V'; $dates = getAllDates(); $result = ""; //default //NOTE: where are you getting $count from??? $partresult1 = getChefBar($dates[$datum],$schichtV); $partresult2 = getChefBar($dates[$datum],$schichtA); $result = $partresult1 + $partresult2; return $result; } ?> Is this: getChefBar($dates[$datum],$schichtV); correct way to get data from array ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/180484-get-data-from-array/#findComment-952206 Share on other sites More sharing options...
waynew Posted November 6, 2009 Share Posted November 6, 2009 To get data from an array, you can either loop through the entire array and get all the values inside that array by doing something like this: $i = 0; $my_array = array('value','another value','etc'); while($i < (sizeof($my_array) -1)){ echo $my_array[$i]; $i++; } or you can reference a value in an array by its index: echo $my_array[0]; echo $my_array[2]; //etc Quote Link to comment https://forums.phpfreaks.com/topic/180484-get-data-from-array/#findComment-952443 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.