phpnewbie112 Posted October 23, 2008 Share Posted October 23, 2008 Hello, I really need your advices and ideas about the given problem. I have a field in a form dedicated to enter international format mobile numbers example 1928123456, 336123456789, 4156897526, 2010102030 I have a mysql tables with 3 fields: Country - Code - Rate example: France - 336 - 5 USA - 1 - 4 How can I make the following code work but while reading the values from the db rather than a manual one: if(substr($num,0,3)=="336") { $rate = "5"; } else if(substr($num,0,1)=="1") { $rate = "4"; } Link to comment https://forums.phpfreaks.com/topic/129802-solved-substrnum-advice/ Share on other sites More sharing options...
wildteen88 Posted October 23, 2008 Share Posted October 23, 2008 Pardon... You're working out the 'rate' within PHP when you already have the 'rate' stored in the database. I dont get that Link to comment https://forums.phpfreaks.com/topic/129802-solved-substrnum-advice/#findComment-672951 Share on other sites More sharing options...
rhodesa Posted October 23, 2008 Share Posted October 23, 2008 try this: <?php $number = '1928123456'; $sql = sprintf("SELECT * FROM rates WHERE SUBSTRING('%s',0,LENGTH(code)) = code",mysql_real_escape_string($number)); $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($result)){ print_r($row); } ?> Link to comment https://forums.phpfreaks.com/topic/129802-solved-substrnum-advice/#findComment-672956 Share on other sites More sharing options...
phpnewbie112 Posted October 23, 2008 Author Share Posted October 23, 2008 wildteen88 I am showing this as an example. I want to get both code and rates from database based on the number I have, the phpcode shall give based on the number the appropriate code and corresponding rate. rhodesa I will test your code thanks Link to comment https://forums.phpfreaks.com/topic/129802-solved-substrnum-advice/#findComment-672967 Share on other sites More sharing options...
phpnewbie112 Posted October 23, 2008 Author Share Posted October 23, 2008 rhodesa it is returning an empty page Link to comment https://forums.phpfreaks.com/topic/129802-solved-substrnum-advice/#findComment-672978 Share on other sites More sharing options...
rhodesa Posted October 23, 2008 Share Posted October 23, 2008 oops...1 not 0: <?php $number = '1928123456'; $sql = sprintf("SELECT * FROM rates WHERE SUBSTRING('%s',1,LENGTH(code)) = code",mysql_real_escape_string($number)); $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($result)){ print_r($row); } ?> Link to comment https://forums.phpfreaks.com/topic/129802-solved-substrnum-advice/#findComment-672990 Share on other sites More sharing options...
phpnewbie112 Posted October 23, 2008 Author Share Posted October 23, 2008 interesting; we have some hints now; it is showing: Array ( [id] => 1 [country] => USA [c0de] => 1 [rate] => 4 ) how can we extract only the rate value from this array? Link to comment https://forums.phpfreaks.com/topic/129802-solved-substrnum-advice/#findComment-672994 Share on other sites More sharing options...
rhodesa Posted October 23, 2008 Share Posted October 23, 2008 <?php $number = '1928123456'; $sql = sprintf("SELECT * FROM rates WHERE SUBSTRING('%s',1,LENGTH(code)) = code",mysql_real_escape_string($number)); $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($result)){ print $row['rate']; } ?> Link to comment https://forums.phpfreaks.com/topic/129802-solved-substrnum-advice/#findComment-672997 Share on other sites More sharing options...
phpnewbie112 Posted October 23, 2008 Author Share Posted October 23, 2008 wow thanks a million you're a genius! Link to comment https://forums.phpfreaks.com/topic/129802-solved-substrnum-advice/#findComment-673001 Share on other sites More sharing options...
phpnewbie112 Posted October 24, 2008 Author Share Posted October 24, 2008 One more question if you don't mind, I tried to include the following: if isempty($row['rate']) { echo "Not Included"; } so when there is no return rate, it gives "not included" message but it is return nothing. pls advice. thanks a million Link to comment https://forums.phpfreaks.com/topic/129802-solved-substrnum-advice/#findComment-673502 Share on other sites More sharing options...
rhodesa Posted October 24, 2008 Share Posted October 24, 2008 <?php $number = '1928123456'; $sql = sprintf("SELECT * FROM rates WHERE SUBSTRING('%s',1,LENGTH(code)) = code",mysql_real_escape_string($number)); $result = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($result)){ while($row = mysql_fetch_assoc($result)){ print $row['rate']; } }else{ print "Not included"; } ?> Link to comment https://forums.phpfreaks.com/topic/129802-solved-substrnum-advice/#findComment-673690 Share on other sites More sharing options...
phpnewbie112 Posted October 24, 2008 Author Share Posted October 24, 2008 Thanks I really appreciate your help Link to comment https://forums.phpfreaks.com/topic/129802-solved-substrnum-advice/#findComment-673728 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.