Kevin S
Kevin S

Reputation: 173

printf in html code

I want to put this PHP code into an html echo...

*$sql="SELECT SUM(((annual + holiday)+overtime_earned*1.5)/24) FROM leave_balance WHERE user ='$user'";

  if ($result=mysqli_query($con,$sql))
    {
    // Fetch one and one row
    while ($row=mysqli_fetch_row($result))
      {
      printf ("%s \n days off",$row[0] );
      }
    // Free result set
    mysqli_free_result($result);
  }*

Where 'PHP CODE OUTPUT' is where I want the php code output.

echo "<ul class='nav navbar-nav'>" .
                   "<li><a href='members.php?view=$user' class='active'>PHP CODE OUTPUT</a></li>" .

is there a better way to do this?

Upvotes: 1

Views: 691

Answers (1)

jaggedsoft
jaggedsoft

Reputation: 4038

I have simplified your code below:

$sql="SELECT SUM(((annual + holiday)+overtime_earned*1.5)/24) FROM leave_balance WHERE user ='$user'";
$result=mysqli_query($con,$sql);
$row=mysqli_fetch_row($result);
echo "<ul class='nav navbar-nav'><li><a href='members.php?view={$user}' class='active'>{$row[0]} days off</a></li>";
mysqli_free_result($result);

Another way to do it would be to store it as a variable:

$daysoff = $row[0];
echo "{$daysoff} days off";

Upvotes: 1

Related Questions