Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Problem dividing PHP variables?!

Options
  • 17-11-2006 2:19am
    #1
    Registered Users Posts: 1,987 ✭✭✭


    Im retrieving two integer values from a db and they are both printing to screen fine but when i try to divide one by the other i get this error:

    Warning: Division by zero in C:\Apache\htdocs\dn\cl_desc.php on line 128

    This is what i have on line 128, id number is just an id number for the row, but it shud just divide the two integers that both methods return:
    Code:
    <?php $total = rating_cl($id_num) / voters_cl($id_num); echo $total; ?>
    


Comments

  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Theres obviously a problem with the functions. More code is needed here to debug methinks.


  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    function rating_cl($c)
    {
    	$sql = 'SELECT vote FROM cl WHERE id='.$c.'';
    	$data = mysql_query($sql);
    	while ($row = mysql_fetch_assoc($data)) 
    	{
       		echo $row['vote'];
    	}
    }
    function voters_cl($c)
    {
    	$sql = 'SELECT voters FROM cl WHERE id='.$c.'';
    	$data = mysql_query($sql);
    	while ($row = mysql_fetch_assoc($data)) 
    	{
       		echo $row['voters'];
    	}
    }
    

    heres the two functions that are being called but its not them as they are returning the right data and printing them onto the website fine, its just the divide bit thats causing an error for some reason!?!


  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    [edit] oops too late :D


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    You're using $c in the functions, where is $id_num being set?

    I've mocked up some functions and tested it like this, and it works:

    [php]

    <?php


    $id_num = "6";


    function rating_cl($id_num) {

    $id_num = $id_num * '5';

    return $id_num;

    }

    function voters_cl($id_num) {

    $id_num = $id_num / '3';

    return $id_num;

    }

    echo "rating_cl is ".rating_cl($id_num)."<br />";

    echo "voters_cl is ".voters_cl($id_num)."<br />";

    $total = rating_cl($id_num) / voters_cl($id_num);

    echo $total;



    ?>

    [/php]


  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    EDIT:

    Did it print out the division answer?


  • Advertisement
  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Nope, no error using that. But why would there be, it's merely an alteration on the echo call, none of the calculation code is changed.

    EDIT: Yeah, the code I posted above works. As I said, where and how is $id_num being defined?


  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    Sorted it! The problem was in the functions, they were echoing the result and the division operator didn't like it, so i just returned the values from the function and it works fine now!
    Thanks for the help!


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Shít yeah, I should have noticed that, you needed to return the values rather than print them! No problem anyway! ;)


  • Closed Accounts Posts: 2,046 ✭✭✭democrates


    It would be prudent to always check a value is non-zero before attempting to divide anything by it, and if it's zero to present a relevant message such as "No voters yet".


  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    democrates wrote:
    It would be prudent to always check a value is non-zero before attempting to divide anything by it, and if it's zero to present a relevant message such as "No voters yet".

    I know but ther values where never zero, its just as i said the values where being echoed instead or returned!


  • Advertisement
  • Registered Users Posts: 6,508 ✭✭✭daymobrew


    Ziycon wrote:
    I know but ther values where never zero, its just as i said the values where being echoed instead or returned!
    You should consider the case when the query doesn't return any rows i.e. when the id ($c) is invalid. Returning 0 from the function might be a good error value then.


Advertisement