Jump to content


Photo

Is an array the answer?


  • Please log in to reply
11 replies to this topic

#1 yanks6rule

yanks6rule
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 23 October 2006 - 04:00 AM

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.

#2 ak7861

ak7861
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 23 October 2006 - 04:20 AM

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",
}
);

#3 yanks6rule

yanks6rule
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 23 October 2006 - 04:52 AM

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

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 23 October 2006 - 04:57 AM

this should work for both of your situations:

<?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>";
   }
?>

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 ak7861

ak7861
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 23 October 2006 - 06:00 AM

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?

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 23 October 2006 - 06:26 AM

this should work for both of your situations:

<?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']);
   }

   


Simpler is
<?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;
   }
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 ak7861

ak7861
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 23 October 2006 - 09:06 AM

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.

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 23 October 2006 - 11:14 AM

<?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>';
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 23 October 2006 - 01:44 PM

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.

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.


that's exactly what we've done. in my example $coords and in barand's example $data - that's the array.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#10 ak7861

ak7861
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 23 October 2006 - 08:16 PM

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

#11 roopurt18

roopurt18
  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 23 October 2006 - 08:42 PM

He's not declaring a new array, he's appending the current row of the returned dataset to the end of the current array.
PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com

#12 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 23 October 2006 - 08:47 PM

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 position 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.

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users