-5

In a html page, I would like to appear, for example, the following text:

Amount: 50

where

Amount - is a fixed word
50 - is a sum of the values from a table column.

I have the php script (let's call it "Script.php") where I calculate the sum:

$query_ = mysqli_query($link, "SELECT SUM(table_column) AS amount FROM table"); 
$row = mysqli_fetch_assoc($query_); 
$sum = $row['amount'];

echo "<font color='red', size='5'>$sum";

Currently the html code looks like this:

<ul>
    <li>Some info: &nbsp;some info</li>
    <li>Amount: ??? </li>
</ul>

What should I put instead of "???" in order to obtain what I want? Thanks.

UPDATE: the php code and the html code are in two different files.

@Machavity: My question is not the same as the one considered to you as duplicated by me. As I have mentioned, the php code and the html code are in two different files. Please revise your "marked as duplicate" or refer to another question which I have duplicated. Thanks.

Adi S.
  • 53
  • 1
  • 10
  • echo "$sum"; what's your problem with this line it seems to be ok if $sum contains correct data – siddhesh Dec 31 '17 at 12:59
  • @siddhesh excuse me, it's not this way OP will get the value of `$sum`. – statosdotcom Dec 31 '17 at 13:01
  • why it's script.php looks fine @statosdotcom am I missing something – siddhesh Dec 31 '17 at 13:03
  • @siddhesh As you put: `echo "$sum";` this will print on final html: `$sum`, instead of the desired result that could be produced from this: `echo "" . $sum;`, yes? If $sum is inside double quotes the value of variable is not printed, just its name ;) Happy new year coding. God bless. – statosdotcom Dec 31 '17 at 13:08
  • @statosdotcom inside double quotes value of the variable is always printed. but single quote never prints the value of variable. for you reference look at this https://stackoverflow.com/questions/10512452/php-using-a-variable-inside-a-double-quotes – siddhesh Dec 31 '17 at 13:11
  • @siddhesh I have to applause and thank you! I am sorry bugging your perfect code. Learning php right now with you. Please accept my apologies. – statosdotcom Dec 31 '17 at 13:16
  • @statosdotcom no problem. it's normal to have such misunderstanding. – siddhesh Dec 31 '17 at 13:18

5 Answers5

2

You can do it like this:

<ul>
   <li>Some info: &nbsp;some info</li>
   <li>Amount: <?= $sum ?> </li>
</ul>

UPDATE:

At the top of your view file, add this:

<?php require_once 'Script.php' ?>

And then put this code:

<ul>
   <li>Some info: &nbsp;some info</li>
   <li>Amount: <?= $sum ?> </li>
</ul>

I put the require_once code at the top, because it is better to leave your presentation code as clean and readable as possible. The <?= is valid and also makes the code cleaner and shorter. When adding side code that doesn't do anything to the presentation (don't adds something on the screen), you are redundantly messing the code. That could make your code more difficult to understand in the future (if you continue messing it).

Nikolay Traykov
  • 1,537
  • 17
  • 26
  • Why is the negative vote? My answer is correct and was older. – Nikolay Traykov Dec 31 '17 at 13:04
  • Here, have a sypathy +1. I even went to [PHP Sandbox](http://sandbox.onlinephpfunctions.com/code/4952760768c4b1d3f0a666c016e08296528e65ee) to make sure this was correct. I usually use echo, but there's a good argument for using this method as well. – TecBrat Dec 31 '17 at 13:06
  • I would suspect because your code won't work without short open tags? Normally you shouldn't use that syntax. Use a normal echo instead. – René Höhle Dec 31 '17 at 13:07
  • It does not work. Maybe I was not clear that php file and html file are two separate files. – Adi S. Dec 31 '17 at 13:08
  • Ah, I see. I didn't understand there are different files. I will correct my answer. As for the = syntax, it is completely ok without configuring anything. – Nikolay Traykov Dec 31 '17 at 13:12
  • 1
    According to https://stackoverflow.com/questions/2020445/what-does-mean-in-php , It's enabled by default since 5.4 regardless of php.ini settings. – TecBrat Dec 31 '17 at 13:15
  • Yes, that's right, TecBrat. I didn't specify it because php 5.3 is very old now. I guess not much people are using it like Adi S. for example, because I see he's newby and you won't install old stuff if you just start learning coding. – Nikolay Traykov Dec 31 '17 at 13:20
  • @NikolayTraykov. In this case I need to change the html file to be a php file, correct? If I do so, it works, but I have an unwanted side effect. In the ex-html page, besides
      ...
    code, I have the following code:
    Before, the two buttons appeared one under the other without any spacing. Now there is a spacing between them. How can I remove the spacing?
    – Adi S. Dec 31 '17 at 15:51
  • Regarding my previous post, the problem was solved when I have added to the new php file (ex-html). After I have changed the html file to php I have removed the above reference to html thinking that is not needed anymore as now the file is a php file, but according to another post I have read, this is necessary (below the link). – Adi S. Dec 31 '17 at 20:04
2

Here is what I think will do.

<ul>
    <li>Some info: &nbsp;some info</li>
    <li>Amount: <?php echo $sum ?></li>
</ul>
halojoy
  • 225
  • 2
  • 7
  • It does not work. Maybe I was not clear that php file and html file are two separate files. – Adi S. Dec 31 '17 at 13:07
1
<ul>
    <li>Some info: &nbsp;some info</li>
    <li>Amount: <?php include 'Script.php'; ?> </li>
</ul>

Note that your echo should also do a </font> to prevent the rest of the page from turning red.

You must rename your HTML file to .php to be able to do this. But if you do that, you might as well use PHP properly. Remove the echo from Script.php and then you can do:

<?php include 'Script.php'; ?>
<ul>
    <li>Some info: &nbsp;some info</li>
    <li>Amount: <font color="red" size="5"><?php echo $sum; ?></font> </li>
</ul>
rickdenhaan
  • 10,857
  • 28
  • 37
  • It worked. That is exactly what I have desired.Thanks a lot. – Adi S. Dec 31 '17 at 15:20
  • Maybe the next question is a very beginner question. Are there any side effects if now the html file is a php file? I am asking this because except the
      ...
    code I have a lot of other code in the ex-html file. Or the rest of the code (no matter what) should work perfectly as it worked in the ex-html file?
    – Adi S. Dec 31 '17 at 15:25
  • I am already seeing some side effects. In the ex-html page, I have the following code:
    Before, the two buttons appeared one under the other without any spacing. Now there is a spacing between them. How can I remove the spacing?
    – Adi S. Dec 31 '17 at 15:37
  • Regarding my previous post, the problem was solved when I have added to the new php file (ex-html). After I have changed the html file to php I have removed the above reference to html thinking that is not needed anymore as now the file is a php file, but according to another post I have read, this is necessary (below the link). – Adi S. Dec 31 '17 at 20:04
  • Yes, the file is still HTML. The `.php` extension just tells your server that it needs to run it through PHP first to process any code before serving it to the browser, but what the browser sees is still HTML so it should have a doctype. Another side effect is that you'll need to change every link to your ex-HTML page in your entire website. – rickdenhaan Jan 01 '18 at 02:25
1

There's the short tag version of your code, which is now completely acceptable to use despite antiquated recommendations otherwise:

<tr><td> <input type="hidden" name="type" value="<?= $var ?>" ></td></tr>

which (prior to PHP 5.4) requires short tags be enabled in your php configuration. It functions exactly as the code you typed; these lines are literally identical in their internal implementation:

php <?= $var1, $var2 ?> <?php echo $var1, $var2 ?>

MESSIAH
  • 1
  • 6
0

I would advice you to use a template system like Twig to separate your logic from your templates. That makes it much easier to understand. I know that when you start with PHP it's not the easiest thing but you can learn very good with such tasks.

In your case you can simply echo your variable like this:

<ul>
    <li>Some info: &nbsp;some info</li>
    <li>Amount: <?php echo $sum ?></li>
</ul>
René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • It does not work. Maybe I was not clear that php file and html file are two separate files. – Adi S. Dec 31 '17 at 13:13
  • @Adi S.: ok when you start you echo in the first output :D it's not really clear. So use a template engine and you code looks much better. – René Höhle Dec 31 '17 at 13:14
  • I have looked a little bit a Twig (it is the first time when I have heard of it), but if I correctly understood, it could help me (or not) to write easier code, but it would not help me to solve my problem (in the mean time it was solved by rickdenhaan answer). – Adi S. Dec 31 '17 at 15:28