Page 1 of 1

Putting a + or - in front of a number in a table

Posted: Tue Nov 15, 2011 9:31 pm
by Zyviel
I am putting numbers into a html table to display the pluses or minuses on a players inventory item. The negative numbers show up with a minus but the positive numbers are just showing up with the number and no plus sign.

I'm just curious if there is an html or css formatting command that I can use to have a plus show up in front of positive numbers? If not I will add if statements in my php to check if the numbers are positive and then concatenate the + onto the numbers.

Thanks for any suggestions anyone has.

Re: Putting a + or - in front of a number in a table

Posted: Tue Nov 15, 2011 9:35 pm
by Jackolantern
Hmm...I have never actually dealt with this myself, but I wonder if <pre> may handle it? Often, <pre></pre> tags are used to format programming code because it tells the HTML rendering engine that you have preformatted the text and not to mess with it.

Maybe try that, and see what effect that has. It would keep unnecessary strain off of your server if you could create a client-side option to ensure the pluses show.

Re: Putting a + or - in front of a number in a table

Posted: Tue Nov 15, 2011 10:06 pm
by Zyviel
Thanks for the reply Jackolantern.

I checked out the pre tag but it looks like I still have to put what I want to create inside the pre tags.

I can do that or I can just concatenate the + onto the number. Was just curious if there was a html or css tag that would make numbers show up with a plus automaticly. Thanks again for the suggestion.

Re: Putting a + or - in front of a number in a table

Posted: Tue Nov 15, 2011 10:13 pm
by hallsofvallhalla
hmm not sure on that one, i think a if statement may be the option

Code: Select all

if($variable > 0){$modifier = "+";}else {modifier = "";}

echo "Rocks=" . $modifier . $info3['rocks'];

Re: Putting a + or - in front of a number in a table

Posted: Tue Nov 15, 2011 11:28 pm
by Jackolantern
I would honestly do it with Javascript, particularly if you are going to be having to process it on many different numbers in a table. I typically try to leave simple, presentation-oriented processing like that on the client-side, particularly if you are creating something that will demand Javascript be on, such as a game.

Re: Putting a + or - in front of a number in a table

Posted: Wed Nov 16, 2011 9:39 am
by Chris
hallsofvallhalla wrote:hmm not sure on that one, i think a if statement may be the option

Code: Select all

if($variable > 0){$modifier = "+";}else {modifier = "";}

echo "Rocks=" . $modifier . $info3['rocks'];
A short hand if would be handier and quicker for this. No need for defining a variable.

Code: Select all

echo 'Rocks=' . $var < 0 ? '-' : '' . $info3['rocks'] 
But as Jack said you might want to look into JavaScript.

Re: Putting a + or - in front of a number in a table

Posted: Wed Nov 16, 2011 4:36 pm
by hallsofvallhalla
very pretty!

Re: Putting a + or - in front of a number in a table

Posted: Wed Nov 16, 2011 10:04 pm
by Jackolantern
Ok, I did a couple of tests on my own, and I don't quite understand the problem. Unless I am missing something, or did not do something right, when I did my tests, a simple "+" stayed in front of the numbers I put into table cells without any CSS, JS or additional tags. Is this maybe an issue with a certain browser, or did I maybe do something different?

Re: Putting a + or - in front of a number in a table

Posted: Wed Nov 16, 2011 11:16 pm
by Chris
Reading over this topic again, I think you might be looking for the CSS content tag:

CSS

Code: Select all

.pos:before {
    content: '+';
}
HTML:

Code: Select all

<table>
    <tr>
        <th> Nº </th>
    </tr>
    <tr>
        <td class="pos">1</td>
    </tr>
    <tr>
        <td>-1</td>
    </tr>
    <tr>
        <td class="pos">69</td>
    </tr>
    <tr>
        <td class="pos">48</td>
    </tr>
    <tr>
        <td>-91</td>
    </tr>
</table>