waynew Posted July 14, 2008 Author Share Posted July 14, 2008 What function allows me to find out what the index of the last element is? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 14, 2008 Share Posted July 14, 2008 Man this is the biggest topic I have ever posted here. There is an edit button. It's getting big because your putting in new replies when its only been 8 minutes between your posts. Surely there is another way to go about this? Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 I think I may have solved the problem. I'm sticking with using the true value in array_slice so that all values maintain their index values. However, I need a function that will tell me what the index value of the last element in the array is. Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 I'm still stumped on this. Something in the cut is definately wrong. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 14, 2008 Share Posted July 14, 2008 I still think you could store all the information in one array. Just put the month before the profit. Eg: array("Jan-07_4129","Feb-07_45817.124"); etc etc Then you could just splice it like that, explode from the underscore to get the date and the profit then do whatever else you need to do... Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 I know its just that stats.php which is included at the top and creates the arrays is pretty complex and finnicky. Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 Is there a better function to use instead of slice that can take out a designated part of an array? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 14, 2008 Share Posted July 14, 2008 Just merge them after the creation... Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 I had a look into that and I don't think that will work. You see, the values are perfect before they are sliced. So it's not an issue of the arrays coming in. It's definately an issue with the slice: //This graph shows profit, cost and end price. if(isset($_GET['startdate']) && isset($_GET['enddate'])){ //check to see if user is wanting to limit dates $dates_selected = true; //this means that the user has chosen to select a time period. Used later on. $start_date = $_GET['startdate']; //begin date. $end_date = $_GET['enddate']; //end date. $end_date = $end_date - $start_date; //Split up together in order to maintain sync of timeline and sum. $x_months = array_slice($x_months,$start_date,$end_date, true); $monthly_profit = array_slice($monthly_profit,$start_date,$end_date, true); } Oh God I feel like crying right now :'( Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 By the way, thanks for your help Project. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 14, 2008 Share Posted July 14, 2008 Use the edit button. Lol. Still, I think it's worth a shot. Backup that and just try merging the arrays. Can't hurt. Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 Ok, how would you merge those two arrays? Bearing in mind that I don't want any keys overwritten? Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 Ok. I've found the root of the problem. The root of the problem went unnoticed because of the way I debugged by echo-ing while looping, instead of actually just print_r -ing both arrays. Have a look at the two arrays as they come in from stats.php Array ( [0] => Oct 07 [1] => Nov 07 [2] => Dec 07 [3] => Jan 08 [4] => Feb 08 [5] => Mar 08 [6] => Apr 08 [7] => May 08 [8] => Jun 08 [9] => Jul 08 ) Array ( [0] => 1151.519 [6] => 24345.6248 [5] => 186.456 [1] => 8177.1744 [4] => 10674.6675 [2] => 16434.037 [3] => 29104.1746 [7] => 5935.786 [8] => 10403.2978 [9] => 4444.859 ) How on earth this has happened I don't know. :-\ Here is stats.php. I have commented it as much as possible. I am working from a live DB that had some design flaws so please excuse the mess. Also bear in mind that a number of arrays aren't used in the file posted before: <?php /* Engine behind salesgraph. Wayne */ //Uses it's own connection as it wont be revolving around the usual db class. include("db_connect.php"); $months = array( 0=>'All Months', '1'=>'Jan', '2'=>'Feb', '3'=>'Mar', '4'=>'Apr', '5'=>'May', '6'=>'Jun', '7'=>'Jul', '8'=>'Aug', '9'=>'Sep', '10'=>'Oct', '11'=>'Nov', '12'=>'Dec' ); //------------------------------------------------------------------------------------ //Go through jobs table, putting all job_ids that were won into array called $jobs_id. //------------------------------------------------------------------------------------ $job_ids = array(); //will store won job ids. $reg_dates = array(); //store reg_date of every won job $won_jobs_result = mysql_query("SELECT id, reg_date FROM jobs WHERE status = 1") or die(mysql_error()); $i = 0; //counter used to increment key of $jobs_id array inside while loop below. while($won_job_row = mysql_fetch_assoc($won_jobs_result)){ $job_ids[$i] = $won_job_row['id']; $reg_dates[$i] = $won_job_row['reg_date']; $i++; } mysql_free_result($won_jobs_result); //------------------------------------------------------------------------------------- // End gathering of won job ids //------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------- // Date calculations. Figure out the time periods. //------------------------------------------------------------------------------------- //Get the first year for usage in graph title $first_year_result = mysql_query("SELECT YEAR(reg_date) FROM jobs WHERE status = 1 ORDER BY reg_date ASC LIMIT 1") or die(mysql_error()); $first_year = array(); $last_year = array(); while($first_year_row = mysql_fetch_assoc($first_year_result)){ $first_year[0] = $first_year_row['YEAR(reg_date)']; } mysql_free_result($first_year_result); //Last Year - used in title of graph with first year. $last_year_result = mysql_query("SELECT YEAR(reg_date) FROM jobs WHERE status = 1 ORDER BY reg_date DESC LIMIT 1"); $last_year = array(); while($last_year_row = mysql_fetch_assoc($last_year_result)){ $last_year[0] = $last_year_row['YEAR(reg_date)']; } mysql_free_result($last_year_result); //List of distinct months/years : graph timeline from beginning to end. $time_line_result = mysql_query("SELECT DISTINCT MONTH(reg_date), YEAR(reg_date) FROM jobs WHERE status = 1 ORDER BY reg_date ASC") or die(mysql_error()); $months_time_line = array(); $years_time_line = array(); $x_months = array(); $i=0; //Reset to 0 for purpose of incrementing array key while($month_year_row = mysql_fetch_assoc($time_line_result)){ $months_timeline[$i] = $month_year_row['MONTH(reg_date)']; //Get month number $months_time_line[$i] = $months[$months_timeline[$i]]; //use months array to find relevant string $years_time_line[$i] = substr($month_year_row['YEAR(reg_date)'],2); //Get the last 2 figures of the year. 07 and 08 etc. $x_months[$i] = $months_time_line[$i]." ".$years_time_line[$i]; //Add two dates together $i++; } mysql_free_result($time_line_result); //------------------------------------------------------------------------------------- // End Date calculations. //------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------- // Start profit and cost calculations. //------------------------------------------------------------------------------------- $i = 0; $tender_id = array(); $tender_months = array(); $tender_years = array(); $tender_margin = array(); $tender_price = array(); $tender_result = array(); $tender_profit = array(); $tender_cost = array(); while($i < sizeof($job_ids)){ $tender_result[$i] = mysql_query("SELECT id, MONTH(reg_date), YEAR(reg_date), tender_price, required_margin FROM tenders where job_id = $job_ids[$i]") or die(mysql_error()); while($row = mysql_fetch_assoc($tender_result[$i])){ $tender_id[$i] = $row['id']; $tender_months[$i] = $row['MONTH(reg_date)']; $tender_years[$i] = substr($row['YEAR(reg_date)'],2); $tender_timeline[$i] = $months[$tender_months[$i]]." ".$tender_years[$i]; $tender_price[$i] = $row['tender_price']; $tender_margin[$i] = $row['required_margin']; //Calculation of cost + profit $tender_margin[$i] = $tender_margin[$i] / 100; //turn int into decimal place $tender_profit[$i] = $tender_price[$i] - ($tender_price[$i] * $tender_margin[$i]); //$tender_profit[$i] = $tender_profit[$i] * -1; //turn minus into plus: not sure why minus is showing up $tender_cost[$i] = $tender_price[$i] + $tender_profit[$i]; } $i++; } //------------------------------------------------------------------------------------- // End profit and cost calculations of each tender //------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------- // Begin arranging tender calculations into month arrays for sync with months on graph //------------------------------------------------------------------------------------- $i=0; //Reset counter. //Set up arrays that will be used in graph - three line points $monthly_cost = array(); $monthly_profit = array(); $monthly_price = array(); //create keys for each month in each array and increment to zero while($i < sizeof(x_months)){ //make sure each array is length of graph timeline. $monthly_cost[$i] = 0; $monthly_profit[$i] = 0; $monthly_price[$i] = 0; $i++; } $i=0; //Reset counter. $j = 0; //Create counter for inner loop. while($i < sizeof($tender_id)){ //echo "i = $i<br/><br/>"; while($j < sizeof($x_months)){ //echo "j outside if = $j<br/><br/>"; if($tender_timeline[$i] == $x_months[$j]){ $monthly_cost[$j] = $monthly_cost[$j] + $tender_cost[$i]; $monthly_profit[$j] = $monthly_profit[$j] + $tender_profit[$i]; $monthly_price[$j] = $monthly_price[$j] + $tender_price[$i]; //echo $j." = match<br/><br/>"; } $j++; } $j=0; $i++; } ?> Quote Link to comment 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.