crazy_jayne Posted August 6, 2011 Share Posted August 6, 2011 Hello I hope someone can help me I am really struggling with PHP and don't quite understand what I am doing, so please bear with me I am trying to display a message if the result is greater than 12 Please see my attempts below //$BoxTotal = "{qty{$row[$heading_column]}}"; // doens't seem to let me write it without the "" and is now text $BoxTotal += "{qty{$row[$heading_column]}}"; // += trying to convert it to number - still string $BoxTotal += $BoxTotal; // += trying to convert it to number - still string if ($BoxTotal >= 12) { // picks up 15 if it is a string - not the number $warning = "warning only 12 bottles per box please " . $BoxTotal ; } I only seem to be able to get $BoxTotal if I enclose it in quotation marks, which, I think is turning it into text I scoured the Internet and found that perhaps += might convert it back into a number but this doesn't seem to work I expect its really simple when you know how If if helps to see the whole thing I have attached a pdf file, the bit I am looking at is at line 415 (commented out at the moment) Any help really appreciated Cheers Jayne [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/244038-converting-text-to-numbers/ Share on other sites More sharing options...
manix Posted August 6, 2011 Share Posted August 6, 2011 I believe this is what you need? <?php $str = "10"; $num = (int)$str; ?> Quote Link to comment https://forums.phpfreaks.com/topic/244038-converting-text-to-numbers/#findComment-1253247 Share on other sites More sharing options...
voip03 Posted August 6, 2011 Share Posted August 6, 2011 <? $s = "12345"; echo intval($s)+1; ?> Quote Link to comment https://forums.phpfreaks.com/topic/244038-converting-text-to-numbers/#findComment-1253250 Share on other sites More sharing options...
radiations3 Posted August 6, 2011 Share Posted August 6, 2011 The following code might help you if it does the post is solved : <html> <head> <script> function calculate( length, width,height) { var x = length.value; var y = width.value; var z = height.value var t= parseInt(x)+parseInt(y); if (x==900) alert(x); else alert(y); document.frm.textfield.value = t*z; } </script> </head> <body> <form name="frm"> <table> <tr> <td>Length</td> <td><input name="len" type="text" /></td> </tr> <tr> <td>Width</td> <td><input name="wid" type="text" /></td> </tr> <tr> <td>Height</td> <td> <input name="hit" type="text" id="hit" /> </td> </tr> <tr> <td>Result</td> <td><input type="text" name="textfield" id="textfield"></td> </tr> <tr> <td> </td> <td><input type="button" name="Submit" value="Submit" onClick="calculate(document.frm.len, document.frm.wid, document.frm.hit)" /></td> </tr> </table> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/244038-converting-text-to-numbers/#findComment-1253252 Share on other sites More sharing options...
MasterACE14 Posted August 6, 2011 Share Posted August 6, 2011 $BoxTotal = (int)$row[$heading_column]; if ($BoxTotal >= 12) { $warning = "warning only 12 bottles per box please " . $BoxTotal ; } Quote Link to comment https://forums.phpfreaks.com/topic/244038-converting-text-to-numbers/#findComment-1253270 Share on other sites More sharing options...
crazy_jayne Posted August 6, 2011 Author Share Posted August 6, 2011 Thanks guys but I am still drowning The first two seem to work independently but not when I try put them in my code. The third, I think I understand the idea, but I can't integrate it so it doesn't much everything else up. The fourth one looks like the sort of thing I was looking for but the qty part of the equation is missing Is my code fundamentally flawed? Cheer Jayne Quote Link to comment https://forums.phpfreaks.com/topic/244038-converting-text-to-numbers/#findComment-1253282 Share on other sites More sharing options...
MasterACE14 Posted August 6, 2011 Share Posted August 6, 2011 what is the 'qty' part suppose to do? Quote Link to comment https://forums.phpfreaks.com/topic/244038-converting-text-to-numbers/#findComment-1253286 Share on other sites More sharing options...
crazy_jayne Posted August 6, 2011 Author Share Posted August 6, 2011 It's part of a nested loop that someone here helped me with a little while ago. That part seems to be working fine I have attached the full code in a pdf file in the first post Cheers J Quote Link to comment https://forums.phpfreaks.com/topic/244038-converting-text-to-numbers/#findComment-1253294 Share on other sites More sharing options...
darkfreaks Posted August 6, 2011 Share Posted August 6, 2011 //using ternary statement to determine condition what to do if true and what to do if false $BoxTotal= (>= 12) ? 'warning only 12 bottles per box please"'.$BoxTotal.'" ' : $BoxTotal; Quote Link to comment https://forums.phpfreaks.com/topic/244038-converting-text-to-numbers/#findComment-1253330 Share on other sites More sharing options...
darkfreaks Posted August 6, 2011 Share Posted August 6, 2011 also a reason it isnt working is because $header is outputing [h_box_no] which is wrong it needs to be ["h_box_no"] so try using double quotes $row["$heading_column"] ; Quote Link to comment https://forums.phpfreaks.com/topic/244038-converting-text-to-numbers/#findComment-1253367 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.