Solved

Populating arrays

Posted on 2013-11-16
11
147 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 82

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 82

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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 82

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 82

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

758 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

19 Experts available now in Live!

Get 1:1 Help Now