Solved

Populating arrays

Posted on 2013-11-16
11
150 Views
Last Modified: 2014-03-27
This is a stupid question.  I'm sure somebody can answer it in a second.
Here's the code:

$right_array = array(
                  "BNMB" => "",
                  "STRT" => "",
                  "STOREKEY" => "",
                  "SEQ" => ""
                  );
$right_array_count= 0;
while ($ROW = Mysql_fetch_assoc($mapfile, MYSQL_BOTH))
      {
            if ($ROW['BNMB'] % 2 == 0)
            {
                  $fred = "It's even";
                  $left = "N/A";
                  $right = $right_array_count;
*****                  $right_array[$right_array_count] =   *****
                  $right_array_count = ($right_array_count + 1);

I need to populate the "right_array" with the vars from the query.  Everything on the net is either about an array with no multiple vars or 2 dimensional arrays.

I just need the syntax.

Thanks
Glenn
0
Comment
Question by:breeze351
  • 5
  • 4
  • 2
11 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39653820
Yes.  Your array is not organized by numbers but text keys:  "BNMB", "STRT", "STOREKEY", "SEQ".  It must be accessed with those keys, not by numbers.

$right_array["BNMB"]
$right_array["STRT"]
$right_array["STOREKEY"]
$right_array["SEQ"]
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39653860
Fortunately all of the PHP functions are documented on the PHP.net web site!

mysql_fetch_assoc()
mysql_fetch_array()

If you're not 100% sure what a PHP function does, you can look it up at the click of a mouse.

If you're new to PHP and want to learn more about how to use the language, this article may help you get started.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
 

Author Comment

by:breeze351
ID: 39653977
Dave:
I think I'm going about this the wrong way or I'm explaining it wrong.  The language that I am familiar with deals with arrays in a different way.  I can read a file and store variables in the array.  Each record in the data file will have a different line in the array.

What I'm trying to do is:
1) Query the data base for buildings.
2) Store the info that I need into 2 arrays based on odd or even building #.
3) When the query is over, display the odd #'s on the left and the even #'s on the right.

Don't I have to tell PHP what line to insert into the array?  Or is an array in PHP only 1 line?

I just thought of something, is what I'm trying a 2 dimensional array in PHP that has to be defined as such?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:breeze351
ID: 39653981
Ray:
Thank you for your reply.
However, all the links gave me are "DEPRECATED"!!!
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39654009
You are going about it wrong.  Arrays in PHP do not have to have numbered elements.  They can be text 'keys' instead.  This page http://us1.php.net/manual/en/language.types.array.php tells you all about it.

And $ROW['BNMB'] is one element in the array $ROW.  Note that variable names are case sensitive in PHP.  And on Linux, the column names are case sensitive also.

And the 'mysql' functions are Deprecated.  'mysqli' has been the preferred driver for about 6 years.  However, you are probably not using PHP 5.5.0 so you don't have to worry about it yet.
0
 

Author Comment

by:breeze351
ID: 39654023
Dave:
This code works:

while ($ROW = Mysql_fetch_assoc($mapfile, MYSQL_BOTH))
      {
            if ($ROW['BNMB'] % 2 == 0)
            {
                  $fred = "It's even";
                  $left = "N/A";
                  $right_array[$right_array_count]["BNMB"] = $ROW['BNMB'];
                  $right_array[$right_array_count]["STRT"] = $ROW['STRT'];
                  $right_array[$right_array_count]["STOREKEY"] = $ROW['STOREKEY'];
                  $right_array[$right_array_count]["SEQ"] = $ROW['SEQ'];
                  $right_array_count = ($right_array_count + 1);
                  $right = $right_array_count;
            }
            else
            {
                   $fred = "It's odd";

...................

$count = 0;
echo "Right<br>";

      while ($count < $right_array_count)
      {
            echo
                  $right_array[$count]['BNMB']."^".
                  $right_array[$count]['STRT']."^".
                  $right_array[$count]['STOREKEY']."^".
                  $count.
                  "<br>";
            $count++;
      }

.................

Is something wrong with this?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39654049
Technically no but it does Not match the $right_array variable in your original post.  I suggest you do a 'var_dump' on all three versions so you can see the difference.  In your original post, you would have $right_array["BNMB"].  In the code above, you would have $right_array[0]["BNMB"].
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39654495
Glad you noticed that MySQL is deprecated!  I was afraid you had not read the manual for those functions since you posted this:

Mysql_fetch_assoc($mapfile, MYSQL_BOTH))

When you get ready to convert to the current DB extensions, this article will help.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

If you want to show us the query statement and a sample of the test data, we might be able to help you get the data you want more directly, without all of the PHP code.   Just a thought... ~Ray
0
 

Author Comment

by:breeze351
ID: 39654892
Dave:

This code does work.  Check it out.  I'm not worried about giving the login as this is only test data.

Site: langsystems.net
User: sales
Password: lsi

Click "Walk By Map"
The address is "575" "lex"

When you look at this, you'll probably see what I'm trying to do.

Glenn
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 39654902
That's nice, Glenn, but that is different than your original question.   That's all I was saying.
0
 

Author Closing Comment

by:breeze351
ID: 39655427
Thanks Dave
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question