0

I have this following code:

 $result = $conn->query($query);
  if ($result->num_rows > 0) {

 while($row = $result->fetch_assoc()) 
      { 
  echo "<tr>"; 
  echo "<td>" . preg_replace('/[^0-9]+/','',$row['total']) . "</td>";  
  echo "</tr>"; 
   }

What I try to do is if the output is empty, then insert (-) character. Only if there is no value output.

I tried this:

 $result = $conn->query($query);
  if ($result->num_rows > 0) {

 $total = $total ?? "" ?: "-";

 while($row = $result->fetch_assoc()) 
      { 
  echo "<tr>"; 
  echo "<td>" . $total . "</td>";   
  echo "</tr>"; 
   }

this replaces all outputs with (-). How can I achieve this? please. thank you for your help!

Note that if the result of preg_replace('/[^0-9]+/','',$row['total']) is empty, then it should insert (-) character.

The inputs are like this:

100.
80.
70.
etc

Expected output would be something like this:

Total
100
90
-
80
70
-
10
Max
  • 932
  • 1
  • 10
  • 20
  • @Roshan you mean certainly `if`. But yeah, you don't need regex for that. In your for loop, check for the value of `$row['total']` – Cid Jan 24 '19 at 08:32
  • We have the desired output, but can we have input sample for `$row['total']` ? – Cid Jan 24 '19 at 08:36
  • Then add another if else statement inside while loop, check if `$row['total']` has value then cast the value of that, else replace it with `"-"`. Something like this: `if($row['total'] == " "){ echo $row['total']; }else{ echo "-"; }` – Roshan Jan 24 '19 at 08:37
  • I added some input sample in the post. thanks. – Max Jan 24 '19 at 08:39
  • 2
    What does a non-numeric row have as a value? Empty space? `null`? Something else? Do you need a descending for loop? If there is no row for 90, then how would expect output for that position? Maybe you'd like to add/UNION some static rows into your query. We don't know. – mickmackusa Jan 24 '19 at 08:45
  • Not sure of the logic behind `$total = $total ?? "" ?: "-";`, surely this would shorten to `$total = $total ?? "-";` if $total contained nulls. – Nigel Ren Jan 24 '19 at 08:49
  • Are the values alway integers followed by a dot? – mickmackusa Jan 24 '19 at 08:51
  • yes, they are. I can remove the dots in mysql query. but I don't want to do this for other reasons. – Max Jan 24 '19 at 08:53
  • So `rtrim($row['title'], '.')` will do then. What are you doing to handle missing rows? – mickmackusa Jan 24 '19 at 08:55
  • thanks for your suggestion. there are no missing rows. – Max Jan 24 '19 at 08:59
  • @Max you can trim or regexp in mysql query – Pritamkumar Jan 24 '19 at 09:09
  • @Pritamkumar, thank you. I needed a php solution. – Max Jan 24 '19 at 09:10
  • 1
    i think you should try using in database query as it will fasten your code execution time – Pritamkumar Jan 24 '19 at 09:13
  • This page is loaded with answers that should not work with the sample data, yet they are all upvoted. Very disappointing for this website. – mickmackusa Jan 24 '19 at 10:13

2 Answers2

2

You can check total value and replace it if it is empty. For example:

while($row = $result->fetch_assoc()) { 
  $total = empty($row['total']) ? '-' : preg_replace('/[^0-9]+/','',$row['total']);
  echo "<tr>"; 
  echo "<td>" . $total . "</td>";  
  echo "</tr>"; 
}
Maksym Fedorov
  • 6,383
  • 2
  • 11
  • 31
0

you can also use case statement in mysql

SELECT (CASE
    WHEN total regexp '^[0-9]+$'  THEN total

    ELSE '-' END) as total
FROM `tbl_order`

where regexp '^[0-9]+$' matches whether variable total is numeric or not

by using case we check if total is numeric then print total otherwise -

Pritamkumar
  • 682
  • 1
  • 11
  • 30