Search code examples
phphtmlmysqlbuttonsocial-media-like

PHP like button(counter) inside a table


I have a table made in php and inside that table I show some content from my database. I have created a simple button (similar to like) in every td and I want it to increase by 1 with every hit. Here is the line with the button:

echo "<tr align=\"center\"> 
            <td>$nameTemp</td> 
            <td>$categoryTemp</td> 
            <td>$textTemp</td> 
            <td>$likesTemp <input type= 'submit' value='like' name='likes'></td>
            <td>$usernameTemp</td> 
            <td> <button type=\"button\" style=\"cursor:pointer\" onclick=\"openWindow('$multimediaTemp','div1')\">View me</button> </td> 
        </tr>
        ";

$likesTemp is the total likes var


Solution

  • I made you a sample of what you needed :

    • Button will increase the $_SESSION['likeTemp'].

    (The variable will be incremented each time the submit button is pressed).

    Code :

    <?php
    session_start();
    
    if(isset($_POST['likes']))
        ++$_SESSION['likeTemp'];
    
    ?>
    <form method="post">
    <table>
    <td><?php echo "Blabla";  ?></td> 
    <td><?php echo "Trololo"; ?></td>
    <td>
    <?php 
        if(isset($_SESSION['likeTemp']))
            echo $_SESSION['likeTemp'];
        else
        {
            $_SESSION['likeTemp'] = 1;
            echo $_SESSION['likeTemp']; 
        }
    ?>
    <input type="submit" value="like" name="likes"></td>
    </table>