Page 1 of 1

how can you this with php and mysql?

Posted: Mon Dec 03, 2007 11:13 pm
by scheinarts
scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi guys... today I came up with this challenge, and after a few hours I couldnt come up with an answer... So I hope someone can shed some light. 

I am populating a <select> field with javascript going through a for loop for someArray that holds the values to be populates. In turn, this javascript array variable was populated by php reading from a mysql database... like this.. while(query stuff){<option>mysql values</option>}. No problem until here.

In the mysql database i have 2 tables: clients and shipping_instructions. So here, one client have have many shipping instructions.

I am already populating a javascript called clientsArray with the data from the mysql query. Now I need to dynamically create and populate one a javascript array for each client so that it holds all the shipping instruction values for that client... like this...

[syntax="javascript"]ClientOneArray = new Array("454rr54r", rr4r544r", 6y7y6yy7");

ClientTwoArray = new Array("454rr54r", rr4r544r", 6y7y6yy7");

ClientThreeArray = new Array("454rr54r", rr4r544r", 6y7y6yy7");
The weird looking values are fictional shipping instruction numbers that exist for that client.

How would I set up the mysql query to populate those values for every client.

NOTE - Obviously in the shipping_instructions table each record has the client id so thats how I join the tables

I would really appreciate any input on this... One million thanks!


scottayy | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Dec 04, 2007 12:12 am
by s.dot
Are you needing help with the query? Or how to join tables? Or writing the results into a javascript array?

What have you tried, and what were the results?

Posted: Tue Dec 04, 2007 12:19 am
by scheinarts
Hi! Sorry about that... since it wasnt really code... just some pseudo code i came up with.

I am needing help with the query and the while loop that will dynamically create those javascript arrays... You see, the problem is that I am only getting ONE shipping_instruction per client, when in fact each client has numerous shipping instructions.

Posted: Tue Dec 04, 2007 12:20 am
by devendra-m
scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

<?
	$shipping_instructions[0]=array('ab','bc','dv');
	$shipping_instructions[1]=array('re','bg','ok');
	$shipping_instructions[2]=array('tr','wq','lk');
	$shipping_instructions[3]=array('uh','df','gh');
	$n=4;
	foreach($shipping_instructions as $key=>$value)
	{	
		$shipping_instructions[$key]=implode(',',$shipping_instructions[$key]);
	} 

?>

Code: Select all

<script>
function test()
{
	<?
for($i=0;$i<$n;$i++)
{
	
?>
eval("client<?=$i?>=new Array(<?=$shipping_instructions[$i]?>)");

<? }
?>
	
}
</script>

scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Dec 04, 2007 12:34 am
by scheinarts
hmmm... Is $n the number of shippings instructions for all clients, which could be thousands, or the just the number of shipping instructions for one client?

The thing is getting all the shipping_instructions for one customer and their could be hundreds of customers. So php has to echo hundreds of javascript arrays each containing all the shipping instructions for each client. Then name of these created arrays should be for example

Code: Select all

$clientName . "Arr"