SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jQuery: mysql row data

    Hello

    I have a .load that executes a php mysql query from another page.

    This query displays all the data of the database.

    Each result is wrapped around a div.

    <div id ="post<? echo $row['post_id']; ?>">
    the id of the div depends on the post id of the result.

    How do I put the data of $row['post_id'] in my javascript?

  2. #2
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can assign post_id to a javascript variable like this

    Code JavaScript:
    <script type="text/javascript">
     
    var myVar = <?php echo row['post_id'];  ?> ;
     
    alert(myVar);
     
    </script>

  3. #3
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, I cannot do this because ajax calls do not process inline javascript.

  4. #4
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You can use JSON - using json_encode on the PHP side, then using a getJSON on the jQuery side:

    Example:
    PHP - test.php:
    Code PHP:
    <?php
     
    	echo json_encode(
    		array(
    			array(
    				'id' => 1,
    				'title' => 'Test1'
    			),
    			array(
    				'id' => 2,
    				'title' => 'Test2'
    			),
    			array(
    				'id' => 3,
    				'title' => 'Test3'
    			)
    		)
    	);
     
    ?>

    HTML/JS
    Code JavaScript:
    <!DOCTYPE html>
    <html>
    	<body>
    		<div id="testDiv"></div>
     
    		<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
    		<script>
    		$.getJSON(
    			'test.php',
    			function(data) {
    				var items = '';
    				$.each(data, function(i) {
    					items +='<p id="post_' + data[i].id + '">' + data[i].title + '</p>';
    				});
    				$('#testDiv').html(items);	
    			}
    		);		
    		</script>
    	</body>
    </html>

  5. #5
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, thank you for your response.

    How I would I use json_encode on a while loop?

  6. #6
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    pretty much like the getJSON example I showed before:

    - Call an empty array before the while loop
    - Then populate the array in the loop
    - Then encode the array when your out of the loop.

    Here is another example using an sqlite database that can be used with the HTML example above:
    Code PHP:
    <?php
     
    	error_reporting(-1);
     
    	$db = new PDO('sqlite::memory:');
    	$db->exec('CREATE TABLE test(id INTEGER PRIMARY KEY, title VARCHAR NOT NULL UNIQUE)');
    	$db->exec('INSERT INTO test VALUES(1, "Title 1")');
    	$db->exec('INSERT INTO test VALUES(2, "Title 2")');
    	$db->exec('INSERT INTO test VALUES(3, "Title 3")');	
     
    	$arr = array();
    	foreach( $db->query('SELECT * FROM test', PDO::FETCH_ASSOC) as $row ) {
    		$arr[] = $row;
    	}
    	echo json_encode($arr);
     
    ?>

  7. #7
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, thank you for your response. I have it working now.
    Last question, I noticed all the html for the output is in the javascript. Is there a way to put it outside the javascript?

    Thanks


Bookmarks

Posting Permissions

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