Jump to content

[SOLVED] Sort out numeric values


mmarif4u

Recommended Posts

I am trying to get just numeric values from database table.

i use ctype_digit and is_numeric. yeh i know it works fine.

but when i use while loop ,in the else statement it shows either empty OR any value if i assign to the variable.

Code:

<?php
$sql="select * from user where mobile !=''";
$query = $db->sql_query($sql) or die (mysql_error());
while($row = $db->sql_fetchrow($query)) {
if(ctype_digit($row['mobile']))//can also use is_numeric function
{
$mobile=($row['mobile']);

}else { $mobile = 'its not digit'; }
echo $mobile;
?>

 

i dont want to show the empty OR 'its not digit' sentence.

any ideas.

hope i clarify it well.

Link to comment
https://forums.phpfreaks.com/topic/112719-solved-sort-out-numeric-values/
Share on other sites

try this:

<?php
  $sql = "select * from user where mobile !=''";
  $query = $db->sql_query($sql) or die(mysql_error());
  $row = $db->sql_fetchrow($query);

  if (ctype_digit($row['mobile']))
  //can also use is_numeric function
  {
      $mobile = $row['mobile'];
  } else {
      $mobile = 'its not digit';
  }
  echo $mobile;
?>

 

ACE

latest code:

<?php
$sql="select * from user where mobile !=''";
$query = mysql_query($sql) or die (mysql_error());
$row = mysql_fetch_array($query);
if(ctype_digit($row['mobile']))//can also use is_numeric function
{
$mobile=$row['mobile'];

}else 
{ 
$mobile = 'its not digit'; 
}
echo $mobile;
?>

 

giving me just one result.

Ok,to be more clear.

i have these entries in table.

0123824061

its not digit

its not digit

016

0165241

016

the following code retrieve these entries:

<?php
$sql="select * from user where mobile !=''";
$query = $db->sql_query($sql) or die (mysql_error());
while($row = $db->sql_fetchrow($query)) {
if(ctype_digit($row['mobile']))//can also use is_numeric function
{
$mobile=$row['mobile'];
}else 
{ 
$mobile = 'its not digit'; 
}
echo $mobile;
echo "<br>";
}
?>

Now i want these values:

0123824061

016

0165241

016

but if remove else statement OR make that variable empty in else statement like:

else{ $mobile =''; }

Then the result is:

0123824061

 

 

016

0165241

016

mean showing two empty rows for the alpha.

which i dont want.

 

hope i clear it well.

i solved it:

 

<?php

$sql="select * from user where mobile !=''";

$query = $db->sql_query($sql) or die (mysql_error());

while($row = $db->sql_fetchrow($query)) {

   $mobile='';

   if(ctype_digit($row['mobile']))//can also use is_numeric function

   {

      $mobile=($row['mobile']);                            

   }

   if (!empty($mobile)) {

      echo $mobile;

   }

}

?>

Archived

This topic is now archived and is closed to further replies.

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