yanks6rule Posted October 23, 2006 Share Posted October 23, 2006 Hey everyone, I have just started getting back into PHP programming and I have hit a snag. I am trying to create an array of X,Y data, but I am not sure this is possible. I have two fields in my MySQL db that are named x_coor and y_coor. I want to loop through my db and pull these out as X,Y data and have the output be in an array or something similar that will give me this:$plot[0] = (x_coor(row1), y_coor(row1));$plot[1] = (x_coor(row2), y_coor(row2));and so on and so forth. Is this possible or do I need to come up with a better way to do this. Quote Link to comment Share on other sites More sharing options...
ak7861 Posted October 23, 2006 Share Posted October 23, 2006 Hi,I have a similar question. I have a while loop which takes data from a mysql query. Is it possible to loop results in an array? For eg.$category = array(while ($get_cat_sections_row = mysql_fetch_array($get_cat_sections)){"$get_cat_sections_row["cat"]" => "$get_cat_sections_row["cat"].php",}); Quote Link to comment Share on other sites More sharing options...
yanks6rule Posted October 23, 2006 Author Share Posted October 23, 2006 So I was able to print an array of arrays after working through some steps. I was wondering if anyone could help me loop through this data to output the data I am looking for. Here is my code that creates the multidimensional array:while($row = mysql_fetch_array($sql)){$plot[] = array('x' => $row[1], 'y' => $row[2]);}and the output is this:Array ( [0] => Array ( [x] => 160 [y] => 61 ) [1] => Array ( [x] => 91 [y] => 145 ) [2] => Array ( [x] => 83 [y] => 151 ) )I was wondering how I can loop through this data to get $plot[0] = (160, 61)I was trying foreach statements but not seemed to work.Thanks,Yanks Quote Link to comment Share on other sites More sharing options...
.josh Posted October 23, 2006 Share Posted October 23, 2006 this should work for both of your situations:[code]<?php // example query: $sql = "select x_coor, y_coor from coords"; $result = mysql_query($sql) or die(mysql_error()); // make the array while ($list = mysql_fetch_array($result)) { $coords[] = array('x_coor' => $list['x_coor'], 'y_coor' => $list['y_coor']); } // example output: $i = 0; while($coords[$i]){ foreach($coords[$i] as $key => $val) { echo"$key : $val <br>"; } $i++; } // another example output: foreach($coords as $val) { echo "x: {$val['x_coor']} y: {$val['y_coor']} <br>"; }?>[/code] Quote Link to comment Share on other sites More sharing options...
ak7861 Posted October 23, 2006 Share Posted October 23, 2006 What if i want to keep one array for all the results.. for eg.$subcat = array("blah" => "blah.php","blah" => "blah.php","blah" => "blah.php","blah" => "blah.php","blah" => "blah.php","blah" => "blah.php",);So basically the looping will be in the array string. Any idea? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 23, 2006 Share Posted October 23, 2006 [quote author=Crayon Violent link=topic=112397.msg456131#msg456131 date=1161579444]this should work for both of your situations:[code]<?php // example query: $sql = "select x_coor, y_coor from coords"; $result = mysql_query($sql) or die(mysql_error()); // make the array while ($list = mysql_fetch_array($result)) { $coords[] = array('x_coor' => $list['x_coor'], 'y_coor' => $list['y_coor']); } [/code][/quote]Simpler is[code]<?php$sql = "select x_coor, y_coor from coords"; $result = mysql_query($sql) or die(mysql_error()); // make the array while ($list = mysql_fetch_assoc($result)) { $coords[] = $list; }?>[/code] Quote Link to comment Share on other sites More sharing options...
ak7861 Posted October 23, 2006 Share Posted October 23, 2006 I dont think you understand what im looking for. I want to select all the results from a certain table and put it into the array. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 23, 2006 Share Posted October 23, 2006 [code]<?php$sql = "select * FROM certainTable"; $result = mysql_query($sql) or die(mysql_error()); // make the array $data = array(); while ($list = mysql_fetch_assoc($result)) { $data[] = $list; }// to view arrayecho '<pre>', print_r($data, true), '</pre>';?>[/code] Quote Link to comment Share on other sites More sharing options...
.josh Posted October 23, 2006 Share Posted October 23, 2006 yeah you're right barand. i had a fubar moment. i actually do it that way, lol. I think i just c/ped his example and worked from there or something.[quote author=ak7861 link=topic=112397.msg456177#msg456177 date=1161594419]I dont think you understand what im looking for. I want to select all the results from a certain table and put it into the array. [/quote]that's exactly what we've done. in my example $coords and in barand's example $data - that's the array. Quote Link to comment Share on other sites More sharing options...
ak7861 Posted October 23, 2006 Share Posted October 23, 2006 But i can't keep on declaring a new array while its looping. I want all of the results to be in "1" array. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted October 23, 2006 Share Posted October 23, 2006 He's not declaring a new array, he's appending the current row of the returned dataset to the end of the current array. Quote Link to comment Share on other sites More sharing options...
.josh Posted October 23, 2006 Share Posted October 23, 2006 it IS in one array. a multi-dimensional array. Just like you specified. there are 2 things happening in that loop:a) you are declaring a new [i]position[/i] in your main array like $plot[0], $plot[1], $plot[2], $plot[3], etc... b) you are inserting an array of information (x and y coords) in each position of that array. the while loop is in essence does this:// remember arrays start at 0 not 1$plot[0] = array('x' => 1, 'y' => 1);$plot[1] = array('x' => 2, 'y' => 2);$plot[2] = array('x' => 3, 'y' => 3);$plot[3] = array('x' => 4, 'y' => 4);...$plot is your one array. for each of the $plot's elements you have another array with two elements, one for your x and one for your y. This makes it a multi-dimensional array. So let's say you want to echo the 3rd one (above it is the x = 3, y = 3), you would do so like this:echo $plot[2]['x']; // echo's the 3 in the 'x' element of position #3 in $plotecho $plot[2]['y']; // echo's the 3 in the 'y' element of position #3 in $plotor if you want to loop through all of them, you would use the examples already displayed above. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.