0
$query33=mysql_query("SELECT * FROM order_payment_tb where order_status='Success'",$conn) or die (mysql_error());
while($row33=mysql_fetch_array($query33)) {
  $query3=mysql_query("SELECT * FROM user_order_tb where status='Pending' and order_id='".$row33['order_id']."'",$conn) or die (mysql_error());
  $count5=mysql_num_rows($query3);
  {
    echo $count5; ?>
  <?php
  }
}
?>

My result data from mysql is 0111 and I want to show data as 3 that is ( 0+1+1+1=3 ). Please Help

Albzi
  • 15,431
  • 6
  • 46
  • 63

5 Answers5

3

str_split splits the number into an array and each number in the array is summed up using array_sum

Replace $data with your result data

array_sum(str_split($data));
affaz
  • 1,191
  • 9
  • 23
1

The answer is equal to the number of rows returned by this query...

SELECT x.name
     , x.the
     , y.columns
     , y.you 
     , y.actually
     , y.want
  FROM order_payment_tb x
  JOIN user_order_tb y
    ON y.order_id = x.order_id
 WHERE x.order_status = 'Success'
   AND y.status = 'Pending';
Strawberry
  • 33,750
  • 13
  • 40
  • 57
0

str_split Convert a string to an array. Then you can loop through the values and add them, used typecast to make them as int only then add.

  $str = mysql_num_rows($query3);
  if ($str) {
     $arr = str_split($str);
     $sum = 0;
     foreach ($arr as $num) {
          $sum += (int) $num;
     }
     echo $sum;
  }
}
?>
Naincy
  • 2,953
  • 1
  • 12
  • 21
0
$string = '02113';
$stringArray = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY);
$count = 0;

for ($i=0; $i < count($stringArray); $i++) {

    $count = $count + (int)$stringArray[$i];

}

echo $count;
Maulik Savaliya
  • 1,262
  • 7
  • 17
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/15205070) – Farkie Feb 14 '17 at 13:16
  • @Farkie It does attempt to answer the question (albeit poorly). See [this Meta post](http://meta.stackoverflow.com/questions/287563/youre-doing-it-wrong-a-plea-for-sanity-in-the-low-quality-posts-queue) – Machavity Feb 14 '17 at 14:04
0

Please try this, i have updated your code, here is the code below:

$query33=mysql_query("SELECT * FROM order_payment_tb where order_status='Success'",$conn) or die (mysql_error());
$sum = 0;
while($row33=mysql_fetch_array($query33)) {
  $query3=mysql_query("SELECT * FROM user_order_tb where status='Pending' and order_id='".$row33['order_id']."'",$conn) or die (mysql_error());
  $count5=mysql_num_rows($query3);
  $sum = $sum + $count5;

}
echo $sum;
?>

Hope, this may be useful to you.

Prateek Verma
  • 869
  • 1
  • 6
  • 9