Search code examples
phpcsstooltip

php text in css tooltip


What I am trying to do is add information from extracted with php into a css tooltip. The text, instead of appearing inside the tooltip, appears next to the label tag I have. The tooltip appears over the label tag. Here my code so you can probably help me:

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<?php
//Connect to the database
require_once("dboconn.php");
$conn=ConnectionFactory::connect();

$query = "SELECT * FROM programmer";
$resultset = $conn->query($query);

function projects($prog_id){
    require_once("dboconn.php");
    $conn=ConnectionFactory::connect();

    $query2= "SELECT * FROM programmer pm INNER JOIN project p ON pm.programmer_id=p.programmer_id WHERE pm.programmer_id=:id";
    $resultset2= $conn->prepare($query2);
    $resultset2->bindValue(":id",$prog_id);
    $resultset2->execute();
    $isthere=false;

    while($row2 = $resultset2->fetch()){
        echo "Working on project ".$row2['project_id'].".";
        $isthere=true;
    }

    if(!$isthere){
        echo "No projects";
    }
}

while ($row = $resultset->fetch())
{
        echo "<p class='col'>";
        echo "<input type='checkbox' name='programmers[]' id='programmer".$row['programmer_id']."' value='".$row['programmer_id']."'>";
        echo "<a href='' class='help tip-below' data-tip='".projects($row['programmer_id'])."'><label for='programmer".$row['programmer_id']."'>".$row['programmer_name']." ".$row['programmer_surname']."</label></a>";
        echo "</p>";
}

?>

Thanks a lot for helping me out!


Solution

  • Your function projects() has no return value (instead you print it, which is bad practice btw)!

    Change

    while($row2 = $resultset2->fetch()){
        echo "Working on project ".$row2['project_id'].".";
        $isthere=true;
    }
    
    if(!$isthere){
        echo "No projects";
    }
    

    To something like this

    $result = "";
    while($row2 = $resultset2->fetch()){
        $result .= "Working on project ".$row2['project_id'].".";
    }
    
    if(empty($result)){
        return "No projects";
    }
    return $result;
    

    To clarify this, you can think of a function as a calculator maybe. You input specific data (an equation e.g.) and it returns something (the solution). What happens inside, you don't know and don't really care.

    Now back to PHP, the input is clear: the parameters ($prog_id), but your function has no return, instead it just prints something to the screen (what you really shouldn't do outside a development environment). To test this you can use

    var_dump( projects($some_id) );
    

    And it will return NULL (because no return was specified), but what you really wanted was a string of some sort.