Solved

Populating arrays

Posted on 2013-11-16
11
148 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 108

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
 

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

919 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now