Jump to content


Photo

Help With Jquery And Loops


  • Please log in to reply
2 replies to this topic

#1 jbwebdesign

jbwebdesign

    Advanced Member

  • Member
  • PipPipPipPip
  • 169 posts
  • LocationNew York

Posted 16 April 2013 - 09:52 AM

hello everyone, i am working on a script and i want to do the following

i have a table that looks like this...

<table>
     <tr>
          <td>Tuesday</td>
          <td>12/31</td>
          <td>New Years Eve</td>
          <td>Test</td>
          <td>Apples</td>
          <td>Oranges</td>
          <td>Bananas</td>
     </tr>
<tr>
          <td>Monday</td>
          <td>12/30</td>
          <td>New Years Eve EVE</td>
          <td>Test</td>
          <td>Apples</td>
          <td>Oranges</td>
          <td>Bananas</td>
     </tr>
<tr>
          <td>Sunday</td>
          <td>12/29</td>
          <td>New Years Eve Eve EVE</td>
          <td>Test</td>
          <td>Apples</td>
          <td>Oranges</td>
          <td>Bananas</td>
     </tr>
</table>

I want to convert EACH TR into a PHP ARRAY

I want the code to look something like this and it does in the console, however i need to output all of it into a string:
<?php
array( 
	"data" => 0, 
		array(
			"Tuesday",
			"12/31",
			"New Years Eve",
			"Test",
			"Apples",
			"Oranges",
			"Bananas"
		),
		
	  "data" => 1, 
		array(
			"Monday",
			"12/30",
			"New Years Eve EVE",
			"Test",
			"Apples",
			"Oranges",
			"Bananas"
		),
		
		"data" => 2, 
		array(
			"Sunday",
			"12/29",
			"New Years Eve Eve EVE",
			"Test",
			"Apples",
			"Oranges",
			"Bananas"
		),		 
	);
?>


My Current Javascript code is as follows:
var count = 0;
	$("#output").find('tr').each(function(){
  		count++
		var array_str = 'array(' + '"data" => "' + count + '", array(';
		console.log(array_str);
		
		$(this).find('td').each(function(value){
			var str = '"'+ value +'" => "'+$(this).text()+'",'; //this makes the php array keys
			//var str = Array(value);
			console.log(str);	
		});
		
		console.log("));");
	});	

Can anyone please help me to output the entire console code that gets returned into a string or into a DIV.

thanks

Edited by jbwebdesign, 16 April 2013 - 09:54 AM.

  • 0

#2 Ben

Ben

    Administrator

  • Administrators
  • 5,682 posts
  • LocationGainesville, Florida

Posted 16 April 2013 - 10:53 AM

Just a quick comment... Are you sure you can do this? PHP code is processed before/as the page loads. Javascript code is only processed after the page loads. For example, you can use PHP to set Javascript variables, but I'm pretty sure you can't use Javascript to create PHP (short of passing a string to PHP via AJAX and then PHP parsing it into an array). Unless you meant this?
  • 0
Benjamin Falk
Falken Creative : Twitter

#3 jbwebdesign

jbwebdesign

    Advanced Member

  • Member
  • PipPipPipPip
  • 169 posts
  • LocationNew York

Posted 16 April 2013 - 10:55 AM

well what i am trying to do is take the TABLE and convert it into a php array so that i can insert it into a MySQL database.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

★★★★★ 5 Star Rated Web Developer Course - check it out now!