Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using JavaScript, PHP and HTML for my application. I have a SQL database that contains tweets. I need to query from database and display tweets after the keyword (connecting to a hyper tree) is clicked. I have researched and tried to include an external php file (generatetweets.php) inside myFunction() at index.php. However it's not working. Anyone can enlighten me or any reference for me to guide? Thank you in advance.

Extracted example2.js, where child.name refers to the keyword from hyper tree

JavaScript
onComplete: function() {
            //Log.write("done");

            //Make the relations list shown in the right column.
			//ONCLICK FUNCTION FOR KEYWORDS TO LOAD RELATED TWEETS
            var node = ht.graph.getClosestNodeToOrigin("current");
            var html = "<div><b>Keyword: " + node.name + "</b></div>";
            html += "<ul>";
            node.eachAdjacency(function(adj){
                var child = adj.nodeTo;
				var childName=child.name;
				html += '<a önclick="myFunction('+'''+child.name+'''+')">'+child.name + '</a>;
				
            });
            html += "br />";
            $jit.id('inner-details').innerHTML = html;
}



generatetweets.php

PHP
<?php
// connect to the database
include "mysqli.connect.php";

// create your SQL statment to retrieve everything from
// the users table
$sql = "SELECT * FROM post WHERE content LIKE '%school%' ORDER BY date DESC";

// run the query
$result = $mysqli->query($sql);

// check for error
if ($mysqli->errno)
{
  error_log($mysqli->error);
  echo "<br />Something's wrong";
  exit();
}


?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<head>
</head>
<body>
    <?php
        // Check if there are records in the first place
            if ($result->num_rows < 1)
                {
                    echo "<h3>No records found</h3>";
                }


            // Iterate through the records
            $counter = 0;
            // Use fetch_array to get rows returned one at a time
            while($row = $result->fetch_array(MYSQLI_ASSOC))
                {
    ?>  
                <table>
                    <tr id="tweetlist">
                        <td style="width:50px">
                            <!-- name goes here -->
                            <!--<img id="img<?=$counter?>" style="padding-right:5px" src="<?=$row["displaypicture"]?>"></img>-->

                        </td>
                        <td>
                            <!-- email textfield goes here -->
                            <span id="date<?=$counter?>" style="color:#CCFF33">[<?=$row["date"]?>] </span>
                            <span id="name<?=$counter?>">@<?=$row["username"]?>: </span>
                            <span id="emailTxt<?=$counter?>"><?=$row["content"]?> <span/>
                        </td>
                    </tr>
                </table>
                <br/>
                <?php
                    $counter++;
                }
                    $result->free();
                    $mysqli->close();
                ?>

</body>




Extracted index.php, to display tweets in div "demo"

PHP
<body onload="init();">

<!-- Header -->
<div id="header" class="container">

    <!-- Logo -->
    <h1 id="logo"><a href="index.php">Mood</a></h1>  <!-- logo from style-n1.css -->

    <div id="center-container">
        <div id="infovis"></div>    
    </div>

    <div id="right-container">
        <div id="inner-details"></div>
        <div id="log"></div>
        <div id="node_name"></div>  
        <div id="demo" style="padding-removed 50px">

            <script type="text/javascript">
                function myFunction() {

                    $("#demo").load("generatetweets.php");  

                }
            </script>
        </div>
    </div>
</div>

</body>





Hypertree is referenced from http://philogb.github.io/jit/static/v20/Jit/Examples/Hypertree/example2.html
Posted
Updated 24-Jul-14 16:23pm
v4
Comments
nice_hand23 8-Aug-14 4:18am    
There's a missclick on line 13 of your first code-box:
onclick="myFunction(...)..." instead of "önclick=..."

1 solution

As far as I know, this method will be easy and efficient to implement.
Inside the myFunction(), send a XMLHTTPREQUEST to that php page. And in that php page, where you are retrieving from DB, echo the HTML code that you want to display(this will contain the tweets and infos). Get this response in the Javascript function and add it to an empty HTML element in that page where you want to display.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900