Converting my SQL data into a JavaScript array

This code snippet was extracted from the website Dynamic Drive .com showcasing a Pausing up-down Scroller (text animation):

Is there a way to fetch text data from a MySQL table instead of manually inputting it as an array, allowing for easier content modifications?

var pausecontent=new Array()
pausecontent[0]='<a href="http://www.javascriptkit.com">JavaScript Kit</a><br />Comprehensive JavaScript tutorials and over 400+ free scripts!'
pausecontent[1]='<a href="http://www.codingforums.com">Coding Forums</a><br />Web coding and development forums.'
pausecontent[2]='<a href="http://www.cssdrive.com" target="_new">CSS Drive</a><br />Categorized CSS gallery and examples.'

Answer №1

To transform your mysql output into a JSON object, you can utilize the following code snippet:

$sth = mysql_query("SELECT ...");
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
 $rows[] = $r;
}
print json_encode($rows);

Once converted, you can access this result through ajax requests.

If you need guidance on how to display the encoded results, check out this resource JSON encode MySQL results then read using jQuery

Answer №2

What do you think of this piece of code?

<?php
$server_name="localhost";
    $db_user="sql_user";
    $db_pass="password";
$db="mydb";
mysql_connect($server_name, $db_user, $db_pass);
mysql_select_db($db);


$sql = "SELECT text FROM posts";
$result = mysql_query($sql) or die ('Query Error: ' . mysql_error());

while ($results = mysql_fetch_array($result))
$texts[] = $results;}

?>


<div id="map_canvas" style="width: 600px; height: 400px"></div> 

 <script type="text/javascript">

  var content = document.getElementById("<?=$texts?>");

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

Eliminating rows from a DataTable through an Ajax request

I have a DataTables table from datatables.net with a custom column containing icons for various actions. One of these actions is deleting a row without reloading the data. I'm looking for a built-in function to remove datatable rows locally after dele ...

Using PHP to place JSON information into MySQL database

I'm having trouble inserting this JSON data into my MySQL database. Can someone please help me troubleshoot this issue? Here is the JSON data: { "status": 1, "response": { "Aviator Goggles": { "price": 1009, "quantity" ...

Find out which way the user is scrolling in your React js application

I'm struggling to determine whether the scroll event is moving up or down, and I haven't been able to find a solution yet. import React, { useState, useEffect } from "react"; import { Link } from "react-router-dom"; const Nav ...

Loading complex models with Three.js

Whenever I attempt to load a significantly large file utilizing the appropriate loaders provided by the library, the tab in which my website is running crashes. Despite trying to implement the Worker class, it doesn't seem to resolve the issue. Here i ...

What is the correct way to customize colors for specific components in Material-ui?

Struggling with theming in Material-UI, particularly when it comes to customizing element colors. Some elements default to 'theme.palette.main.dark' and I want to override this behavior. For example, the TextField and SpeedDial components automa ...

Unable to update the y formatter for tooltip on Apexcharts

My current issue involves a seemingly simple task. I have created a basic formatter method to convert numbers into currency values. It appears to be functioning properly based on the y axis values being displayed with formatting like "$8,000". However, I a ...

What is the process of integrating Bootstrap into an ExpressJS project?

Working on a project with nodejs and Express framework, I am looking to incorporate Bootstrap locally. I have included the following in the <head> of my index file: <script language="javascript" src="../node_modules/bootstrap/dist/js/bootstrap.mi ...

Struggling with updating a user in MongoDB using findOneAndUpdate and encountering a frustrating internal server error 500?

Presently, I am in the process of developing an API that is designed to update the current user in mongoDB based on the data provided in the request. However, whenever I execute findOneAndUpdate(), I encounter a 500 internal server error. (I will outline ...

es-lint is issuing a warning about the presence of multiple modules within the node_modules directory that have names differing only in their casing

After reviewing all my import statements, I found that everything looks correct. The only unusual import I have is react-bootstrap, which I import as: import { Jumbotron, Button } from 'react-bootstrap'; I'm using the Jumbotron and Button ...

Angular Component - Array missing initial value in @Input property

Having trouble transferring values between components? I'm currently dealing with a situation involving two components: report-form and comment-form. The report form contains an array of comments, displaying a list of comments and a button for each on ...

In PHP/HTML, if the URL is domain.co.uk/?debug, then the following action will

Apologies for what may seem like a basic question, but I've been searching for a clear answer with no luck! My goal is simple - when someone goes to , I want to show extra details, like the code version or any other notes I include. Once again, sorr ...

Node.js Internal Server Error

Currently in the process of setting up a deployment on Heroku, everything runs smoothly locally but encountering an error on Heroku. at=info method=GET path="/" host=pure-cliffs-68507.herokuapp.com request_id=66cd6aa8-209d-4065-88ce-ad201ce6e713 fwd="210 ...

Using callback functions to handle parameters in GET requests

Hey there, I'm currently diving into the world of callback functions and I have a burning question that needs clarification. Adding event listeners seems easy enough: $0.addEventListener("click", function(event){console.log(event)}); When you click s ...

Information is cleared before it is transmitted

Let's begin with the following code: JavaScript: function keyPressed(e) // function for key press event { if (e.keyCode == 13) // 13 represents the enter key { $(this).val(""); } } $(document).ready(function () { $(' ...

Using VueJs components within a ReactJs application is a seamless process that can enhance the functionality

After creating a web component using VueJs and compiling it with the yarn build command, I decided to integrate it into a ReactJs project. However, every time I attempted to import the web component, the ReactJs application would break and display an error ...

I seem to be having trouble using my markers on my istamap

function initialize() { var mapProp = { center:new google.maps.LatLng(51.508742,-0.120850), zoom:5, mapTypeId:google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("googleMap"),mapProp); var marker = new ...

Establishing a connection to a website using webosocket without relying on a public

I have limited knowledge about websockets, but I do know how to establish a connection like this: const CHAT_URL = 'ws://echo.websocket.org/'; However, what should be done when a website does not have a public websocket API? How can the data be ...

Searching for the lowest and highest values in an array using pointers

My goal is to determine the minimum and maximum values in a given array by passing its size as a parameter and using pointers for the min and max values. However, it seems like something is off with my approach. What am I missing? void FindMinMax(const int ...

Unable to activate focus() on a specific text field

It's quite peculiar. I'm working with a Sammy.js application, and my goal is to set the focus on a text field as soon as the HTML loads. Here's the CoffeeScript code snippet I've written: this.partial('templates/my-template.jqt&ap ...

Guide to placing the cursor at a specified position within an editable content div

I am struggling with a div <div id="user_status" class="status expanddiv" onkeyup="username_Search('New','user_status');" contenteditable="true" data-text="What's on your mind?..."> Some Text will be inserted here. </div ...