Jump to content

Is an array the answer?


yanks6rule

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.
Link to comment
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",
}
);
Link to comment
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
Link to comment
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]
Link to comment
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?
Link to comment
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]
Link to comment
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.
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.