Ways to display additional database details using onmouseover?

Latest Update: I have successfully connected to a MySQL database, retrieved information, and encoded it in JSON format.

<?php
$mysqli_db_hostname = "localhost";
$mysqli_db_user = "root";
$mysqli_db_password = "password";
$mysqli_db_database = "database";

$con = @mysqli_connect($mysqli_db_hostname, $mysqli_db_user, $mysqli_db_password,
 $mysqli_db_database);

if (!$con) {
 trigger_error('Could not connect to MySQL: ' . mysqli_connect_error());
}

$var = array();
$sql = "SELECT * FROM uploads";
$result = mysqli_query($con, $sql);

while($obj = mysqli_fetch_object($result)) {
$var[] = $obj;
}
echo '{"uploads":'.json_encode($var).'}';

?>

In the HTML code snippet below, you can see that height, brand, and model are displayed in a single row.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>  
$('#jsondata tr').mouseover(function(){
  var row = $(this).prop('id');
  $('#jsondata'+row).show();
}).mouseleave(function(){
  var row = $(this).prop('id');
  $('#jsondata'+row).hide();
 });
</script>

<table class="mGrid" id="jsondata">
<thead>
<th>height</th>
<th>brand</th>
<th>model</th>
</thead>
<tbody></tbody>
</table>
<div id="result">
<table class="mGrid" id="specific1">
<thead>
<th>email</th>
<th>height</th>
<th>location</th>
</thead>
<tbody></tbody>
</table>
</div>

<script type="text/javascript">
$(document).ready(function(){
var url="data_retrieval.php";
 $("#jsondata tbody").html("");
$.getJSON(url,function(data){
$.each(data.uploads, function(i,user){
var newRow =
"<tr>"
+"<td>"+user.height+"</td>"
+"<td>"+user.brand+"</td>"
+"<td>"+user.model+"</td>"
+"</tr>" ;
$(newRow).appendTo("#jsondata tbody");
 });
    $.each(data.uploads, function(i,user){
        var newdiv =
        '<table id="specific'+i+'"><tr><td>'+user.email+'</td><td>'+user.height+'</td><td>'+user.location+'</td></tr></table>';
        $(newdiv).appendTo("#result"); // Should be an DIV....
});
});
});
</script>

I am looking for a way to fetch the height, brand, and model data separately without affecting the retrieval of other information. Any suggestions on how to achieve this would be greatly appreciated. Thank you.

Answer №1

Give this code a shot:

//UPDATE2: Please test the code above and let my script take care of the rest! (comments)

$(document).ready(function(){
    var url="data_retrieval.php";
    $("#jsondata tbody").html("");
    $.getJSON(url,function(data){
        $.each(data.uploads, function(i,user){
            var newRow =
            "<tr>"
            +"<td>"+user.height+"</td>"
            +"<td>"+user.brand+"</td>"
            +"<td>"+user.model+"</td>"
            +"</tr>" ;
            $(newRow).appendTo("#jsondata tbody");
        });
        $.each(data.uploads, function(i,user){
            var newdiv =
            '<table id="specific'+i+'"><tr><td>'+user.DATASPECIFIC+'</td><td>'+user.DATASPECIFIC2+'</td><td>'+user.DATASPECIFIC3+'</td></tr></table>';
            $(newdiv).appendTo("#jsondataspecific"); // Should be wrapped in DIV....
        });
    });
});

//UPDATE: Included .mouseleave() to conceal it....

  $('#jsondata tr').mouseover(function(){
  var row = $(this).prop('id');
  $('#specific'+row).show();
  }).mouseleave(function(){
  var row = $(this).prop('id');
  $('#specific'+row).hide();
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="mGrid" id="jsondata">
<thead>
<tr>
<th>height</th><th>brand</th><th>model</th>
</tr>
</thead>
<tbody>
<tr id="1">
<td>5 feet</td><td>newbrand</td><td>55-v2</td>
</tr>
<tr id="2">
<td>15 feet</td><td>newbrand2</td><td>55-v3</td>
</tr>
<tr id="3">
<td>51 feet</td><td>newbrand3</td><td>65-v5</td>
</tr>
</tbody>
</table>
</div>

<table class="mGrid" id="specific1" style="display: none; background-color: green;">
<thead>
<tr>
<th>other info1</th><th>other info1</th><th>other info1</th>
</tr>
</thead>
<tbody>
<tr>
<td>This is specific1</td><td>This is specific1</td><td>This is specific1</td>
</tr>
</tbody>
</table>
<table class="mGrid" id="specific2" style="display: none; background-color: red;">
<thead>
<tr>
<th>other info2</th><th>other info2</th><th>other info2</th>
</tr>
</thead>
<tbody>
<tr>
<td>This is specific2</td><td>This is specific2</td><td>This is specific2</td>
</tr>
</tbody>
</table>
<table class="mGrid" id="specific3" style="display: none; background-color: yellow;">
<thead>
<tr>
<th>other info3</th><th>other info3</th><th>other info3</th>
</tr>
</thead>
<tbody>
<tr>
<td>This is specific3</td><td>This is specific3</td><td>This is specific3</td>
</tr>
</tbody>
</table>

Hello from Vienna

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What are some methods for preventing JavaScript function calls from the browser console?

In the process of developing a web application using HTML and JavaScript, I'm looking for a way to prevent users from accessing functions through their browser console in order to maintain fairness and avoid cheating. The functions I want to protect a ...

Vue.js seems to be leading me down a long and steady path of progress at a snail

I've exhausted all efforts to resolve the file paths for Home and App. I even turned to AI to help me out, but still no luck. Code snippet from main.js in the src folder: import Home from '@views/Home.vue'; // Using alias import App from ...

Having trouble positioning the right side image on the Bootstrap 4 Landing page

Struggling to incorporate bootstrap 4 into my sample project for the right side background. I'm facing some conflicts and attempts to align it on the right side are not successful. Does anyone know how to do this correctly? Thank you // Navbar scr ...

Using HTML within a JSON string in Java

I need to save the contents of a Java class called MyClass into a text file using JSON for encoding, instead of implementing the Serializable interface. I plan to utilize Google's Gson library, specifically the JsonWriter class. The structure of the M ...

Personalized HTML selection list

When a select option is too long, it stretches the container to accommodate its length. I have created a truncate function to limit the display to 20 characters and add ellipses to prevent this stretching. I have been searching for a way to show the entir ...

Validating items within an array in a JSON field in Postgresql

Is there a way to ensure that a postgres json field is validated in such a manner that every item within an array inside the json must contain specific properties? For instance, if the json field contains an array of objects labeled as contacts, I require ...

What is the solution for the error "BREAKING CHANGE: webpack < 5 used to automatically include polyfills for node.js core modules"?

I am trying to use the "web3" and "walletconnect/web3-provider" package in a Vue & Laravel 8 project. I have installed it using the npm i --save web3 @walletconnect/web3-provider command and then added the following code to import into ...

Guide to encapsulating JavaScript fetch within a function - dealing with unhandled promise rejection

I'm attempting to create a wrapper function for JavaScript's fetch command. I found the following example code on this post: function fetchAPI(url, data, method = 'POST') { const headers = { 'Authorization': `Token ${get ...

download image using React map method

I am trying to transfer an image from one useState variable to another when it is clicked const [photos, setPhotos] = useState([]); useEffect(() => { setPhotos(PhotoArray); }, []); This is the source image that I am using: export const PhotoArr ...

Importing an object into Three.js from Blender using JSON_KEYBOARD_SHORTCUT

I'm currently utilizing the Blender plugin to export JSON files, but I've encountered an issue where the texture of my object is not being exported. The materials section within the JSON file appears as follows: "materials" : [ { "DbgCo ...

An error was encountered with Jquery/Ajax when trying to serialize $(var) due to a syntax error: Unrecognized expression for the

Hello everyone. I'm currently tackling a challenge in my ASP.NET Web API project, as I encountered an error in the following code snippet: function LoadGraph(text) { console.log(typeof(text)); $.ajax({ url: "/api/Graph/LoadGraph", ty ...

How to incorporate an image within a div element without reliance on the <img> tag

I am dealing with HTML content <div> <img src="xyz.jpg"> </div> The challenge I am facing is fetching an image in hex format from a web service, converting it to JPEG, and displaying it inside a div. However, the <img src=""> ta ...

React js select all checkbox implementationIs this what you need? Let

I am working on creating a select-all checkbox feature where each product row has a checkbox. When a checkbox is clicked, it should capture the product id, variations, and count to calculate and display the total price of the selected products. I have su ...

"Integrating a PHP file with CanvasJS: A step-by-step guide

Hello everyone, I am new to posting here and still learning the ropes of javascript. My current challenge involves figuring out how to incorporate data from a PHP file into a CanvasJS file. <script src="http://canvasjs.com/assets/script/canvasjs.min.js ...

How is it possible for React to function within the views directory?

In the directory structure of my project, I have a React application housed within the views folder. The root folder contains the model, controller, and views directories. Within the views folder, there is a React app. I am unsure of how to start both si ...

Updating hidden input value dynamically using jQuery based on quantity selected

After creating a jQuery script to dynamically update a hidden input value based on the user's selected quantity, I encountered an issue: $('input[name^="quant"]').change(function () { updateTotal(); }); function updateTotal() { ...

Is there a way to incorporate a Laravel foreach loop within a JavaScript file?

I recently added a select-box using jQuery: <span onclick="createProduct()">Add New<i class="fa fa-plus"></i></span> <script> function createProduct() { var html = ''; html += ' <div clas ...

How can we use Mongoose .find to search with an array stored in req.params and respond with an array containing each value along with its

It would be great if a user could input multiple tags in a search field, and have them separated client-side (before making the .get call) to send over ajax with each keypress. While testing the API with Postman on the server-side, if the .get method retu ...

Implementing real-time style changes with Angular 6 through Environment Variables

Attempting to dynamically change styles in Angular 6 using environment variables has been a success for me. Here is how my file structure is organized: src -app -assets -environments -scss -theme1.scss -theme2.scss -_variables.scss -styles.sc ...

Endless loop of jquery textbox focus event

Currently, I am employing jQuery for validating textbox values. I have two textboxes - txt1 and txt2. For this purpose, I have created a jQuery function: $("#txt1").blur(function () { if ($("#txt1").val() == "") { $("#scarrie ...