Jump to content

Archived

This topic is now archived and is closed to further replies.

yanks6rule

Is an array the answer?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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",
}
);

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[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 array
echo '<pre>', print_r($data, true), '</pre>';
?>
[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
But i can't keep on declaring a new array while its looping. I want all of the results to be in "1" array.

Share this post


Link to post
Share on other sites
He's not declaring a new array, he's appending the current row of the returned dataset to the end of the current array.

Share this post


Link to post
Share on other sites
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 $plot
echo $plot[2]['y']; // echo's the 3 in the 'y' element of position #3 in $plot

or if you want to loop through all of them, you would use the examples already displayed above.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.