Shrewd Shrewd - 4 days ago 5
PHP Question

Tooltip with MySQL query

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

Answer

To use ajax

<?php
//ajax php file
$mat = $_POST['mat'];
$connect = mysqli_connect(CONNECTION QUERRY);
$zapytanie = "SELECT iloscsurmaterialu, Produkt FROM receptaprodukt WHERE surowiec='$mat'";
$result = mysqli_query($connect, $zapytanie);
$title = '';
if(!$result){
    echo json_encode(array('success' => false));
    exit;
}
while($row = mysqli_fetch_assoc($result))
{
    $title .= "'".$row['iloscsurmaterialu'].' - '.$row['Produkt']."'";
}
echo json_encode(array(
    'success' => true,
    'title' => $title
));
exit;



//jQuery post: https://api.jquery.com/jquery.post/
$.post( "ajax/get_title.php", {mat: 'somemat'}, 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.
    }
  $( ".result" ).html( data );
}, "json");
Comments