hi admin or others i want to use this script to display trading comment with number of comment in post eg 54=”veryhot” please check this script and correct error thanks

<script>

function myFunction()
{
var count = <?php echo $comment_num; ?>; 
if (count>=10) { document.getElementById( "Trending!!" );} 
else if ( count >= 6) { document.getElementById( "Super HOT!!" );} 
else if ( count >=5 ) { document.getElementById( "Extra HOT!!" );} 
else if ( count >=4 ) { document.getElementById( "HOT!!" );} 
else if ( count >=3 ) { document.getElementById( "New!" );} 
else if ( count >=2 ) { document.getElementById( "New!" );} 
else if ( count >=1 ) { document.getElementById( "New!" );} 
else { document.getElementById( "1.0" );
}
</script>

this is the idea i have but not good in js please help me out

Recommended Answers

All 2 Replies

Why are you mixing PHP and Javascript up like that? And why are you using document.getElementByID? Also, putting spaces in id attributes is invalid.

Clue, just use PHP for this, perhaps a case statement:

switch (true) {
    case $count <= 6:
        $trend = 'super-hot';
        break;

    case $count <= 4:
        $trend = 'hot';
        break;

    case $count <= 1:
        $trend = 'new';
        break;

    default:
        $trend = 'lame';
        break;
}

Hi,

as pty said, mixing PHP and JavaScript code like this is not the right way.

And document.getElementById(ID_HERE) will return an object with the HTML element who has the ID_HERE value. (see more on this here: https://www.w3schools.com/jsref/met_document_getelementbyid.asp)

You should either do this fully in PHP, as pty suggested, or pass the number of comments to JavaScript and have the switch/if elseif there. More info here: https://www.w3schools.com/js/js_switch.asp and here: https://www.w3schools.com/js/js_if_else.asp

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.