weixin_33691598 2016-11-28 21:29 采纳率: 0%
浏览 8

MySQL查询工具提示

I have problem with tooltip.

I have some rows in table

echo '<td><center>'.$dana['material'].'</td>';
echo '<td><center>'.$zmiennacal.'</td>';
$mat=$dana['material'];

and now its my question, can I do a dynamic tooltip with MySQL query like this

$connect = mysqli_connect(CONNECTION QUERY);
$zapytanie = "SELECT iloscsurmaterialu, Produkt FROM receptaprodukt WHERE surowiec='$mat'"
or die(mysql_error());
$result = mysqli_query($connect, $zapytanie);

while($row = mysqli_fetch_assoc($result))
{
    echo  "'.$row['iloscsurmaterialu'].' - '.$row['Produkt'].'";
}

and take this result in tooltip, in second row in my table where is $zmiennacal

I'm trying some scripts but no one works :( (i know that it must be in ajax but I don't know that structure as PHP :P

Thank you in advance, please HELP

  • 写回答

1条回答 默认 最新

  • from.. 2016-11-28 21:39
    关注

    To use ajax. You need to have a 'event' that triggers the ajax. Maybe a hover will work?

    <?php
    //ajax php file
    $mat = $_POST['mat'];
    $sql = 'SELECT iloscsurmaterialu, Produkt FROM receptaprodukt WHERE surowiec = :mat';
    // to see where $dbh comes from: http://php.net/manual/en/pdo.construct.php
    $sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
    $sth->execute(array(':mat' => $mat));
    $result = $sth->fetchAll();
    $title = '';
    if(!$result){
        echo json_encode(array('success' => false));
        exit;
    }
    foreach($result as $row)
    {
        $title .= "'".$row['iloscsurmaterialu'].' - '.$row['Produkt']."'";
    }
    echo json_encode(array(
        'success' => true,
        'title' => $title
    ));
    exit;
    
    
    
    //jQuery post: https://api.jquery.com/jquery.post/
    $('body').on('hover', '#mytable td', function(){
        $td = $(this);
        $td.attr('title', 'Loading...');
        var mat = $(this).data('mat');
        $.post( "ajax/get_title.php", {"mat" : mat}, function( data ) {
            if(data && data.success){
                var title = data.title;
                $td.attr('title', title); //note that td is not specific, it will add title to all the tds. You need to select the correct one.
            }
        }, "json");
    });
    
    //Html
    $mat = $dana['material'];
    echo '<td data-mat = "'.$mat.'"><center>'.$mat.'</td>';
    echo '<td><center>'.$zmiennacal.'</td>';
    
    评论

报告相同问题?