Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Applying javascript processing (string.split function) to database row in PHP

    Hi all,

    I'm trying to save my database columns as strings so I can use the split function to get an array of each item that is separated by a comma.
    So far my code looks like this:

    Code:
    <?php
    			//player chose to load an existing game, if one exists load it, otherwise start a new game
    			$mysqli = new mysqli("localhost", "root", "", "webdevfinal");
    			if($mysqli->connect_errno) 
    			{
    				echo "Connection Failed: " . $mysqli->connect_errno;
    			}
    			
    			$result = $mysqli->query("SELECT r1 FROM gamefiles");
    			
    			while($row=$result->fetch_assoc())
    			{
    				echo(var r1array = $row.split(','));
    			}
    As evidenced by the code, I got a little confused around the echo statement. I know echo essentially just sends its contents to be executed in javascript, but will the javascript portion have access to the "r1array" variable once the php block is closed? I just want to populate a javascript array with the contents of r1array, which is each individual element in $row. Any suggestions on how to approach this are greatly appreciated.

    Thanks in advance.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    I'm confused by your post.

    PHP is "server side". So when your script executes, everything that is PHP is processed BEFORE it is served to you (the user who is viewing the web page on their browser). That means you can have PHP actually generate the javascripting itself, literally. When the page is served to your browser, you'll see the HTML and javascripting ... you won't even know that PHP has actually created it dynamically.

    Here's what you do ...

    Create a "mock" test page that has all of the javascripting and arrays (for javascripting). Just code it all by hand and populate your array with fake data. Get your page working exactly how you want ... all of the HTML, all of the javascripting. When you get that far, post it so I can see it ... so I can see it actually working.

    Then, I'll show you PHP that can render the same page, creating the javascripting that you "hand coded with fake data". PHP will create the javascripting and the array that you did "by hand", except it will do it dynamically using your database.

    In summary, you are not looking at this the right way (as far as I can tell). PHP is "server-side", javascripting is "client-side". Your browser (client-side) has no idea the web page it is using (with javascripting) was created by a PHP script.
    Last edited by mlseim; 04-18-2014 at 09:56 PM.

  • Users who have thanked mlseim for this post:

    despondent (04-19-2014)

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    If you are using PHP to generate the JavaScript then the echo statement needs to generate the complete JavaScript statement. Also echo isn't a function and it outputs a string not JavaScript.

    The following will output a JavaScript statement for creating an array containing all the values from $row

    echo 'var r1array = ['.$row.split(',').'];';
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    despondent (04-19-2014)

  • #4
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for the suggestions, I managed to get the database operations running smoothely thanks to them.

    One final question though to avoid making a new thread:

    I've been using a system of using echo to create html elements in <?php?> tags, then reading their value by ID in javascript, but for some reason I can't even get basic reading to occur. I've been staring at this way too closely so additional perspective would really help.
    Below is an exerpt of test code I made to see if I was even reading element values properly, but the console keeps logging "undefined". Since this is at its most basic usage and it's not working, I must be missing something fundamental?

    Code:
    <html>
    
    <head>
    </head>
    <body>
    	
    	<data id='r1p' value='10'>
    
    <script>
    var r1 = document.getElementById('r1p').value;
    			console.log(r1);
    </script

  • #5
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for the suggestions, I managed to get the database operations running smoothely thanks to them.

    One final question though to avoid making a new thread:

    I've been using a system of using echo to create html elements in <?php?> tags, then reading their value by ID in javascript, but for some reason I can't even get basic reading to occur. I've been staring at this way too closely so additional perspective would really help.
    Below is an exerpt of test code I made to see if I was even reading element values properly, but the console keeps logging "undefined". Since this is at its most basic usage and it's not working, I must be missing something fundamental?

    Code:
    <html>
    
    <head>
    </head>
    <body>
    	
    	<data id='r1p' value='10'>
    
    <script>
    var r1 = document.getElementById('r1p').value;
    			console.log(r1);
    </script

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Are you trying to do something like this?
    Using HTML5 custom data attributes to store data on HTML elements - Robert's talk

    Otherwise, without using HTML5, you can use a <div> class and keep it hidden. Javascript will use it though, but it won't show on the page.

    <style>
    .myValue {
    display: none;
    }
    </style>

    <div class="myValue">10</div>


    document.getElementsByClassName('myValue')[0].innerHTML

  • Users who have thanked mlseim for this post:

    despondent (04-19-2014)

  • #7
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts
    That actually seemed to mostly fix it. Only issue now is for some reason I can't access variables within an echo statement... I was under the impression this was proper use of php with echo, but am I mistaken?:

    Code:
    $result = $mysqli->query("SELECT `r1` FROM `gamefiles` WHERE 1");
    				while($row=$result->fetch_assoc())
    				{
    					echo ("<div class='htmlValues' id='r1p'>" . $row[0] . "</div>");
    				}
    				$result->free();
    When I console.log the innerHTML of id'r1p', I get returned a literal
    Code:
    " . $row[0] . "
    Am I screwing up the syntax with echo somehow?

  • #8
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    I wonder if this:

    echo ("<div class='htmlValues' id='r1p'>" . $row[0] . "</div>");

    Should be:

    echo ("<div class='htmlValues' id='r1p'>" . $row['r1'] . "</div>");


    Here's another way to test it .... display the value and exit (stop the script) ...

    Code:
    $result = $mysqli->query("SELECT `r1` FROM `gamefiles` WHERE 1");
    				while($row=$result->fetch_assoc())
    				{
    					echo ("<div class='htmlValues' id='r1p'>" . $row[0] . "</div>");
                                            exit;
    				}
    				$result->free();

    Are you expecting only one row to be found for each query?
    I question that because of this: WHERE 1

    Maybe you're not getting any results, or you're getting all results?
    I'm not sure what "WHERE 1" will do for the query.


    .
    Last edited by mlseim; 04-19-2014 at 05:10 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •