Solved

Populating arrays

Posted on 2013-11-16
11
151 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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 110

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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)

739 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