Jump to content

MoFish

Members
  • Posts

    227
  • Joined

  • Last visited

Posts posted by MoFish

  1. Hi,

     

    I have several processes which are fired on a single button click. I am trying to send feedback to the browser to let the user know which process is currently running - but am only getting feedback once they are all completed, instead of during ...

     

    I wondered if anyone could help me in resolving why why the messages are only coming through once all events are fired and not during?

     

    As an example I have the following...

     

    A PHP page with one form and a button called designForm.

    	<script>
    	$('#designForm').submit(function(event) {
    		event.preventDefault();
    		$.ajax({
    			type: 'POST',
    			url: 'inc/design.process.php',
    			data: $(this).serialize(),
    			dataType: 'html',
    			success: function (data) {
    				$('#content').html(data);
    			}
    		});
    	});
    	</script>
    

    This then fires the design-process.php page which has the following.

    <?php
    
    	function progress($message, $percent, $type){
    			echo "<div class='alert-".$type."'>
    			<p>$message</p>
    				<div class='progress progress-striped active'>
    				<div class='progress-bar'  role='progressbar' aria-valuenow='".$percent."' aria-valuemin='0' aria-valuemax='100' style='width: ".$percent."%'>
    					<span class='sr-only'>".$percent."% Complete</span>
    				</div>
    			</div>
    		</div>";
    	}
    
    	sleep(5);
    	progress("First event firing...", "10", "warning"); 
    	flush(); 
    
    	sleep(5);
    	progress("Second event firing...", "50", "warning");
    	flush(); 
    
    	sleep(5);
    	progress("Third event firing...", "100", "warning");
    	flush(); 
    
    ?>
    

    All works well - apart from they all appear at once. I thought the flush command sent them directly to the browser but must have got lost somewhere.

     

    Thank you,

     

    MoFish

  2. Hi,

     

    I am trying calculate all the values in the 'amount' column for each individual staff member (staff_id) and return this as 'total' for the person with the greatest value.

     

    This will be based on current year, current month, current week.

     

    I understand I dont need to select all these values, but the calculation doesnt appear correct.

     

    My Current Query For Year:

    SELECT SUM(amount) AS total, 
    sales_staff.`id`, sales_staff.`name`, sales_staff.`icon`,
    sales_league.`id` , sales_league.`amount` , sales_league.`money_received` , sales_league.`closedby_id` , sales_league.`new_customer` , 
    sales_league.`date_added` , sales_league.`time_added` 
    FROM sales_staff 
    JOIN sales_league 
    ON sales_staff.id = sales_league.staff_id 
    WHERE YEAR(date_added) = YEAR(CURDATE()) 
    GROUP by staff_id 
    ORDER BY amount DESC LIMIT 1
    

    Returns:

    Year: £3596

     

    My Current Query For Month:

    SELECT SUM(amount) AS total, 
    sales_staff.`id`, sales_staff.`name`, sales_staff.`icon`, 
    sales_league.`id` , sales_league.`amount` , sales_league.`money_received` , sales_league.`closedby_id` , sales_league.`new_customer` , 
    sales_league.`date_added` , sales_league.`time_added` 
    FROM sales_staff 
    JOIN sales_league 
    ON sales_staff.id = sales_league.staff_id 
    WHERE YEAR(date_added) = YEAR(CURDATE()) AND MONTH(date_added) = MONTH(CURDATE()) 
    GROUP by staff_id 
    ORDER BY amount DESC LIMIT 1 
    

    Returns
    Month:  £3596

     

    My Current Query For Week

    SELECT SUM(amount) AS total, 
    sales_staff.`id`, sales_staff.`name`, sales_staff.`icon`, 
    sales_league.`id` , sales_league.`amount` , sales_league.`money_received` , sales_league.`closedby_id` , sales_league.`new_customer` , 
    sales_league.`date_added` , sales_league.`time_added` 
    FROM sales_staff 
    JOIN sales_league 
    ON sales_staff.id = sales_league.staff_id 
    WHERE YEAR(date_added) = YEAR(CURDATE()) AND WEEK(date_added) = WEEK(CURDATE()) 
    GROUP by staff_id 
    ORDER BY amount DESC LIMIT 1 
    

    Returns:

    Week: £2100

     

    From the table below I would of expected staff_id = 2 total for the month to be around £6000 based on the amounts in the red column below?

     

     

    totalme.jpg

     

     

    In summary,

     

    I'm looking to look at the table and find out who has sold the most based on the 'amount' field and return this as a totals value along with there name, id etc for the current (year, month, week)

     

    Thanks,

     

    MoFish

  3. SELECT SUM( amount ) AS total, sales_staff.id, sales_staff.name, sales_staff.icon, sales_league.`id` , sales_league.`amount` , sales_league.`money_received` , sales_league.`closedby_id` , sales_league.`new_customer` , sales_league.`date_added` , sales_league.`time_added`
    FROM sales_staff
    JOIN sales_league ON sales_league.staff_id = sales_staff.id
    WHERE YEAR( date_added ) = YEAR( CURDATE( ) )
    GROUP BY staff_id
    ORDER BY amount DESC
    LIMIT 1 
    

    GROUP BY should do the trick?

  4. Hi Requnix,

     

    It appers to be adding all the values for 'amount' where staff_id = 1 in this example - which works out to be 17000.

     

    But the 3rd one has a date of 2013 so thought this would have been excluded from the calculation?

     

    Thanks,

     

    MoFish

     

     

    table.png

     

    result.png

  5. Hi,

     

    I have the following query which works and calculates an amount as 'total' - however the total value appears to be including values from the year 2013.

     

    Does the sum part exclude the where clause?

     

    I was hoping it would only give me the total for the current year.

     

    How would this therfore be restructured to only give me the 'amount' for the current year?

     

    Thanks,

     

    MoFish

    SELECT SUM(amount) AS total,
    sales_staff.id, sales_staff.name, sales_staff.icon, sales_league.`id`, sales_league.`amount`, sales_league.`money_received`, sales_league.`closedby_id`, sales_league.`new_customer`, sales_league.`date_added`, sales_league.`time_added`
    FROM sales_staff
    JOIN sales_league ON
    sales_league.staff_id = sales_staff.id
    WHERE YEAR(date_added) = YEAR(CURDATE())
    ORDER BY amount DESC LIMIT 1
    
  6. without knowing the methods inside your sql class

     

    we would only be guessing

    <?php
    class sql
    {
    	
    	var $name;
    	var $host;
    	var $username;
    	var $password;
    	var $mysql_link;	
    	var $sql_query;	
    	var $sql_result;
    	var $error;
    	
    	function sql()
    	{
    		$this->name=MYSQL_DB;
    		$this->host=MYSQL_HOST;
    		$this->username=MYSQL_USER;
    		$this->password=MYSQL_PASS;
    		$this->connection();
    	}
    	
    	function connection()
    	{
    		$this->mysql_link=mysql_connect($this->host,$this->username,$this->password) or die ('Error connecting to MySQL');
    		mysql_select_db($this->name, $this->mysql_link) or die('Database '.$db['db'].' does not exist!');
    	}
    	
    	function query($sql_query)
    	{
    		
    		$this->sql_query=$sql_query;
    		$this->sql_result=mysql_query($this->sql_query);
    		$this->error=mysql_error();
    		if (!$this->sql_result) 
    		{
    			echo "<b>" . $sql_query . "</b><br><br>";
            	$this->error=mysql_error();
            	echo $this->error;
            }
    		echo mysql_error();
    	}
    	
    	function num_rows() 
        { 
            $mysql_rows = mysql_num_rows( $this->sql_result ); 
            return $mysql_rows; 
        } 
        
        function fetch_array() 
        { 
            if ( $this->num_rows() > 0 ) 
            { 
                $mysql_array = mysql_fetch_array( $this->sql_result ); 
                if (!is_array( $mysql_array )) 
                { 
                    return FALSE; 
                } 
                return $mysql_array; 
            } 
        }
        
        function insert_id()
        {
        	return mysql_insert_id();
        }
    
    }
    
    ?>
    

    you also should not be creating a new instance of your database class inside of another class method just to run each query.

     

    Should I therfore be setting db one at the first instance?

     

    Thank you for your help.

  7. Hi,

     

    I'm getting the following error and im not entirely sure why - Call to a member function fetch_array() on a non-object

     

    When I execute the SQL query manually in phpmyadmin it displays 'total' a value of as 7000 which is correct - so the query appears correct.

     

    Could anyone point me in the right direction?

     

    Something simple im missing.

        public function get_staff_current_month($staff_id)
        {
        	$db = new sql();
        	$sql = "SELECT SUM(amount) as total FROM $this->tablename WHERE staff_id = $staff_id AND YEAR(date_added) = YEAR(CURDATE()) AND MONTH(date_added) = MONTH(CURDATE()) ORDER BY id ASC";
        	$result = $db->query($sql);
        	//var_dump($result);
        	$row = $result->fetch_array();
        	return $row['total'];
        }
    

    Regards,

     

    MoFish.

  8. Hi,

     

    I am trying to read some hex values from a style-sheet, however am having a little bit of a problem reading the values from a file into a string due to them being different with no unique identifier.

     

    Is there an easy way to read the file and put get the value "#e51400" for a variable called $red and "#0076a3" for a variable called $blue?

     

    I looked into str_replace but couldn't see a simple way to do this.

     

    Thanks,

     

    MoFish

    @red: #e51400;
    @blue: #0076a3;
    
  9. Hi,

    Is it possible to get the last date of a month in mysql on a start date variable?

     

    Example...

    WHERE date >= '$date' and date <= GETLASTDAY('$date'):
    

    I'm aware this wont work - but may help understand what i'm trying to achieve.

    I currently pass in a variable called $date which has a value of "2013-11-01" . I'm hoping to execute a mysql query where it would figure out the last possible date in that month based on the $date variable e.g '2013-11-30'.

     

    In theory i want to execute something like the below - but it automagically figure out the second value

    WHERE date >= '2013-11-01' and date <= '2013-11-30':
    

    Is there an easy way to do this?

     

    Thanks,

     

    MoFish

  10. Hi,

     

    I have a two input fields posting dates in the format of 01/11/2013 (d/m/Y)

     

    I am trying to change these into the database format of 2013-11-01 (Y-m-d)

     

    I'm getting unexpected results for some reason and am not sure the reason behind it.

     

    Could anyone clarify whats going on?

    	if(isset($_POST['filterBtn'])){
    		$postfromdate = $_POST['fromDate']; // 26/11/2013		
    		$fromdate = date('Y-m-d', strtotime($postfromdate));
    
    		echo "START <br/> post value <strong>" . $postfromdate . "</strong> converted value <strong>". $fromdate . "</strong> <br/>";
    		
    		$posttodate = $_POST['toDate']; // 26/11/2013
    		$todate = date('Y-m-d', strtotime($posttodate));
    		
    		echo "END <br/> post value <strong>" . $posttodate . "</strong> converted value <strong>". $todate . "</strong> <br/>";
            }
    

    and the results:

     

    START
    post value 01/11/2013 converted value 2013-01-11
    END
    post value 30/11/2013 converted value 1969-12-31

     

    I'm not sure why im getting 1969 in some cases.

     

    Thanks,

     

    MoFish

     

  11. abjnoob i'm still not entirely sure what you have been reading, but from my understand we do not have a LIMIT 1 mentioned through-out the posts in the thread (hence our confusion). I also do not understand how the KISS suggestion helps me in getting the first result to display in my array lol.

     

    Ch0cu3r I think your reading the correct thread. I tried your suggestion to use mysql_num_rows instead of fetch_array but this did not seem to help. Any other suggestions?

  12. Yeah, I see.  He's knee deep without the basics.  I'm sure it's a copy paste snippet here and snippet there collection of code statements in an attempt.

     

    Aww, I thought I had done rather well to get the results coming out from the database - I just couldn't figure out why I was missing one. So what exactly am I missing? Have i been lead astray during the cross wires of posts somehow? lol

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